Tuple Exercises
Test your understanding of Python tuples with these exercises:
Exercise 1: Create a Tuple
Create a tuple of your three favorite colors and print it.
Python
# Your code here
colors = ("red", "blue", "green")
print(colors)
Exercise 2: Access a Tuple Item
Access the second item in the tuple colors
and print it.
Python
# Your code here
print(colors[1]) # Output should be "blue"
Exercise 3: Unpack a Tuple
Unpack the tuple colors
into three variables and print them.
Python
# Your code here
(color1, color2, color3) = colors
print(color1) # Output: red
print(color2) # Output: blue
print(color3) # Output: green
Exercise 4: Join Two Tuples
Join the tuple colors
with another tuple containing the colors "yellow"
and "purple"
, then print the new tuple.
Python
# Your code here
more_colors = ("yellow", "purple")
all_colors = colors + more_colors
print(all_colors)
Exercise 5: Use Tuple Methods
Count how many times the color "blue"
appears in the tuple all_colors
.
Python
# Your code here
blue_count = all_colors.count("blue")
print(blue_count) # Output should be 1
Import Links
Here are some useful import links for further reading: