Tuples in Python are immutable sequences, typically used to store collections of heterogeneous data. This article presents 20 exercises that will help you strengthen your understanding of tuples and practice various operations with them. At the end of the article, you’ll find the answers to all exercises.
Exercises
Create a tuple with the values
5, 10, 15, 20, 25
and print the tuple.t = (5, 10, 15, 20, 25) print(t) # Output: (5, 10, 15, 20, 25)
Given the tuple
t = ("1984", "The Hobbit", "Moby Dick")
, access the second item.t = ("1984", "The Hobbit", "Moby Dick") print(t[1]) # Output: The Hobbit
Use negative indexing to access the last item of the tuple
t = ("fiction", "non-fiction", "fantasy")
.t = ("fiction", "non-fiction", "fantasy") print(t[-1]) # Output: fantasy
Unpack the tuple
t = (100, 200, 300)
into three variables and print them.t = (100, 200, 300) a, b, c = t print(a, b, c) # Output: 100 200 300
Create a tuple with a single item,
"Python"
, and print the tuple.t = ("Python",) print(t) # Output: ('Python',)
Use slicing to obtain the first three items from the tuple
t = ("book1", "book2", "book3", "book4", "book5")
.t = ("book1", "book2", "book3", "book4", "book5") print(t[:3]) # Output: ('book1', 'book2', 'book3')
Check if the item
"Dune"
is in the tuplet = ("1984", "Brave New World", "Fahrenheit 451")
.t = ("1984", "Brave New World", "Fahrenheit 451") print("Dune" in t) # Output: False
Combine two tuples,
t1 = (2, 4, 6)
andt2 = (8, 10, 12)
, into a single tuple.t1 = (2, 4, 6) t2 = (8, 10, 12) t3 = t1 + t2 print(t3) # Output: (2, 4, 6, 8, 10, 12)
Find the length of the tuple
t = ("a", "b", "c", "d")
.t = ("a", "b", "c", "d") print(len(t)) # Output: 4
Multiply the tuple
t = ("book",)
by 3 and print the result.t = ("book",) print(t * 3) # Output: ('book', 'book', 'book')
Convert the tuple
t = (40, 50, 60)
into a list.t = (40, 50, 60) l = list(t) print(l) # Output: [40, 50, 60]
Use the
count()
method to find how many times"The Hobbit"
appears in the tuplet = ("The Hobbit", "1984", "The Hobbit", "Dune")
.t = ("The Hobbit", "1984", "The Hobbit", "Dune") print(t.count("The Hobbit")) # Output: 2
Find the index of
"non-fiction"
in the tuplet = ("fiction", "non-fiction", "fantasy")
.t = ("fiction", "non-fiction", "fantasy") print(t.index("non-fiction")) # Output: 1
Convert the list
l = [5, 10, 15, 20]
into a tuple.l = [5, 10, 15, 20] t = tuple(l) print(t) # Output: (5, 10, 15, 20)
Create a tuple
t = ("red", "green", "blue")
and iterate over its elements using afor
loop.t = ("red", "green", "blue") for color in t: print(color) # Output: # red # green # blue
Python doesn’t support tuple comprehensions, but you can achieve this using a generator and converting it to a tuple:
t = tuple(x**2 for x in range(2, 7)) print(t) # Output: (4, 9, 16, 25, 36)
Create a nested tuple with the following structure:
((2, 4, 6), ("x", "y", "z"))
and access the second item in the first tuple.t = ((2, 4, 6), ("x", "y", "z")) print(t[0][1]) # Output: 4
Write a Python function that accepts a tuple and returns a new tuple with all the elements squared.
def square_tuple(t): return tuple(x**2 for x in t) print(square_tuple((2, 3, 4))) # Output: (4, 9, 16)
Given a tuple
t = (200, 300, 400, 500, 600)
, remove the first item and return the remaining tuple.t = (200, 300, 400, 500, 600) t = t[1:] print(t) # Output: (300, 400, 500, 600)
Write a Python script that concatenates all the elements of a tuple into a single string.
t = ("Hello", "from", "Python", "with", "love") result = " ".join(t) print(result) # Output: Hello from Python with love
Answers
t = (5, 10, 15, 20, 25)
print(t) # Output: (5, 10, 15, 20, 25)t = ("1984", "The Hobbit", "Moby Dick")
print(t[1]) # Output: The Hobbitt = ("fiction", "non-fiction", "fantasy")
print(t[-1]) # Output: fantasyt = (100, 200, 300)
a, b, c = t
print(a, b, c) # Output: 100 200 300t = ("Python",)
print(t) # Output: ('Python',)t = ("book1", "book2", "book3", "book4", "book5")
print(t[:3]) # Output: ('book1', 'book2', 'book3')t = ("1984", "Brave New World", "Fahrenheit 451")
print("Dune" in t) # Output: Falset1 = (2, 4, 6)
t2 = (8, 10, 12)
t3 = t1 + t2
print(t3) # Output: (2, 4, 6, 8, 10, 12)t = ("a", "b", "c", "d")
print(len(t)) # Output: 4t = ("book",)
print(t * 3) # Output: ('book', 'book', 'book')t = (40, 50, 60)
l = list(t)
print(l) # Output: [40, 50, 60]t = ("The Hobbit", "1984", "The Hobbit", "Dune")
print(t.count("The Hobbit")) # Output: 2t = ("fiction", "non-fiction", "fantasy")
print(t.index("non-fiction")) # Output: 1l = [5, 10, 15, 20]
t = tuple(l)
print(t) # Output: (5, 10, 15, 20)t = ("red", "green", "blue")
for color in t:
print(color) # Output:
red
green
bluet = tuple(x**2 for x in range(2, 7))
print(t) # Output: (4, 9, 16, 25, 36)t = ((2, 4, 6), ("x", "y", "z"))
print(t[0][1]) # Output: 4def square_tuple(t):
return tuple(x**2 for x in t)print(square_tuple((2, 3, 4))) # Output: (4, 9, 16)
t = (200, 300, 400, 500, 600)
t = t[1:]
print(t) # Output: (300, 400, 500, 600)t = ("Hello", "from", "Python", "with", "love")
result = " ".join(t)
print(result) # Output: Hello from Python with love
Conclusion
Practicing these exercises will improve your understanding of tuples in Python, which are an essential part of the language. You’ll become more comfortable working with tuples and applying tuple operations to solve problems effectively.