Module 03 · Lesson 02
Tuples & Sequence Unpacking
Group values that belong together with tuples, which cannot be changed, and take them apart again with unpacking, including the star form for the rest.
- About 8 minutes
- Beginner friendly
- 12-question quiz at the end
What you will learn
- Make a tuple
- Unpack in one line
- Collect with a star
- Tuple or list
Values that belong together
A tuple is a small, fixed group of values, like the two numbers of a map position.
Make one
Commas make a tuple:
point = (3, 5). The brackets just make it easy to see.Fixed
Once made, a tuple cannot change. No adding, removing or swapping items.
Unpack
x, y = pointhands each value to its own name in one line.Star
first, *rest = scoresputs the first value infirstand all the others in a list calledrest.
Fixed groups all around
Some values always travel together and should never be mixed up.
- Map pinsA place is a latitude and a longitude, always as a pair.
- ColoursScreens mix red, green and blue: orange is
(255, 165, 0). - Dates
(2026, 11, 28)keeps year, month and day in a fixed order. - Screen sizesA screen is
(1920, 1080): width first, then height.
Unpacking in one line
One line, two new names. Tap each part to see its job.
Part 1 of 4
first name:Gets the first value of the tuple, 3.
Pack and unpack
Pick an example, change it, then press Run. Say what you expect to see before you press.
Example 1 of 6Make one
Indexes and len() work just like on a list.
Your turn: Make a 3D point with x, y and z, and print only z.
point = (3, 5) print(point) print(point[0], point[1]) print(len(point))
Press Run to see what it prints.Watch values unpack
Press Step to run one line at a time. Watch new names appear from one tuple.
Ready. Press Step to run the first line.
- size = (1920, 1080)
- width, height = size
- pixels = width * height
- colour = (255, 165, 0)
- red, *rest = colour
- print(f"{pixels:,} pixels")
- print(red, rest)
Output so far
Nothing printed yet.
What Python does
Python reads the program from the top. Nothing has run yet.
Names
None yet.
Quick guesses
Guess first, then read why. A wrong guess is where the learning happens. Nothing here is scored.
Question 1 of 6
1.Slicing a tuple: what prints?
t = (1, 2, 3)
print(t[1:])0 of 6 answered
Common tuple misconceptions
Each card is something beginners often believe. Tap it to see what Python really does.
Misconception 1 of 4
Notes
Everything above, in words. Open it when you want the full explanation.
Read the notesNotesOpenClose
Syntax
The general shape of the statements in this lesson.
point = (3, 5) size = 1920, 1080 # packing
single = ("hi",) empty = () # one item needs a comma
point[0] point[-1] point[1:] len(point)
point.count(3) point.index(5)
x, y = point # unpacking
first, *rest = (1, 2, 3) # rest is [2, 3]
a, b = b, a # swapPress Run to see what it prints.Worked examples
Read the problem, run the code, then read why it works.
Example 1 of 2
Distances from a list of coordinates
Given a few map points as (x, y) tuples, print each point and how far it is from the origin along the grid (the sum of the absolute values).
points = [(3, 4), (-2, 5), (0, -6)]
for x, y in points:
distance = abs(x) + abs(y)
print(f"({x}, {y}) is {distance} blocks away")Press Run to see what it prints.Predict first
How it works(open after your guess)
Common pitfalls
Mistakes people make with this topic, and what to write instead.
Pitfall 1 of 3
Forgetting the comma in a one-item tuple
Spot the bug
Can you see what is wrong? Run it to watch it go wrong, then open the fix.
colours = ("red")
print(len(colours))Press Run to see what it prints.Show what goes wrong and the fixWhat goes wrong and the fix
In the real world
Where this shows up in software people use every day.
- Retail
Opening hours for a week
A bakery's website stores its opening hours as one tuple per day, so the times for a day always stay together and cannot be edited by accident.
Why Python fits
Each day is a fixed record of three parts, which is exactly what a tuple is for. The list holds the days because the bakery might add or remove a day later.
Pythonhours = [("Mon", 7, 15), ("Tue", 7, 15), ("Sat", 8, 13), ("Sun", 0, 0)] for day, opens, closes in hours: if opens == closes: print(f"{day}: closed") else: print(f"{day}: {opens}:00 to {closes}:00 ({closes - opens} h)")OutputPress Run to see what it prints.
Key takeaways
Tick each one you could explain to a friend.
0 of 5
Common questions
Guess the answer first, then open it.
How do I add an item to a tuple?
Can I turn a list into a tuple?
Why does a function return a tuple?
Your notes
Sign in to keep private notes alongside this lesson.
Where to go next
The quiz and the Lab live on their own pages, so this lesson stays about learning.
Finished this lesson?
Create an ID or sign in to save your progress across devices.
Check what you learned
12 questions, with an explanation after every answer. A verified attempt counts towards opening the module Lab.
Keep going in the Lab
7 tasks on this lesson, from writing code to tracing it by hand. Practice only, no XP.
Spaced review
Questions you miss in a quiz come back on a spaced schedule, so what you learned here sticks.
This lesson at a glance
The 7 lines of code this lesson uses, each with a short note. Keep it open while you do the quiz and the Lab.
What else you can search for and explore
Curious to go further? Each card opens a web search in a new tab.
References
Look things up here whenever you need the exact details.