Assignments in Python#
(Click here for the German version of this page)
The assignment operator#
As we have already seen, Python also uses =
as the normal assignment operator, just like most other programming languages. The basic function of said operator needs no further introduction.
But in Python it has additional functionality. Normally the assignment operator has one operand on the left side and one operand on the right side, i.e.,
a = b
But Python does not have this restriction. Simply put, there just have to be the same number of operands on both sides. Therefore, it is possible to perform multiple assignments in one operation. The operands on each side are separated by commas.
Example:
# a should get the value 5 and b should get the value 10
a,b = 5,10
Or expressed more generally:
variable1, variable2, ... = value_for_variable1, value_for_variable2, ...
If both sides do not have equal length, it will not work, just like in this example:
a,b = 10
a,b = 5,10,15
# --> ValueError: too many values to unpack (expected 2)
If one wants to assign the same value to multiple variables, it should be done like this:
a,b = 10,10
Exercise#
Operands on the right side can be literal values or variables.
What do you think would happen if one were to do this:
a = 5
b = 10
a,b = b,a
# What value will a have?
# What value will b have?
# Is this an illegal operation,
# which results in an error?
Solution
Solution: a will be 10, b will be 5
A swap of the two variables was performed.
One can imagine it as a shortcut for:
temp = a
a = b
b = temp
# Swapping of three or more variables is also possible.
# But swapping two variables is the more common use case.
More generally: First all operands on the right side are evaluated, and only then are they assigned to the left side.