December 2017
Beginner to intermediate
410 pages
12h 45m
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 wanted 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)
print(b)
print(c)
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)
print(b1)
print(c1)
Multiple assignment is often used ...