In Python, a tuple is an ordered, immutable collection defined within parentheses. Tuples are unique to lists, but they cannot be modified after they are created. Tuples are used to store related data together. Here’s a summary of tuples in Python
Creating Tuples
You can create a tuple by enclosing comma-separated values in parentheses ()
my_tuple = (1, 2, 3)
You can create tuples without parentheses, which is known as tuple packing
my_tuple = 1, 2, 3
Accessing Tuple Elements
You can access individual elements of a tuple using indexing. The index for the first element starts at 0.
my_tuple = (1, 2, 3)
print(my_tuple[0]) # Output: 1
Tuple elements can also be accessed using negative indexing, where -1 signifies the last element
my_tuple = (1, 2, 3)
print(my_tuple[-1]) # Output: 3
Tuple methods
Because tuples are immutable, only a limited number of methods are available. Some common tuple methods include:
- count(): Returns the number of the specified element in the tuple.
- index(): Returns the index in the tuple corresponding to the first find of the specified element.
my_tuple = (1, 2, 2, 3, 4, 2)
print(my_tuple.count(2)) # Output: 3
print(my_tuple.index(3)) # Output: 3
Tuple Packing and Unpacking
Tuple packing refers to forming a tuple by assigning multiple values to a single tuple variable on the corresponding single tuple
my_tuple = 1, 2, 3
Tuple unpacking allows individual tuple elements to be assigned to partitioner variables:
my_tuple = 1, 2, 3
a, b, c = my_tuple
print(a, b, c) # Output: 1 2 3
Comparing Tuples
Tuples can be compared using comparison operators such as <, >, <=, >=, ==, and !=.
The comparison is done element-wise
tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
print(tuple1 < tuple2) # Output: True
Tuples are typically used for functions that return multiple values, for data verity not to modify elements, and where immutability and ordering are needed.
Important note: The elements of a tuple cannot be modified, but if an element is itself modifiable (for example, a list), then the mutable object can be modified.
Example Code
# Creating a tuple
my_tuple = (1, 'apple', True, 3.14)
print(my_tuple) # Output: (1, 'apple', True, 3.14)
# Accessing tuple items
print(my_tuple[0]) # Output: 1
print(my_tuple[1]) # Output: 'apple'
print(my_tuple[2]) # Output: True
# Modifying a tuple (not directly possible as tuples are immutable)
# Uncomment the following line to see the TypeError
# my_tuple[0] = 2
# Tuple length
print(len(my_tuple)) # Output: 4
# Concatenating tuples
new_tuple = my_tuple + ('orange', 42)
print(new_tuple) # Output: (1, 'apple', True, 3.14, 'orange', 42)
# Tuple packing and unpacking
a, b, c, d, e, f = new_tuple
print(a, b, c, d, e, f) # Output: 1 'apple' True 3.14 'orange' 42
# Slicing a tuple
print(new_tuple[1:4]) # Output: ('apple', True, 3.14)
print(new_tuple[:3]) # Output: (1, 'apple', True)
print(new_tuple[4:]) # Output: ('orange', 42)
# Counting occurrences of an item in a tuple
count = new_tuple.count('apple')
print(count) # Output: 1
# Finding the index of an item in a tuple
index = new_tuple.index(True)
print(index) # Output: 2
# Immutable nature of tuples
nested_tuple = ([1, 2, 3], 'hello')
nested_tuple[0].append(4)
print(nested_tuple) # Output: ([1, 2, 3, 4], 'hello')
# Iterating over a tuple
for item in new_tuple:
print(item)
Output
(1, 'apple', True, 3.14) 1 'apple' True 4 (1, 'apple', True, 3.14, 'orange', 42) 1 'apple' True 3.14 'orange' 42 ('apple', True, 3.14) (1, 'apple', True) ('orange', 42) 1 2 ([1, 2, 3, 4], 'hello') 1 'apple' True 3.14 'orange' 42