Assigns a value to a variable(s).
A = B
- A
- Any valid object.
- B
- Any valid object.
According to coercion rules.
#TODO
Assignment operation always works from right to left. The object that was referenced by the variable prior to the assignment is now dereferenced.
>>> a = 10
>>> a = 5
>>> a
5Multiple assignment also follows the right-to-left rule. Consider the following example:
>>> a = b = c = 10
>>> a
10
>>> b
10
>>> c
10First the integer 10 is assigned to the variable c, then the value of c (10) is assigned to b and b is assigned to a. After evaluating this expression all the variables are referencing the same object - integer 10.
Another case of assignment is a multi-variable assignment. Consider the following example:
>>> a, b, c = 1, 2, 3 # <- this is a tuple
>>> a
1
>>> b
2
>>> c
3Following right-to-left rule a tuple containing (1, 2, 3) is created, then it is iterated over and its consequent values are assigned to the comma separated list of variables on the left.
This syntax can be used to swap the values of two variables:
>>> a = 0
>>> b = 1
>>> a, b = b, a
>>> a
1
>>> b
0Another common use is when we want to assign new values based on existing values:
>>> a = 10
>>> b = 5
>>> a, b = a + b, a * b
>>> a
15
>>> b
50this is different than:
>>> a = 10
>>> b = 5
>>> a = a + b
>>> b = a * b
>>> a
15
>>> b
75#TODO