Editor

Fix the Bug: Modifying a Tuple

Beginner · Fix the bug · from Python Tuples: When and Why to Use Immutable Sequences

From the lesson: Python Tuples: When and Why to Use Immutable Sequences

Think about GPS coordinates. A location like (40.7128, -74.0060) represents New York City. If you accidentally changed one of those numbers, the location would point somewhere completely different. Some data simply should not change after you create it.

Read the lesson

This code tries to change a color in a tuple, but tuples are immutable. Fix it so the program prints the updated colors without changing the original tuple. Create a new tuple instead.

Expected output:

('red', 'yellow', 'blue')
colors = ('red', 'green', 'blue')
colors[1] = 'yellow'
print(colors)

More Tuples exercises

All Python exercises · 50 practice problems · Data Structures quiz