December 2022
Beginner to intermediate
512 pages
14h 57m
English
Multiple assignment in Python is a form of syntactic sugar. It provides the programmer with the ability to express something succinctly while making this information easier to express and to be understood by others.
As an example, let’s use a list of values.
l = [1, 2, 3]
If we want to assign a variable to each element of this list, we can subset the list and assign the value.
a = l[0]
b = l[1]
c = l[2]
print(a)
1
print(b)
2
print(c)
3
With multiple assignment, if the statement to the right is some kind of container, we can directly assign its values to multiple variables on the left. So, the preceding code can be rewritten as follows:
a1, b1, c1 = l
print(a1)
1
print(b1)
2
print(c1)
3
Multiple assignment is often used ...
Read now
Unlock full access