Hướng dẫn tuple assignment python - trăn phân công tuple

Trong bài học này, bạn sẽ tìm hiểu về phân công, đóng gói và giải nén.Một tuple theo nghĩa đen chứa một số mục có thể được gán cho một đối tượng.Việc gán đối tượng được đóng gói cho một tuple mới giải nén các mục riêng lẻ vào các đối tượng trong bộ tuple mới:packed object to a new tuple unpacks the individual items into the objects in the new tuple:

>>>

>>> t = ('spam', 'egg', 'bacon', 'tomato')
>>> t
('spam', 'egg', 'bacon', 'tomato')
>>> t[0]
'spam'
>>> t[1]
'egg'

>>> (s1, s2, s3, s4) = t
>>> s1
'spam'
>>> s2
'egg'
>>> s3
'bacon'
>>> s4
'tomato'

>>> (s1, s2, s3) = t
Traceback (most recent call last):
  File "", line 1, in 
    (s1, s2, s3) = t
ValueError: too many values to unpack (expected 3)

>>> t
('spam', 'egg', 'bacon', 'tomato')
>>> (s1, s2, s3, s4, s5) = t
Traceback (most recent call last):
  File "", line 1, in 
    (s1, s2, s3, s4, s5) = t
ValueError: not enough values to unpack (expected 5, got 4)

>>> (s1, s2, s3, s4) = ('spam', 'egg', 'bacon', 'tomato')
>>> s1
'spam'
>>> s2
'egg'
>>> s3
'bacon'
>>> s4
'tomato'

>>> (s1, s2, s3) = ('spam', 'egg', 'bacon', 'tomato')
Traceback (most recent call last):
  File "", line 1, in 
    (s1, s2, s3) = t
ValueError: too many values to unpack (expected 3)

>>> t = 1, 2, 3
>>> t
(1, 2, 3)
>>> type(t)


>>> x1, x2, x3 = t
>>> x1, x2, x3
(1, 2, 3)
>>> x1, x2, x3 = 4, 5, 6
>>> x1, x2, x3
(4, 5, 6)

>>> t = 2,
>>> t
(2,)
>>> type(t)


>>> a = 'spam'
>>> b = 'egg'
>>> a, b
('spam', 'egg')
>>> # You need to define a temp variable to accomplish the swap.
>>> temp = a
>>> a = b
>>> b = temp
>>> a, b
('egg', 'spam')

>>> a = 'spam'
>>> b = 'egg'
>>> a, b
('spam', 'egg')
>>> # Ready for Magic Time!
>>> a, b = b, a
>>> a, b
('egg', 'spam')

>>> # Fibonacci series
>>> # the sum of two elements defines the next
>>> a, b = 0, 1
>>> while a < 30:
...    print(a)
...    a, b = b, a+b
...
0
1
1
2
3
5
8
13
21