Tuples
Learn about tuples — Python's locked-in, ordered sequences — and why their immutability makes them surprisingly powerful.
Imagine you're filling out a form with your date of birth. Once you write it down, it doesn't change — ever. That's the idea behind a tuple. A tuple is an ordered collection of values, just like a list, but with one key difference: you cannot change it after you create it. That quality is called immutability, and it turns out to be really useful.
See it in action
— step through the idea, then dive into the details below.Tuples: Sealed Boxes of Data
A tuple is an ordered collection of values — like a list, but locked. Once you create it, the contents can never change.
#Creating a Tuple
You can create a tuple with parentheses (), separating values with commas. You can also skip the parentheses entirely — Python recognizes the commas and builds a tuple anyway.
# With parentheses
coordinates = (40.7128, -74.0060)
print(coordinates)
# Without parentheses — still a tuple!
rgb = 255, 128, 0
print(rgb)
print(type(rgb))The Single-Element Trap
Want a tuple with just one item? You must add a trailing comma, or Python treats it as a plain value in parentheses.
``python not_a_tuple = (42) # This is just the number 42 is_a_tuple = (42,) # This IS a tuple print(type(not_a_tuple)) # <class 'int'> print(type(is_a_tuple)) # <class 'tuple'> ``
That lonely trailing comma makes all the difference.
#Accessing Elements
Tuples are indexed exactly like lists — the first item is at position 0. You can also use negative indexes to count from the end.
person = ("Alice", 30, "engineer")
print(person[0]) # first element
print(person[-1]) # last element#Tuples Are Immutable
Once a tuple is created, you cannot add, remove, or change its values. Try to do so and Python will raise an error. This is a feature, not a bug — it means you can trust the data inside a tuple to stay exactly as you left it.
point = (3, 7)
point[0] = 10 # This will cause an error!Tuple vs List: Sealed Box vs Open Bag
Think of a list as a reusable shopping bag — you can toss items in, take them out, and rearrange freely. A tuple is a sealed box from the factory — everything inside is fixed and guaranteed. Use a sealed box when the contents should never change (like GPS coordinates, RGB colors, or a date).
#Unpacking a Tuple
Unpacking lets you assign each element of a tuple to its own variable in one clean line. This is one of the most satisfying features in Python.
color = (255, 128, 0)
r, g, b = color
print(r) # 255
print(g) # 128
print(b) # 0#Swapping Variables in One Line
Tuple unpacking enables a Python party trick: swapping two variables without a temporary helper variable. In most languages this takes three lines; in Python, it takes one.
a = "hello"
b = "world"
a, b = b, a
print(a) # world
print(b) # hello#Returning Multiple Values from a Function
A function can only return one thing — but that one thing can be a tuple containing several values. This is the standard Python pattern for returning multiple results.
def min_max(numbers):
return min(numbers), max(numbers)
scores = [72, 95, 88, 61, 100]
lowest, highest = min_max(scores)
print("Lowest:", lowest)
print("Highest:", highest)#When to Use Tuples vs Lists
Here's a simple rule of thumb:
- Use a list when the data might change (a to-do list, a shopping cart).
- Use a tuple when the data is fixed (a coordinate pair, a configuration setting, a date).
Tuples also unlock one superpower lists don't have: you can use them as dictionary keys, because they are immutable.
# Tuples as dictionary keys
location_names = {
(40.7128, -74.0060): "New York City",
(51.5074, -0.1278): "London",
}
print(location_names[(40.7128, -74.0060)])#Useful Tuple Methods
Tuples have only two built-in methods — count() and index() — because there's not much you can do to an immutable sequence.
grades = ("A", "B", "A", "C", "A")
print(grades.count("A")) # how many times does 'A' appear?
print(grades.index("C")) # at which index is 'C' ?Tuples Are Slightly Faster Than Lists
Because Python knows a tuple can never change, it can store and access tuples more efficiently than lists. For large, fixed datasets this small speed boost can add up. When in doubt, if you know the data won't change, reach for a tuple.
Which of the following correctly creates a tuple containing exactly one element — the number 5?
Key takeaways
- A tuple is an ordered, immutable sequence — once created, its contents cannot change.
- A single-element tuple requires a trailing comma: `(42,)` not `(42)`.
- Unpacking lets you assign tuple elements to variables in one line: `a, b, c = my_tuple`.
- Functions can return multiple values by returning a tuple, which you can unpack at the call site.
- Tuples can be used as dictionary keys; lists cannot — because tuples are immutable (hashable).
What does this code print?
person = ("Alice", 30, "engineer")
print(person[0])
print(person[-1])This code tries to change the first element of a tuple, but it has a bug. What is wrong?
point = (3, 7)
point[0] = 10
print(point)Complete the code so it unpacks the tuple and prints each value on its own line.
color = (255, 128, 0) r, , b = color print(r) print(g) print(b)
Put these lines in the right order to swap two variables using tuple unpacking, then print them.
print(a)
print(b)
a = "hello"
a, b = b, a
b = "world"
Write a function called summarize that takes a list of numbers and returns a tuple containing three values: the minimum, the maximum, and the average (rounded to 2 decimal places). Then unpack and print all three values with clear labels.
Try it live — edit the code and hit Run to execute real Python: