DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

Tuple in Python (1)

Buy Me a Coffee

*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 a tuple (6).
  • My post explains tuple functions.
  • My post explains 10 collection types and their related posts.

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 True if it's non-empty and False if it's empty, checking it with bool().
  • is False if it's non-empty and True if it's empty, inverting the truth value with not.
  • can be checked if a specific element is and isn't in the tuple with in and not in respectively.
  • can be checked if the tuple is and isn't referred to by two variables with is and is not respectively:
    • Be careful, tuple literals with is and is not get warnings so use == and != respectively.
  • and other tuple can be checked if:
    • all the elements in them are and aren't equal with == and != respectively:
      • == and != can also check if types of values are and aren't the same respectively.
    • the tuple is greater than other tuple with >.
    • the tuple is greater than or equal to other tuple with >=.
    • the tuple is less than other tuple with <.
    • the tuple is less than or equal to other tuple with <=.
  • and other tuple cannot be checked if they have and don't have their common elements with bool() and & and with not and & respectively.
  • can be enlarged with * and a number.
  • and other tuples can be concatenated with +.
  • 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).
  • 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.
  • 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.


() 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

print((0, 0.0, 0.0+0.0j, False))
print((1, 1.0, 1.0+0.0j, True))
print(('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)))
print(bool((0,)))
print(bool(((),)))
print(bool(()))
print(not (0,))
print(not ((),))
print(not ())
print('A' in ('A', ('B', 'C')))
print('A' not in ('A', ('B', 'C')))
print((0, 1, 2) is (0, 1, 2))     # It gets warning so use `==`.
print((0, 1, 2) is not (0, 1, 2)) # It gets warning so use `!=`.
print((0, 1, 2) == (0, 1, 2))
print((0, 1, 2) != (0, 1, 2))
print((0, 1, 2) <= (0, 1, 2))
print((0, 1, 2) >= (0, 1, 2))
print((0, 1, 2) < (0, 1, 2))
print((0, 1, 2) > (0, 1, 2))
print((0, 1, 2, 3, 4) * 3)
print(('A', 'B', 'C', 'D', 'E') * 3)
print(() * 3)
print((0, 1, 2) + ((3, 4),) + (((5, 6, 7, 8),),))
for x in (0, 1, 2, 3, 4): print(x)
for x in ((0, 1, 2, 3), (4, 5, 6, 7)): print(x)
for x in (((0, 1), (2, 3)), ((4, 5), (6, 7))): print(x)
v1, v2, v3 = (0, 1, 2); print(v1, v2, v3)
v1, *v2, v3 = (0, 1, 2, 3, 4, 5); print(v1, v2, v3)
for v1, v2, v3 in ((0, 1, 2), (3, 4, 5)): print(v1, v2, v3)
for v1, *v2, v3 in ((0, 1, 2, 3, 4, 5),
                    (6, 7, 8, 9, 10, 11)): print(v1, v2, v3)
print(*(0, 1), 2, *(3, 4, *(5,)))
print((*(0, 1), 2, *(3, 4, *(5,))))
print(tuple(x**2 for x in (0, 1, 2, 3, 4, 5, 6, 7)))
print(tuple(tuple(y**2 for y in x) for x in ((0, 1, 2, 3), (4, 5, 6, 7))))
print(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

print(bool((0, 1, 2) & (1, 3)))
print(not ((0, 1, 2) & (1, 3)))
print((0, 4) | (0, 2, 4))
print((0, 1, 2, 3) & (0, 2, 4))
print((0, 1, 2, 3) - (0, 2, 4))
print((0, 1, 2, 3) ^ (0, 2, 4))
print((0, 1, 2, 3, 4) * 1000000000)
print(tuple(range(1000000000)))
print(tuple(x for x in range(1000000000)))
# Error
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
v = (0,)
v = 0,
v = ((0,))

print(v)
# (0,)
Enter fullscreen mode Exit fullscreen mode
v = () # Empty tuple

print(v)
# ()
Enter fullscreen mode Exit fullscreen mode

A tuple allows duplicated elements as shown below:

v = (0, 1, 2, 0, 1, 2)

print(v)
# (0, 1, 2, 0, 1, 2)
Enter fullscreen mode Exit fullscreen mode
v = (0, 0.0, 0.0+0.0j, False)

print(v)
# (0, 0.0, 0j, False)
Enter fullscreen mode Exit fullscreen mode
v = (1, 1.0, 1.0+0.0j, True)

print(v)
# (1, 1.0, (1+0j), True)
Enter fullscreen mode Exit fullscreen mode

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>)
Enter fullscreen mode Exit fullscreen mode

A tuple can be used with len() to get the length as shown below:

v = (0, 1, 2, 3, 4)

print(len(v))
# 5
Enter fullscreen mode Exit fullscreen mode

Top comments (0)