
Python Tuple (iteration, empty tuple, length, index, addition)
A Python tuple is a ordered and immutable set of objects declared with parentheses.
a = (1, 2, 3)
print(a) # (1, 2, 3)
print(type(a)) # <class 'tuple'>
A tuple has an order like list. A set doesn't have an order.
a = (2, 3, 1)
b = {2, 3, 1}
print(a) # (2, 3, 1)
print(b) # {1, 2, 3}
A tuple is often used in NumPy, pandas, and common SQL libraries.
Iterate tuple
A Python tuple can be iterated and each element of a tuple is accessed in for statement.
a = (2, 3, 1)
for i in a:
print(i)
# 2
# 3
# 1
Any object can be an element of a tuple.
x = [1, 2]
a = ('A', 3, x, 0.12)
for i in a:
print(i)
# A
# 3
# [1, 2]
# 0.12
The empty tuple
Only parentheses mean an empty tuple.
a = ()
b = (1, 2, 3)
print(type(a)) # <class 'tuple'>
print(type(b)) # <class 'tuple'>
A tuple containing only one element
t = (1)
print(t) # 1
print(type(t)) # <class 'int'>
A tuple that has only one element is not a tuple.
The Length of a tuple
The len()
returns the number of elements in a tuple. This number is called the "length" of a tuple.
a = ()
b = (1, 2, 3)
x = len(a)
y = len(b)
print(x) # 0
print(y) # 3
The length of an empty tuple is 0.
The Index of a tuple
The first element of a tuple has 0 index.
a = (5, 6, 7)
print(a[0]) # 5
print(a[1]) # 6
print(a[2]) # 7
print(a[3]) # IndexError: tuple index out of range
print(a[-1]) # 7
print(a[-2]) # 6
print(a[-3]) # 5
print(a[-4]) # IndexError: tuple index out of range
The number inside brackets is called index
and in this case index can be -3, -2, -1, 0, 1, 2. Other index is error because this tuple has only 3 items.
Add tuples
a = (5, 6, 7)
b = ('Apple', 'Microsoft')
c = a + b
print(c)
# (5, 6, 7, 'Apple', 'Microsoft')
Get a reversed tuple
Python slicing syntax enables to make the reversed tuple in the same way as reversing a list. You can't change a tuple itself because it's immutable. List has "reverse" method but tuple doesn't. How to reverse a tuple in Python explains in detail.
a = (4, 5, 6, 7)
b = a[::-1]
print(b) # (7, 6, 5, 4)
print(type(b)) # <class 'tuple'>
Unpack a tuple
Getting each value from a tuple is called unpack in Python.
a = (4, 5, 6)
x, y, z = a
print(x) # 4
print(y) # 5
print(z) # 6
A trailing comma is ignored in Python.
a = (4, 5, 6,)
x, y, z = a
print(x) # 4
print(y) # 5
print(z) # 6
Unpacked variables can be expressed by underscores as follows.
a = (4, 5, 6)
x, y, _ = a
print(x) # 4
print(y) # 5
In this case, the underscore is assigned the value.
a, b, _ = (1, 2, 3)
print(_) # 3
If there are more than one underscores, the underscore represents only the last element.
a, _, _ = (1, 2, 3)
print(_) # 3
The number of elements in a tuple and a trailing comma
A tuple may have a trailing comma. Whether the tuple has it or not, the number of elements remains the same.
a = (4, 5, 6, 7,)
print(len(a)) # 4
Tuple is immutable
A tuple is immutable so can't be added, removed, sorted. But you can assign other tuples to the same variable, though the id of it changes by assigning.
a = (4, 5, 6)
print(id(a)) # 4555645504
a = (7, 8)
print(id(a)) # 4554475456
String as a tuple
t = ('Mac' 'Book')
print(t) # MacBook
You can concatenate strings in a tuple without commas.
Comments
Powered by Markdown