Explore programming tutorials, exercises, quizzes, and solutions!
Python Tuples Exercises
1/20
As you continue learning Python data structures, you come across tuples, which are similar to lists but with one important difference — immutability. Tuples are useful when you want to store a fixed collection of items that should not be modified during program execution.
What does this example help illustrate about how Python handles tuples?
Tuples in Python are immutable, meaning that once they are created, their contents cannot be changed.
In the code:
dimensions = (1920, 1080)
dimensions[0] = 1280 # ❌ This will raise an error
Attempting to change dimensions[0] results in a TypeError because you cannot assign new values to individual elements of a tuple.
If you need a collection of items that can be changed, a list should be used instead. But when data must remain constant (e.g., screen dimensions, coordinates, fixed settings), tuples are a more appropriate choice.
What will the following code output?
tpl = (10, 20, 30)
tpl[1] = 25
print(tpl)
Tuples in Python are immutable, meaning their elements cannot be changed after they are created. Trying to assign a new value to an element using tpl[1] = 25 will raise a TypeError.
What will the following code output?
tpl = (1, 2, 3)
tpl = tpl + (4, 5)
print(tpl)
Tuples in Python are immutable, but they can be concatenated. The statement tpl = tpl + (4, 5) creates a new tuple by concatenating the original tuple with the tuple (4, 5), resulting in the new tuple (1, 2, 3, 4, 5).
What will the following code output?
tpl = (1, 2, 3, 4, 5)
print(len(tpl))
The len() function returns the number of elements in a tuple. In this case, the tuple tpl has 5 elements, so len(tpl) will return 5.
What will the following code output?
tpl = (1, 2, 3, 4, 5)
print(tpl[:3])
The slice tpl[:3] retrieves the elements from the beginning of the tuple up to, but not including, index 3. Therefore, the result is the tuple (1, 2, 3).
What will the following code output?
tpl = (1, 2, 3, 4, 5)
print(tpl[-1])
In Python, negative indices count from the end of the tuple. So, tpl[-1] refers to the last element, which is 5 in this case.
What will the following code output?
tpl = (1, 2, 3)
tpl *= 2
print(tpl)
The *= operator repeats the tuple. The statement tpl *= 2 doubles the tuple, resulting in (1, 2, 3, 1, 2, 3).
Tuples in Python can be concatenated using the + operator. In this case, the original tuple is combined with another tuple (40, 50), resulting in the tuple (10, 20, 30, 40, 50).
What will the following code output?
tpl = (1, 2, 3, 4)
print(3 in tpl)
The in keyword checks if a value is present in a tuple. Since 3 is an element in the tuple tpl, the result is True.
What will the following code output?
tpl = (1, 2, 3)
tpl = tpl + (4,)
print(tpl)
To add an element to a tuple, it must be in another tuple. The statement tpl + (4,) correctly creates a new tuple (1, 2, 3, 4). If you had omitted the comma, it would not be recognized as a tuple and would raise an error.
Tuples are immutable, meaning their elements cannot be changed once defined. Therefore, attempting to assign values to a slice of the tuple (as in tpl[2:4] = (10, 20)) will result in a TypeError.
Tuples are immutable in Python, meaning elements cannot be modified after creation. Attempting to assign a new value to an index, as in tpl[2] = 6, results in a TypeError.
What will be the output of the following code?
tpl = (1, 2, 3, 4, 5)
print(len(tpl))
The len() function returns the number of elements in the tuple. Since the tuple tpl has 5 elements, the output is 5.
In this code, we are first concatenating the tuple tpl with (4, 5, 6) to get (1, 2, 3, 4, 5, 6). Then we are adding (7, 8) to the first three elements of the tuple, and appending the remaining elements. This results in (1, 2, 3, 7, 8, 4, 5, 6).
What will be the output of the following code?
tpl = (1, 2, 3, 4, 5)
tpl = tpl[::-1]
print(tpl)
The [::-1] slicing operation inverts the tuple. In this case, tpl[::-1] reverses the tuple, resulting in (5, 4, 3, 2, 1).
First, the tuple tpl is concatenated with (4,) to become (1, 2, 3, 4). Then, the tuple is repeated twice using the * 2 operation, which results in (1, 2, 3, 4, 1, 2, 3, 4).
The tuple tpl is first reversed using the slicing operation tpl[::-1], which results in (4, 3, 2, 1). Then, it is repeated twice using the * 2 operation, resulting in (4, 3, 2, 1, 4, 3, 2, 1).
The tuple tpl is first sliced to exclude the last element. Then (6,) is added, and finally, the last element is appended using tpl[-1:]. The result is (1, 2, 3, 4, 5, 6).
Practicing Python Tuples? Don’t forget to test yourself later in
our
Python Quiz.
About This Exercise: Python – Tuples
Welcome to the Python Tuples exercises — a focused and practical set of challenges aimed at helping you master tuples, one of Python’s core data structures. Tuples are an essential part of writing clean, efficient, and Pythonic code. Through this exercise collection, you'll gain a deeper understanding of how and when to use tuples effectively in real-world programming scenarios.
Unlike lists, Python tuples are immutable — meaning their contents cannot be changed once created. This immutability makes tuples useful in situations where data integrity and consistency are critical. You’ll frequently encounter tuples when returning multiple values from a function, using keys in dictionaries, or grouping fixed sets of items like coordinates or RGB values.
These exercises are designed to take you from the basics of tuple creation and indexing, all the way to more advanced use cases such as nested tuples, tuple unpacking, and combining tuples with other data types. By working through them, you'll learn how to write more robust, memory-efficient code and appreciate when tuples are a better choice than lists or other structures.
In addition to syntax and structure, the problems in this section will help you develop practical skills — like iterating over tuples, using tuples in loops and conditions, and converting between tuples and other data types. You’ll also explore common pitfalls, such as single-element tuple syntax and the implications of tuple immutability.
Whether you’re a beginner looking to solidify your Python foundation or an intermediate developer aiming to improve your code clarity and performance, these Python Tuples exercises offer a well-rounded practice experience. They're particularly helpful for academic learning, technical assessments, and coding interviews where understanding data structures is key.
As you move forward, we also recommend exploring related Python topics such as lists, dictionaries, sets, and functions. These concepts are deeply interconnected and mastering them together will give you a broader command over Python programming. Pair this practice with our Python quizzes for additional reinforcement and insight.
Start practicing Python Tuples now to sharpen your data structure skills and enhance your ability to write fast, safe, and Pythonic code. Each exercise brings you one step closer to mastering core Python concepts with clarity and confidence.