Assigning multiple values at the same time
Here's a cool programming shortcut: In Python, you can use tuples to
Assigns multiple values.
>>> v = (' A ', 2, True)
>>> (x, y, z) = V①
>>> x
A
>>> y
2
>>> Z
True
1. V is a ternary tuple, and (x, Y, z) is a meta with three variables
Group. Assigning one of the values to another will assign each value in V in order
to each variable. This feature has many uses. Suppose you need to specify a name for a specific range of
Value. You can use the built-in range () function for multi-variable assignment to quickly
The row continuous variable assignment value.
>>> (MONDAY, Tuesday, Wednesday, Thursday, FRIDAY,
SATURDAY, SUNDAY) = range (7) ①
>>> MONDAY
Ii
0
>>> Tuesday
1
>>> SUNDAY
6
1. The built-in range () function constructs an integer sequence. (From Technology
says that the range () function returns neither a list nor a tuple, but an iterative
But you will learn the difference later. ) MONDAY, Tuesday,
Wednesday, Thursday, FRIDAY, SATURDAY and SUNDAY are what you decide
The variable that is justified. (This example comes from the Calendar module, which is a short and interesting module
Print a calendar, a bit like a UNIX program Cal. The Calendar module is the number of weeks
An integer constant is defined.
2. Now, each variable has its value: MONDAY is 0, Tuesday is 1,
So the analogy.
You can also use a multivariate assignment to create a function that returns multiple values by simply returning a package
A tuple with all values. The caller can treat the return value as a simple tuple, or
Assign it to a different variable.