Simple cases of item assignment don't require much
explanation: X[2] += 1
makes an increment
to the third item of list X (or if X is a dictionary,
to the item that has 2 as its key). Less obvious
are assigments using multiple indices:
1 2 3 4 5 6 7 8 9 10 | A = [2,1,5]
B = [False,1,7]
C = list(range(20))
D = [A,6,0,B,"thing",C]
B[0] = D[3][2]
D[5][2] = "stop"
D[4][0] = 'J'
D[3] += 8
D[4] *= 0
D[2] += [9]
|
Some of these assignments are Python errors:
Line 7 is an error because D[4]
is
a string; thus D[4][0] = 'J'
attempts
item assignment to a string, which is an error
in Python.
Line 8 is an error because D[3]
is
a list; adding a number to a list results in a
type error.
Line 10 is an error because D[2]
is
a number; adding a list to a number, or trying
to concatenate a number to a list, is a type
error for the "+" operator.