🔒 Tuples¶
1. Definition and Initialization¶
In Python, a tuple is an ordered, immutable collection of items, enclosed in parentheses.
In [2]:
Copied!
# tuple of integers
t1 = (1, 2, 3, 4, 5)
print(t1) # prints: (1, 2, 3, 4, 5)
# tuple of strings
t2 = ('apple', 'banana', 'cherry')
print(t2) # prints: ('apple', 'banana', 'cherry')
# mixed tuple
t3 = (1, 'apple', True, 3.14, (1, 2, 3))
print(t3) # prints: (1, 'apple', True, 3.14, (1, 2, 3))
# tuple of integers
t1 = (1, 2, 3, 4, 5)
print(t1) # prints: (1, 2, 3, 4, 5)
# tuple of strings
t2 = ('apple', 'banana', 'cherry')
print(t2) # prints: ('apple', 'banana', 'cherry')
# mixed tuple
t3 = (1, 'apple', True, 3.14, (1, 2, 3))
print(t3) # prints: (1, 'apple', True, 3.14, (1, 2, 3))
(1, 2, 3, 4, 5) ('apple', 'banana', 'cherry') (1, 'apple', True, 3.14, (1, 2, 3))
2. Accessing Tuple Items¶
You can access tuple items by referring to their index number. Python uses 0-based indexing.
In [3]:
Copied!
t1 = (1, 2, 3, 4, 5)
print(t1[0]) # prints: 1
print(t1[2]) # prints: 3
# Negative indexing starts from the end.
print(t1[-1]) # prints: 5
t1 = (1, 2, 3, 4, 5)
print(t1[0]) # prints: 1
print(t1[2]) # prints: 3
# Negative indexing starts from the end.
print(t1[-1]) # prints: 5
1 3 5
3. Tuple Immutability¶
Unlike lists, tuples are immutable. This means you can't change, add, or remove items after the tuple is created.
In [4]:
Copied!
t1 = (1, 2, 3, 4, 5)
t1[0] = 10 # raises TypeError: 'tuple' object does not support item assignment
t1 = (1, 2, 3, 4, 5)
t1[0] = 10 # raises TypeError: 'tuple' object does not support item assignment
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[4], line 2 1 t1 = (1, 2, 3, 4, 5) ----> 2 t1[0] = 10 # raises TypeError: 'tuple' object does not support item assignment TypeError: 'tuple' object does not support item assignment
4. Tuple Slicing¶
You can extract a part of a tuple using slicing. The syntax is tuple[start:stop:step]
.
In [ ]:
Copied!
t1 = (1, 2, 3, 4, 5)
print(t1[1:4]) # prints: (2, 3, 4)
print(t1[:3]) # prints: (1, 2, 3)
print(t1[::2]) # prints: (1, 3, 5)
t1 = (1, 2, 3, 4, 5)
print(t1[1:4]) # prints: (2, 3, 4)
print(t1[:3]) # prints: (1, 2, 3)
print(t1[::2]) # prints: (1, 3, 5)
5. Tuple Methods¶
Python provides two built-in methods for tuples: count()
and index()
.
In [ ]:
Copied!
t1 = (1, 2, 2, 3, 4, 4, 4, 5)
# counts the number of occurrences of 2
print(t1.count(2)) # prints: 2
# returns the first index of 4
print(t1.index(4)) # prints: 4
t1 = (1, 2, 2, 3, 4, 4, 4, 5)
# counts the number of occurrences of 2
print(t1.count(2)) # prints: 2
# returns the first index of 4
print(t1.index(4)) # prints: 4
6. Length of a Tuple¶
You can get the number of items in a tuple with the len()
function.
In [ ]:
Copied!
t1 = (1, 2, 3, 4, 5)
print(len(t1)) # prints: 5
t1 = (1, 2, 3, 4, 5)
print(len(t1)) # prints: 5
7. Tuple Unpacking¶
Tuple unpacking allows you to assign each item in an iterable (often a tuple) to a variable.
In [ ]:
Copied!
t1 = (1, 2, 3, 4, 5)
a, b, c, d, e = t1
print(a) # prints: 1
print(b) # prints: 2
print(c) # prints: 3
t1 = (1, 2, 3, 4, 5)
a, b, c, d, e = t1
print(a) # prints: 1
print(b) # prints: 2
print(c) # prints: 3