A tuple is an immutable object in Python that cannot be changed. Tuples are also sequences, just like Python lists. This Python Tuple exercise aims to help you learn and practice tuple operations.
Also Read:
This Python tuple exercise contains 19 coding questions, each with a provided solution.
- Practice and solve various tuple operations, manipulations, and tuple functions.
- All questions are tested on Python 3.
- Read the complete guide on Python Tuples to solve this exercise.
When you complete each question, you get more familiar with Python tuple. Let us know if you have any alternative solutions. It will help other developers.
Use Online Code Editor to solve exercise questions.
Table of contents
- Exercise 1: Perform Basic Tuple Operations
- Exercise 2: Tuple Repetition
- Exercise 3: Slicing Tuples
- Exercise 4: Reverse the tuple
- Exercise 5: Access Nested Tuples
- Exercise 6: Create a tuple with single item 50
- Exercise 7: Unpack the tuple into 4 variables
- Exercise 8: Swap two tuples in Python
- Exercise 9: Copy Specific Elements From Tuple
- Exercise 10: List to Tuple
- Exercise 11: Function Returning Tuple
- Exercise 12: Comparing Tuples
- Exercise 13: Removing Duplicates from Tuple
- Exercise 14: Filter Tuples
- Exercise 15: Map Tuples
- Exercise 16: Modify Tuple
- Exercise 17: Sort a tuple of tuples by 2nd item
- Exercise 18: Count Elements
- Exercise 19: Check if all items in the tuple are the same
Exercise 1: Perform Basic Tuple Operations
- Create a Tuple: Create a tuple named
my_tuple
containing the numbers 1, 2, 3, 4, and 5. - Access Elements: Access and print the third element of
my_tuple
. - Tuple Length: Find and print the length of
my_tuple
.
Expected Output:
My tuple: (1, 2, 3, 4, 5)
The third element of my_tuple: 3
The length of my_tuple: 5
+ Hint
- Create Tuples using parentheses
()
. - Use indexing to access elements, meaning the first element is at index 0, the second at index 1, and so on.
- Use the built-in function
len()
to find the length of tuple.
+ Show Solution
Exercise 2: Tuple Repetition
Repeat a below tuple three times.
Given:
original_tuple = ('a', 'b')
Code language: Python (python)
Expected Output:
('a', 'b', 'a', 'b', 'a', 'b')
+ Hint
Use the *
operator to repeat a tuple.
+ Show Solution
Exercise 3: Slicing Tuples
Slice below tuple to get elements from the 4th to the 7th position.
Given:
numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
Code language: Python (python)
Expected Output:
(4, 5, 6, 7)
+ Hint
Tuple slicing allows you to extract a portion of a tuple. The syntax [start:end]
creates a new tuple containing elements from the start
index up to, but not including, the end
index.
+ Show Solution
Since we want the 4th to 7th positions, we use index 3 as the start (0-based) and index 7 as the end (because it’s exclusive, it will stop before the 8th element, effectively giving us the 4th, 5th, 6th, and 7th elements).
Exercise 4: Reverse the tuple
Given:
tuple1 = (10, 20, 30, 40, 50)
Code language: Python (python)
Expected output:
(50, 40, 30, 20, 10)
Show Hint
Use tuple slicing to reverse the given tuple. Note: the last element starts at -1.
Show Solution
Exercise 5: Access Nested Tuples
Write a code to access and print value 20 from given nested tuple.
Given:
tuple1 = ("Orange", [10, 20, 30], (5, 15, 25))
Code language: Python (python)
Expected output:
20
Show Hint
Use double indexing. First, find the index of the inner tuple, then use a second index to find the element within that inner tuple.
Show Solution
Exercise 6: Create a tuple with single item 50
Show Solution
Exercise 7: Unpack the tuple into 4 variables
Write a code to unpack the following tuple into four variables and display each variable.
Given:
tuple1 = (10, 20, 30, 40)
Code language: Python (python)
Expected output:
tuple1 = (10, 20, 30, 40)
# Your code
print(a) # should print 10
print(b) # should print 20
print(c) # should print 30
print(d) # should print 40
+ Hint
You can assign multiple variables to a tuple directly, provided the number of variables matches the number of elements in the tuple.
Show Solution
Exercise 8: Swap two tuples in Python
Given:
tuple1 = (11, 22)
tuple2 = (99, 88)
Code language: Python (python)
Expected output:
tuple1: (99, 88) tuple2: (11, 22)
Show Solution
Exercise 9: Copy Specific Elements From Tuple
Write a program to copy elements 44 and 55 from the following tuple into a new tuple.
Given:
tuple1 = (11, 22, 33, 44, 55, 66)
Code language: Python (python)
Expected output:
tuple2: (44, 55)
Show Solution
Exercise 10: List to Tuple
Convert a list my_list = [10, 20, 30]
into a tuple.
Expected Output:
(10, 20, 30)
+ Show Solution
Exercise 11: Function Returning Tuple
Write a function get_min_max(numbers)
that takes a list of numbers and returns a tuple containing the minimum and maximum number.
Given:
def get_min_max(numbers):
# Write your code here
# Test the function
my_numbers = [10, 5, 20, 2, 15]
min_max_values = get_min_max(my_numbers)
print(f"Original numbers: {my_numbers}")
print(f"Minimum and maximum values: {min_max_values}")
Code language: Python (python)
Expected Output:
Original numbers: [10, 5, 20, 2, 15]
Minimum and maximum values: (2, 20)
+ Hint
- Use the built-in
min()
andmax()
functions that can be directly applied to a list. - Functions can return multiple values by separating them with commas, which implicitly creates a tuple.
+ Show Solution
Exercise 12: Comparing Tuples
Compare two tuples and find out which one is “greater” and why?
Given:
t1 = (1, 2, 3)
t2 = (1, 2, 4)
Code language: Python (python)
Expected Output:
Tuple 1: (1, 2, 3, 8)
Tuple 2: (1, 2, 4, 5)
(1, 2, 3, 8) is less than (1, 2, 4, 5)
+ Hint
Python compares tuples lexicographically (element by element) from left to right. The first pair of elements that differ determines the outcome.
+ Show Solution
Explanation:
Python compares tuples element by element.
- It compares
t1[0]
(which is 1) witht2[0]
(which is 1). They are equal. - It then moves to
t1[1]
(which is 2) andt2[1]
(which is 2). They are also equal. - Finally, it compares
t1[2]
(which is 3) witht2[2]
(which is 4). Since3 < 4
, Python determines thatt1
is less thant2
Exercise 13: Removing Duplicates from Tuple
Write a code to create a new tuple with only unique elements.
Given:
my_tuple = (1, 2, 2, 3, 4, 4, 5)
Code language: Python (python)
Expected Output:
Original tuple with duplicates: (1, 2, 2, 3, 4, 4, 5)
Tuple with unique elements: (1, 2, 3, 4, 5)
+ Hint
Sets in Python inherently store only unique elements. You can convert the tuple to a set to remove duplicates, and then convert it back to a tuple. Note that sets do not preserve order.
+ Show Solution
Solution 2: If preserving original order of first appearance is important:
Exercise 14: Filter Tuples
Write a code to filter out students with scores less than 90 from a given a list of tuples.
Given:
students = [('Alice', 85), ('Bob', 92), ('Charlie', 78)]
Code language: Python (python)
Expected Output:
Students with scores 90 or above: [('Bob', 92), ('David', 95)]
+ Hint
Use a loop with a conditional statement, or a list comprehension with a condition. Access the score (the second element) of each student tuple.
+ Show Solution
Solution 1: Using loop
Solution 2: Using list comprehension
Exercise 15: Map Tuples
Given a tuple of numbers, create a new tuple where each number is squared.
Given:
t = (1, 2, 3, 4)
Code language: Python (python)
Expected Output:
Original tuple: (1, 2, 3, 4)
Squared tuple: (1, 4, 9, 16)
+ Hint
Use a loop, a list comprehension (and then convert to tuple), or the map()
function with a lambda expression or a regular function. The map()
function is often used for applying a function to each item in an iterable.
+ Show Solution
Solution 1: Using map() and tuple()
Solution 2: Using a loop
Exercise 16: Modify Tuple
Given is a nested tuple. Write a program to modify the first item (22) of a list inside a following tuple to 222
Given:
tuple1 = (11, [22, 33], 44, 55)
Code language: Python (python)
Expected output:
tuple1: (11, [222, 33], 44, 55)
Show Hint
The given tuple is a nested tuple. Use indexing to locate the specified item and modify it using the assignment operator.
Show Solution
Exercise 17: Sort a tuple of tuples by 2nd item
Given:
tuple1 = (('a', 23),('b', 37),('c', 11), ('d',29))
Code language: Python (python)
Expected output:
(
Sorted tuple by 2nd item: (('c', 11), ('a', 23), ('d', 29), ('b', 37))
+ Hint
Tuples are immutable, so you cannot sort them directly. You’ll need to convert the tuple to a list first, sort the list, and then optionally convert it back to a tuple.
Show Solution
Exercise 18: Count Elements
Write a code to counts the number of occurrences of item 50 from a tuple.
Given:
tuple1 = (50, 10, 60, 70, 50)
Code language: Python (python)
Expected output:
2
Show Hint
Use the count()
method of a tuple.
Show Solution
Exercise 19: Check if all items in the tuple are the same
tuple1 = (45, 45, 45, 45)
Code language: Python (python)
Expected output:
True