Module 07 β Tuples
An ordered collection that, once created, can never be changed.
Prerequisite: Module 06 β Lists
After this lesson, you will be able to
- Create and index tuples using parentheses
- Explain why tuples are immutable and when that matters
- Use tuples to return more than one value from a function
Concept & Syntax
A tuple looks and behaves almost exactly like a list β it's ordered, and you can index and slice it the same way β with one crucial difference: once created, a tuple cannot be changed. No append, no item assignment, no remove.
That might sound like a downgrade, but immutability is a feature: it signals "this data shouldn't change," lets Python optimize storage slightly, and β most usefully β is exactly what functions use under the hood whenever they return more than one value.
point = (3, 4)
x, y = point # unpacking
point[0] # 3Examples
Creating and indexing a tuple Very Easy
point = (3, 4)
print(point[0], point[1])Tuples use the same indexing rules as lists β parentheses instead of square brackets is the only visual difference.
Tuples are immutable Easy
colors = ("red", "green")
try:
colors[0] = "blue"
except TypeError as e:
print("Can't do that:", e)Attempting to change an item raises TypeError β this is what 'immutable' means in practice.
Returning multiple values Real World
def min_max(numbers):
return min(numbers), max(numbers)
lowest, highest = min_max([4, 9, 1, 7])
print(lowest, highest)return a, b actually returns a single tuple (a, b), which you can immediately unpack into two variables.
Visual Explanation
π‘ Why does this work?
Read the concept explanation above again slowly, line by line β every keyword there maps directly onto a line of code in the examples. If it still doesn't click, re-run the traced example (if this lesson has one) one step at a time.
π What happens internally? (advanced)
Under the hood, Python compiles your source into bytecode and runs it on a virtual machine (the CPython interpreter). Variables are names in a namespace dictionary pointing at objects in memory β which is exactly what the memory panel in the tracer above is showing you.
Common Mistakes
A single-item tuple needs a trailing comma β without it, Python just sees ordinary parentheses.
single = (5)
print(type(single))single = (5,)
print(type(single))- Use a tuple for data that represents a fixed structure, like (x, y) coordinates or (r, g, b) colors.
- Use tuple unpacking (
a, b = my_tuple) instead of manual indexing where possible. - Reach for a list instead if you expect to add, remove, or reorder items.
Exercise & Challenge
Exercise 1 β Predict the output
dimensions = (1920, 1080)
width, height = dimensions
print(f"{width}x{height}")What does this print?
π Challenge
Write a function circle_stats(radius) that returns a tuple of (circumference, area), then unpack and print both, rounded to 2 decimal places.
- Use pi = 3.14159
- Return both values as a tuple
- Unpack the result into two named variables
π‘ Need a hint?
circumference = 2 * pi * radius, area = pi * radius ** 2
π Show a sample solution
def circle_stats(radius):
pi = 3.14159
return 2 * pi * radius, pi * radius ** 2
circ, area = circle_stats(5)
print(round(circ, 2), round(area, 2))Quiz
Answer every question, then submit to see your score and explanations.
1. What is the main difference between a list and a tuple?
2. How do you write a tuple with a single item, 5?
3. What happens if a function does `return a, b`?
Summary
- Tuples are ordered like lists but cannot be modified after creation.
- A single-item tuple needs a trailing comma: (5,).
- Functions commonly use tuples to return multiple values at once.