*Memo:
- My post explains a tuple (2).
- My post explains a tuple (3).
- My post explains a tuple (4).
- My post explains a tuple (5).
- My post explains tuple functions.
- My post explains a tuple comprehension.
- My post explains a tuple shallow and deep copy.
- My post explains a list (1).
- My post explains a set (1).
- My post explains a frozenset (1).
- My post explains a dictionary (1).
- My post explains an iterator (1).
- My post explains a string (1).
- My post explains a bytes (1).
- My post explains a bytearray (1).
- My post explains a range (1).
A tuple:
- is the ordered immutable(hashable) collection of zero or more elements whose type is
tuple
:- Ordered means that the order of each element in a tuple is kept so it guarantees that the order is always the same.
- Immutable(Hashable) means the elements of a tuple cannot be changed.
- allows duplicated elements.
- can have any types of elements.
- can be used with len() to get the length.
- is
False
if it's empty. - can be checked if a specific element is in the tuple with
in
keyword. - can be checked if the tuple is referred to by two variables with
is
keyword. - and one or more tuples can be concatenated with
+
but not with|
. - can be enlarged with
*
and a number. - can be iterated with a
for
statement. - can be unpacked with an assignment and
for
statement, function and*
but not with**
. - can be created by
()
and/or','
, by tuple() with or without an iterable and by a tuple comprehension:- For
tuple()
, the words type conversion are also suitable in addition to the word creation.
- For
- cannot be big because it gets
MemoryError
. - can be read by indexing and slicing.
- cannot be changed by indexing, slicing and a del statement.
- can be continuously used through multiple variables.
- cannot be shallow-copied by copy.copy(), tuple().
- cannot be deep-copied and even shallow-copied by copy.deepcopy().
Be careful, a big tuple gets MemoryError
.
MemoryError
.()
and/or ','
can create a tuple as shown below:
v = () # Empty 1D tuple
v = (0,) # 1D tuple
v = 0, # 1D tuple
v = (0) # int not tuple
v = 0 # int not tuple
v = (0), # 1D tuple
v = (0, 1, 2, 3, 4) # 1D tuple
v = 0, 1, 2, 3, 4 # 1D tuple
v = (0, 1, 2, 0, 1, 2) # 1D tuple
v = ((0,),) # 2D tuple
v = (((0),)), # 2D tuple
v = (0, 1, 2, 3, (4, 5, 6, 7)) # 2D tuple
v = ((0, 1, 2, 3), (4, 5, 6, 7)) # 2D tuple
v = (((0,),),) # 3D tuple
v = (((((0),)),)), # 3D tuple
v = ((0, 1, 2, 3), ((4, 5), (6, 7))) # 3D tuple
v = (((0, 1), (2, 3)), ((4, 5), (6, 7))) # 3D tuple
# No error
v = (0, 0.0, 0.0+0.0j, False)
v = (1, 1.0, 1.0+0.0j, True)
v = ('A', b'A', bytearray(b'A'), 2, 2.3, 2.3+4.5j, True,
[2, 3], (2, 3), {2, 3}, frozenset({2, 3}), {'A':'a'},
range(2, 3), iter([2, 3]))
print(len((0, 1, 2, 3, 4)))
v = (0, 1, 2) + ((3, 4),) + (((5, 6, 7, 8),),)
v = (0, 1, 2, 3, 4) * 3
v = ('A', 'B', 'C', 'D', 'E') * 3
v = () * 3
for x in (0, 1, 2, 3, 4): pass
for x in ((0, 1, 2, 3), (4, 5, 6, 7)): pass
for x in (((0, 1), (2, 3)), ((4, 5), (6, 7))): pass
v1, v2, v3 = (0, 1, 2)
v1, *v2, v3 = (0, 1, 2, 3, 4, 5)
for v1, v2, v3 in ((0, 1, 2), (3, 4, 5)): pass
for v1, *v2, v3 in ((0, 1, 2, 3, 4, 5), (6, 7, 8, 9, 10, 11)): pass
print(*(0, 1), 2, *(3, 4, *(5,)))
print((*(0, 1), 2, *(3, 4, *(5,))))
v = tuple(x**2 for x in (0, 1, 2, 3, 4, 5, 6, 7))
v = tuple(tuple(y**2 for y in x) for x in ((0, 1, 2, 3), (4, 5, 6, 7)))
v = tuple(tuple(tuple(z**2 for z in y) for y in x) for x in
(((0, 1), (2, 3)), ((4, 5), (6, 7))))
# No error
v = (0, 1, 2) | ((3, 4),) | (((5, 6, 7, 8),),)
v = (0, 1, 2, 3, 4) * 1000000000
v = tuple(range(1000000000))
v = tuple(x for x in range(1000000000))
# Error
A tuple is the ordered immutable(hashable) collection of zero or more elements whose type is tuple
as shown below:
v = (0, 1, 2, 3, 4)
v = 0, 1, 2, 3, 4
print(v)
# (0, 1, 2, 3, 4)
print(type(v))
# <class 'tuple'>
v[1] = 'X'
v[3] = 'Y'
# TypeError: 'tuple' object does not support item assignment
v = (0,)
v = 0,
v = ((0,))
print(v)
# (0,)
v = () # Empty tuple
print(v)
# ()
A tuple allows duplicated elements as shown below:
v = (0, 1, 2, 0, 1, 2)
print(v)
# (0, 1, 2, 0, 1, 2)
v = (0, 0.0, 0.0+0.0j, False)
print(v)
# (0, 0.0, 0j, False)
v = (1, 1.0, 1.0+0.0j, True)
print(v)
# (1, 1.0, (1+0j), True)
A tuple can have any types of elements as shown below:
v = ('A', b'A', bytearray(b'A'), 2, 2.3, 2.3+4.5j, True,
[2, 3], (2, 3), {2, 3}, frozenset({2, 3}), {'A':'a'},
range(2, 3), iter([2, 3]))
print(v)
# ('A', b'A', bytearray(b'A'), 2, 2.3, (2.3+4.5j), True,
# [2, 3], (2, 3), {2, 3}, frozenset({2, 3}), {'A': 'a'},
# range(2, 3), <list_iterator object at 0x000001F3B99BF250>)
A tuple can be used with len()
to get the length as shown below:
v = (0, 1, 2, 3, 4)
print(len(v))
# 5
An empty tuple is False
as shown below:
print(bool(())) # Empty tuple
# False
print(bool((0,))) # tuple
print(bool(((),))) # tuple(Empty tuple)
# True
A tuple can be checked if a specific element is in the tuple with in
keyword as shown below:
v = ('A', ('B', 'C'))
print('A' in v)
# True
print(('B', 'C') in v)
# True
print('a' in v)
print('B' in v)
print('C' in v)
print(('B',) in v)
print(('C',) in v)
print(('A', ('B', 'C')) in v)
# False
A tuple and one or more tuples can be concatenated with +
but not 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),))
v = (0, 1, 2) | ((3, 4),) | (((5, 6, 7, 8),),)
# TypeError: unsupported operand type(s) for |: 'tuple' and 'tuple'