May 2019
Beginner
528 pages
29h 51m
English
The previous chapter introduced tuple unpacking. You can unpack any sequence’s elements by assigning the sequence to a comma-separated list of variables. A ValueError occurs if the number of variables to the left of the assignment symbol is not identical to the number of elements in the sequence on the right:
In [1]: student_tuple = ('Amanda', [98, 85, 87])In [2]: first_name, grades = student_tupleIn [3]: first_nameOut[3]: 'Amanda'In [4]: gradesOut[4]: [98, 85, 87]
The following code unpacks a string, a list and a sequence produced by range:
In [5]: first, second = 'hi'In [6]: print(f'{first} {second}')h iIn [7]: number1, number2, number3 = [2, 3, 5]In [8]: print(f'{number1} {number2} {number3}')2 3 5In [9]: ...
Read now
Unlock full access