A tuple is an ordered collection of items, written with parentheses instead of a list's square brackets. At first glance tuples look like a minor variation on lists — but their one key difference, immutability, changes when and why you'd reach for one.
Creating Tuples and the Single-Element Gotcha
point = (3, 7)
colors = ("red", "green", "blue")
print(point)
print(colors[1])
# the single-element tuple trap
not_a_tuple = (5) # this is just an int in parentheses!
actual_tuple = (5,) # the trailing comma is what makes it a tuple
print(type(not_a_tuple), type(actual_tuple))
Output:
(3, 7)
green
<class 'int'> <class 'tuple'>
This trailing-comma rule trips up almost everyone at least once — Python uses the comma, not the parentheses, to decide whether something is a tuple.
Tuples Are Immutable
Once created, a tuple's contents can never be reassigned — attempting it raises a TypeError, the same way it would for a string:
point = (3, 7)
point[0] = 10 # TypeError: 'tuple' object does not support item assignment
This immutability is a feature, not a limitation — it means a tuple can be used as a dictionary key or stored in a set, neither of which a list can do, since both require their contents to be hashable.
Tuple Unpacking
Tuples make it easy to assign multiple variables in a single line — this pattern, called unpacking, shows up constantly in real Python code:
point = (3, 7)
x, y = point
print(f"x = {x}, y = {y}")
# swapping two variables using tuple unpacking, no temp variable needed
a, b = 1, 2
a, b = b, a
print(a, b)
Output:
x = 3, y = 7
2 1
That swap trick — a, b = b, a — is one of Python's most-loved idioms, and it's built entirely on tuple packing and unpacking. See it used explicitly in the Python program to swap two numbers.
When to Use a Tuple Instead of a List
Reach for a tuple when the data represents a fixed collection that shouldn't change — coordinates, RGB color values, a function returning multiple values — and reach for a list when you need to add, remove, or reorder items after creation. As a bonus, tuples are slightly faster and use a bit less memory than lists, since Python can make stronger assumptions about fixed-size, unchanging data.
