Python – Tuple Exercises
Here are some Python Tuple Exercises to help you practice and get comfortable with working with tuples:
1. Create a Tuple
Create a tuple with the following elements: 10, 20, 30, 40, 50. Then, print the tuple.
Solution:
# Create a tuple
my_tuple = (10, 20, 30, 40, 50)
# Print the tuple
print(my_tuple)
2. Access Tuple Elements
Given a tuple my_tuple = (10, 20, 30, 40, 50), print the second element and the last element of the tuple.
Solution:
# Given tuple
my_tuple = (10, 20, 30, 40, 50)
# Access second and last element
print("Second element:", my_tuple[1])
print("Last element:", my_tuple[-1])
3. Find the Length of a Tuple
Given a tuple my_tuple = (1, 2, 3, 4, 5), find and print the length of the tuple.
Solution:
# Given tuple
my_tuple = (1, 2, 3, 4, 5)
# Find and print the length of the tuple
print("Length of tuple:", len(my_tuple))
4. Count Occurrences of an Element
Given a tuple my_tuple = (10, 20, 30, 10, 10, 40, 50), count how many times the number 10 appears in the tuple.
Solution:
# Given tuple
my_tuple = (10, 20, 30, 10, 10, 40, 50)
# Count occurrences of 10
count_10 = my_tuple.count(10)
print("Occurrences of 10:", count_10)
5. Find the Index of an Element
Given a tuple my_tuple = (5, 10, 15, 20, 25), find the index of the element 15 in the tuple.
Solution:
# Given tuple
my_tuple = (5, 10, 15, 20, 25)
# Find index of 15
index_of_15 = my_tuple.index(15)
print("Index of 15:", index_of_15)
6. Concatenate Two Tuples
Create two tuples, tuple1 = (1, 2, 3) and tuple2 = (4, 5, 6), and concatenate them into a new tuple. Print the new tuple.
Solution:
# Given tuples
tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
# Concatenate the tuples
new_tuple = tuple1 + tuple2
print("Concatenated Tuple:", new_tuple)
7. Repeat a Tuple
Create a tuple my_tuple = (1, 2, 3) and repeat it 3 times. Print the new tuple.
Solution:
# Given tuple
my_tuple = (1, 2, 3)
# Repeat the tuple 3 times
repeated_tuple = my_tuple * 3
print("Repeated Tuple:", repeated_tuple)
8. Convert a List to a Tuple
Given a list my_list = [10, 20, 30], convert it into a tuple and print the result.
Solution:
# Given list
my_list = [10, 20, 30]
# Convert list to tuple
my_tuple = tuple(my_list)
print("Tuple:", my_tuple)
9. Unpack a Tuple
Given a tuple my_tuple = (1, 2, 3), unpack it into three variables a, b, and c. Print each variable.
Solution:
# Given tuple
my_tuple = (1, 2, 3)
# Unpack the tuple into variables
a, b, c = my_tuple
# Print the variables
print("a:", a)
print("b:", b)
print("c:", c)
10. Check if an Element Exists in a Tuple
Given a tuple my_tuple = (1, 2, 3, 4, 5), check if the number 3 exists in the tuple. Print True if it exists, otherwise print False.
Solution:
# Given tuple
my_tuple = (1, 2, 3, 4, 5)
# Check if 3 exists in the tuple
exists = 3 in my_tuple
print(exists) # Output: True
11. Nested Tuple Access
Given a nested tuple my_tuple = (1, (2, 3), 4, (5, 6)), access and print the element 3 from the nested tuple.
Solution:
# Given nested tuple
my_tuple = (1, (2, 3), 4, (5, 6))
# Access element 3 from the nested tuple
print("Element 3:", my_tuple[1][1])
12. Reverse a Tuple
Given a tuple my_tuple = (10, 20, 30, 40, 50), reverse the tuple and print it.
Solution:
# Given tuple
my_tuple = (10, 20, 30, 40, 50)
# Reverse the tuple
reversed_tuple = my_tuple[::-1]
print("Reversed Tuple:", reversed_tuple)
13. Join Two Tuples Using sum()
Given a list of tuples tuple_list = [(1, 2), (3, 4), (5, 6)], join them into a single tuple using the sum() function and print the result.
Solution:
# List of tuples
tuple_list = [(1, 2), (3, 4), (5, 6)]
# Join the tuples using sum()
joined_tuple = sum(tuple_list, ())
print("Joined Tuple:", joined_tuple)