8.3 Tuples
8.3a What are Python tuples?
A tuple is a built-in data structure in Python that is used to store an ordered collection of items. Unlike lists, tuples are immutable, meaning that once a tuple is created, its elements cannot be changed, added, or removed.
8.3b Why do we use tuples?
We use tuples for several reasons:
- Immutability - Since tuples cannot be modified after creation, they can be used to ensure that data remains constant throughout the program.
- Performance - Tuples are generally faster than lists because of their immutability.
- Integrity - They can be used as keys in dictionaries, while lists cannot.
- Readability - Tuples can represent a fixed collection of related items, enhancing code clarity and readability.
8.3c How do we use tuples?
Here are some examples demonstrating the use of tuples and the functions that can be used with them:
Example 8.3.1 - Creating a tuple.
Example 8.3.1 - Output
(1, 2, 3, 4, 5)
Example 8.3.2 - Accessing elements.
Example 8.3.2 - Output
1 (2, 3)
Example 8.3.3 - count(item)
- Returns the number of times an item appears in the tuple.
Example 8.3.3 - Output
3
Example 8.3.4 - len()
- Returns the number of items in the tuple.
Example 8.3.4 - Output
7
8.3d Note on functions not applicable to tuples
In contrast to lists, the following functions are not applicable to tuples because of their immutability:
append(item)
- Tuples do not support item addition after creation.clear()
- Tuples do not support clearing all elements.insert(index, item)
- Tuples do not support item insertion.pop([index])
- Tuples do not support removing items by index.remove(item)
- Tuples do not support removing items by value.reverse()
- Tuples do not support reversing their order in place.sort()
- Tuples do not support sorting their elements in place.
8.3e Looping through a tuple in Python
Looping through a tuple in Python is similar to looping through a list. You can use several methods to iterate over the elements of a tuple:
1. Using a for
loop
Example 8.3.5 - The for
loop is the most straightforward way to iterate over each item in a list.
# Define a tuple
my_tuple = (1, 2, 3, 4, 5)
# Loop through each element in the tuple
for item in my_tuple:
print(item)
1 2 3 4 5
2. Using enumerate()
Example 8.3.6 - If you need to access both the index and the value of each item in the tuple, you can use the enumerate()
function.
# Define a tuple
my_tuple = ('apple', 'banana', 'cherry')
# Loop through the tuple with index
for index, item in enumerate(my_tuple):
print(f"Index: {index}, Item: {item}")
Example 8.3.6 - Output
Index: 0, Item: apple Index: 1, Item: banana Index: 2, Item: cherry
3. Using a while
Loop
Example 8.3.7 - You can also use a while
loop to iterate over a list. This is less common but useful in some scenarios.
# Define a tuple
my_tuple = (10, 20, 30, 40, 50)
# Initialize the index
index = 0
# Loop through the tuple using a while loop
while index < len(my_tuple):
print(my_tuple[index])
index += 1
Example 8.3.7 - Output
10 20 30 40 50
Looping through a tuple in Python is simple and can be done using various methods such as a for
loop, enumerate()
, or a while
loop. Each method has its own use case and can be chosen based on the specific requirements of your program.
Note
Python tuples are immutable, ordered collections of items. They are used when a collection of items needs to remain constant and unchanged throughout the program. Key functions that can be used with tuples include count(item)
and len()
.
8.3f Develop a program using lists as data structures
Exercise 8.3.1 - Given the following tuple of rainbow colours:
rainbow_colours = ('red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet')
Perform the following tasks:
- Loop through the tuple and print each colour.
- Print the length of the tuple.
- Count how many times the colour 'blue' appears in the tuple.
- Use a loop with
enumerate()
to print the index and colour. - Create a new tuple that contains only the colours from the third to the sixth position (inclusive) of the original tuple, and print the new tuple.
Write the complete program and show the output for each step.
Exercise 8.3.1 - Model Answer - Make sure to work out the exercise before checking the model answer.