*Memo:
- My post explains a tuple (1).
- My post explains a tuple (2).
- My post explains a tuple (4).
- My post explains a tuple (5).
- My post explains a tuple (6).
A tuple can be enlarged with * and a number as shown below:
<1D tuple>:
v = (0, 1, 2, 3, 4) * 3
print(v)
# (0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4)
v = ('A', 'B', 'C', 'E', 'D') * 3
print(v)
# ('A', 'B', 'C', 'E', 'D', 'A', 'B', 'C', 'E', 'D', 'A', 'B', 'C', 'E', 'D')
v = () * 3
print(v)
# ()
<2D tuple>:
v = ((0, 1, 2, 3, 4),) * 3
print(v)
# ((0, 1, 2, 3, 4), (0, 1, 2, 3, 4), (0, 1, 2, 3, 4))
v = (('A', 'B', 'C', 'D', 'E'),) * 3
print(v)
# (('A', 'B', 'C', 'D', 'E'),
# ('A', 'B', 'C', 'D', 'E'),
# ('A', 'B', 'C', 'D', 'E'))
v = ((),) * 3
print(v)
# ((), (), ())
<3D tuple>:
v = (((0, 1, 2, 3, 4),),) * 3
print(v)
# (((0, 1, 2, 3, 4),), ((0, 1, 2, 3, 4),), ((0, 1, 2, 3, 4),))
v = ((('A', 'B', 'C', 'D', 'E'),),) * 3
print(v)
# ((('A', 'B', 'C', 'D', 'E'),),
# (('A', 'B', 'C', 'D', 'E'),),
# (('A', 'B', 'C', 'D', 'E'),))
v = (((),),) * 3
print(v)
# (((),), ((),), ((),))
A tuple can be enlarged with * and a number as shown below:
<1D tuple>:
v = (0, 1, 2, 3, 4) * 3
print(v)
# (0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4)
v = ('A', 'B', 'C', 'E', 'D') * 3
print(v)
# ('A', 'B', 'C', 'E', 'D', 'A', 'B', 'C', 'E', 'D', 'A', 'B', 'C', 'E', 'D')
v = () * 3
print(v)
# ()
<2D tuple>:
v = ((0, 1, 2, 3, 4),) * 3
print(v)
# ((0, 1, 2, 3, 4), (0, 1, 2, 3, 4), (0, 1, 2, 3, 4))
v = (('A', 'B', 'C', 'D', 'E'),) * 3
print(v)
# (('A', 'B', 'C', 'D', 'E'),
# ('A', 'B', 'C', 'D', 'E'),
# ('A', 'B', 'C', 'D', 'E'))
v = ((),) * 3
print(v)
# ((), (), ())
<3D tuple>:
v = (((0, 1, 2, 3, 4),),) * 3
print(v)
# (((0, 1, 2, 3, 4),), ((0, 1, 2, 3, 4),), ((0, 1, 2, 3, 4),))
v = ((('A', 'B', 'C', 'D', 'E'),),) * 3
print(v)
# ((('A', 'B', 'C', 'D', 'E'),),
# (('A', 'B', 'C', 'D', 'E'),),
# (('A', 'B', 'C', 'D', 'E'),))
v = (((),),) * 3
print(v)
# (((),), ((),), ((),))
A tuple and other tuples can be concatenated with + as shown below:
v = (0, 1, 2) + ((3, 4),) + (((5, 6, 7, 8),),)
print(v)
# (0, 1, 2, (3, 4), ((5, 6, 7, 8),))
A tuple and other tuple cannot return:
- all the elements in them with
'|'(Union: A ∪ B). - their common elements with
'&'(Intersection: A ∩ B). - the elements in the tuple which aren't in other tuple with
'-'(Difference: A - B). - the elements in either the tuple or other tuple but not both with '^' (Symmetric Difference: A Δ B).
v = (0, 4) | (0, 2, 4)
# TypeError: unsupported operand type(s) for |: 'tuple' and 'tuple'
v = (0, 1, 2, 3) & (0, 2, 4)
# TypeError: unsupported operand type(s) for &: 'tuple' and 'tuple'
v = (0, 1, 2, 3) - (0, 2, 4)
# TypeError: unsupported operand type(s) for -: 'tuple' and 'tuple'
v = (0, 1, 2, 3) ^ (0, 2, 4)
# TypeError: unsupported operand type(s) for ^: 'tuple' and 'tuple'
A tuple can be iterated with a for statement as shown below:
<1D tuple>:
for x in (0, 1, 2, 3, 4):
print(x)
# 0
# 1
# 2
# 3
# 4
<2D tuple>:
for x in ((0, 1, 2, 3), (4, 5, 6, 7)):
for y in x:
print(y)
# 0
# 1
# 2
# 3
# 4
# 5
# 6
# 7
<3D tuple>:
for x in (((0, 1), (2, 3)), ((4, 5), (6, 7))):
for y in x:
for z in y:
print(z)
# 0
# 1
# 2
# 3
# 4
# 5
# 6
# 7
A tuple can be unpacked with an assignment and for statement, function and * but not with ** as shown below:
v1, v2, v3 = (0, 1, 2)
print(v1, v2, v3)
# 0 1 2
v1, *v2, v3 = (0, 1, 2, 3, 4, 5)
print(v1, v2, v3) # 0 [1, 2, 3, 4] 5
print(v1, *v2, v3) # 0 1 2 3 4 5
for v1, v2, v3 in ((0, 1, 2), (3, 4, 5)):
print(v1, v2, v3)
# 0 1 2
# 3 4 5
for v1, *v2, v3 in ((0, 1, 2, 3, 4, 5), (6, 7, 8, 9, 10, 11)):
print(v1, v2, v3)
print(v1, *v2, v3)
# 0 [1, 2, 3, 4] 5
# 0 1 2 3 4 5
# 6 [7, 8, 9, 10] 11
# 6 7 8 9 10 11
print(*(0, 1), 2, *(3, 4, *(5,)))
# 0 1 2 3 4 5
print((*(0, 1), 2, *(3, 4, *(5,))))
# (0, 1, 2, 3, 4, 5)
def func(p1='a', p2='b', p3='c', p4='d', p5='e', p6='f'):
print(p1, p2, p3, p4, p5, p6)
func()
# a b c d e f
func(*(0, 1, 2, 3), *(4, 5))
# 0 1 2 3 4 5
def func(p1='a', p2='b', *args):
print(p1, p2, args)
print(p1, p2, *args)
print(p1, p2, ['A', 'B', *args, 'C', 'D'])
func()
# a b ()
# a b Nothing
# a b ['A', 'B', 'C', 'D']
func(*(0, 1, 2, 3), *(4, 5))
# 0 1 (2, 3, 4, 5)
# 0 1 2 3 4 5
# 0 1 ['A', 'B', 2, 3, 4, 5, 'C', 'D']
Top comments (0)