Q. Multiple Assignment

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)

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 when generating figures and axes while plotting data.

import matplotlib.pyplot as plt

f, ax = plt.subplots()

This one-line command will create the figure and the axes. Other use cases can be seen in the following stack-overflow question: https://stackoverflow.com/questions/5182573/multiple-assignment-semantics

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.28.53