Assignment statements

In its basic form an assignment statement is made of an equal sign = with the target of an assignment on the left and the object to be assigned on the right.

Assignment statement forms

Below you can see different assignment statement forms in Python, and their syntax patterns.

Assignment Statement – Basic Form

>>> a = 'Dan'
>>> a
'Dan'

Assignment Statement – Tuple Assignment

>>> a, b = 'web', 'developer'
>>> (a, b)
('web', 'developer')

Assignment Statement – List Assignment

>>> [a, b] = ['Dan', 40]
>>> a
'Dan'
>>> b
40

Assignment Statement – Sequence Assignment Generalized

>>> a, b, c = 'Dan'
>>> a
'D'
>>> b
'a'
>>> c
'n'

Assignment Statement – Extended Sequence Unpacking

>>> a, b, c, *d = 'developer'
>>> a
'd'
>>> b
'e'
>>> c
'v'
>>> d
['e', 'l', 'o', 'p', 'e', 'r']

Assignment Statement – Multiple-target assignment

>>> a = b = 'developer'
>>> a
'developer'
>>> b
'developer'
>>> a == b
True

Assignment Statement – Augmented assignment

>>> a = 5
>>> a += 2
>>> a
7
>>> a += 4
>>> a
11

Other possibilities beside +=:

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

Leave a Reply