Next / Previous / Contents / TCC Help System / NM Tech homepage

22.1. The assignment statement: name = expression

The purpose of Python's assignment statement is to associate names with values in your program. It is the only statement that does not start with a keyword. An assignment statement is a line containing at least one single equal sign (=) that is not inside parentheses.

Here is the general form of an assignment statement:

target0 = target1 = ... = expression

In most cases, there will be one target that is a name. Python will evaluate the expression, reducing it to a single value, and then bind that name to the that value.

A binding is an association between a name and a value. It is important to note that in Python, unlike many other languages, names themselves are not associated with a specific type. A name is just a label, and it can be bound to any value of any type at any time. In this example, name x is bound first to an int value 5, then to a str value 'Some string'.

>>> x = 5
>>> x
5
>>> x = 'Some string'
>>> print x
Some string

If a target name was already bound to a value, the name is unbound from that value before it is rebound to the new value. For each value in a running program, Python keeps track of how many names are bound to that value. When the value has no more names bound to it, the value's memory is automatically recycled. If the value is an instance of a class, its destructor may be called; see Section 26.3.8, “__del__(): Destructor”.

There are several other forms of assignment statement.

n0 = n1 = ... = expression

If you supply multiple target names, each target will be assigned the value of the expression. Example:

>>> i = j = errorCount = 0
>>> i
0
>>> j
0
>>> errorCount
0

n0, n1, ... = expression

If the target is a comma-separated list of names, the expression must evaluate to an iterable with the same number of values as there are names. Each value in the expression is then bound to the corresponding name. Example:

>>> L = ["Bell's Blue", "male", 6]
>>> name, sex, age = L
>>> name
"Bell's Blue"
>>> sex
'male'
>>> age
6

This feature, called “unpacking,” generalizes to arbritrarily nested sequences within sequences. You may group targets inside parentheses (...) or brackets [...] to show the levels of nesting. Here is an example:

>>> s = [1, [2, 3, [4, 5], 6], 7]
>>> a, (b, c, [d, e], f), g = s
>>> print a,b,c,d,e,f,g
1 2 3 4 5 6 7

All the assignments are effectively simultaneous. Therefore, you can safely exchange the values of two variables using a statement like this:

v1, v2 = v2, v1

Examples:

>>> a=5; b=9998
>>> print a,b
5 9998
>>> a,b=b,a
>>> print a,b
9998 5
>>> c=432
>>> a,b,c = b,c,a
>>> print a,b,c
5 432 9998

name[i] = expression

If name is an iterable, the expression i must evaluate to an integer. The element after position i is replaced by the value of the expression.

>>> L = range(6)
>>> L
[0, 1, 2, 3, 4, 5]
>>> L[2]
2
>>> L[2] = 888
>>> L
[0, 1, 888, 3, 4, 5]

If name is a dictionary (or other mapping), and name does not have a key-value pair whose key equals index, a new key-value pair is added to name with key i and value expression.

>>> d={'pudding': 'figgy'}
>>> d
{'pudding': 'figgy'}
>>> d['tart'] = 'strawberry'
>>> d
{'pudding': 'figgy', 'tart': 'strawberry'}
>>> d["tart"] = "rat"
>>> d
{'pudding': 'figgy', 'tart': 'rat'}

As the last two lines show, if the dictionary already has a key-value pair for key i, the old value of that pair is replaced by the expression value.

name[start:end] = S

If name is a list or other mutable sequence, you can replace the elements of a slice of that sequence with the elements from some sequence S. (For an explanation of slicing, see Section 8.1, “Operations common to all the sequence types”.) This may result in addition, deletion, or replacement of the elements of name. Some examples will give the flavor of this kind of assignment.

>>> L=range(6)
>>> L
[0, 1, 2, 3, 4, 5]
>>> L[2:4]
[2, 3]
>>> L[2:4] = [111, 222, 333, 444, 555]
>>> L
[0, 1, 111, 222, 333, 444, 555, 4, 5]
>>> L[3]
222
>>> L[3:3]
[]
>>> L[3:3]  =  [41.0, 42.0, 43.0]
>>> L
[0, 1, 111, 41.0, 42.0, 43.0, 222, 333, 444, 555, 4, 5]
>>> L[4:7]
[42.0, 43.0, 222]
>>> L[4:7] = ()
>>> L
[0, 1, 111, 41.0, 333, 444, 555, 4, 5]

Note

The “=” signs in an assignment is not an operator, as it is in some other languages. You cannot assign a value to a name inside an expression; an assignment statement must stand alone.

>>> a = 5 + (a=7)
  File "<stdin>", line 1
    a = 5 + (a=7)
              ^
SyntaxError: invalid syntax

Python also supports augmented assignment. In this form, you may place certain operators before the “=”. Here is the general form:

name operator= expression

An assignment of this general form has the same semantics as this form:

name = name operator expression

Supported operator symbols include:

+  -  *  /  %  **  >>  <<  &  ^  |

Examples:

>>> i = 1
>>> i += 3
>>> i
4
>>> i *= 5
>>> i
20