Tuples
A tuple is like a list that cannot change. Write the items between parentheses instead of square brackets. Use a tuple for a few values that belong together and stay together, like the column and row of a lamp on the town map.
1lamp = (4, 7)2print(lamp)3print(type(lamp))4print(len(lamp))
(4, 7) <class 'tuple'> 2
Making a tuple
It is really the commas that make a tuple; the parentheses mostly help you read it. A tuple with one item needs a comma after the item, otherwise the parentheses are just grouping, as in math.
1size = 3, 52print(size)3one = ("coal",)4print(one, len(one))5empty = ()6print(empty, len(empty))7print(tuple(["a", "b"]))
(3, 5)
('coal',) 1
() 0
('a', 'b')
What does this print?
1x = (5)2print(type(x))
Without a comma, (5) is just the number 5 in parentheses, like (2 + 3). Write (5,) for a tuple with one item.
Reading a tuple
Everything that reads a list also reads a tuple: indexes, slices, len, in, min, max, sum, sorted.
1week = ("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")2print(week[0], week[-1])3print(week[5:])4print("Sun" in week)5print(week[2:4])6scores = (8, 3, 9, 3)7print(max(scores), min(scores), sum(scores))8print(sorted(scores))
Mon Sun
('Sat', 'Sun')
True
('Wed', 'Thu')
9 3 23
[3, 3, 8, 9]
sorted always gives back a list, even when you give it a tuple.
A tuple cannot change
Once a tuple is made, you cannot change, add or remove its items. There is no append and no remove, and = on an index is an error:
1lamp = (4, 7)2print(lamp[0])3lamp[0] = 5
4
Traceback (most recent call last):
File "main.py", line 3, in <module>
lamp[0] = 5
TypeError: 'tuple' object does not support item assignmentTo get a different tuple, build a new one. + joins tuples into a new tuple, and you can give the old name the new value.
1lamp = (4, 7)2lamp = (5, lamp[1])3print(lamp)4path = (1, 2) + (3,)5print(path)
(5, 7) (1, 2, 3)
Why would you want something that cannot change? Because then nothing can change it by accident. A tuple of map coordinates stays the coordinates you wrote.
Unpacking
Put several names on the left of = and Python gives each one an item from the tuple, in order. This is called unpacking. It works on lists too.
1lamp = (4, 7)2col, row = lamp3print("Column", col, "row", row)4name, age, town = ("Wick", 9, "Lampton")5print(name, "is", age)
Column 4 row 7 Wick is 9
The number of names must match the number of items. Otherwise you get a ValueError:
1col, row = (4, 7, 2)Traceback (most recent call last):
File "main.py", line 1, in <module>
col, row = (4, 7, 2)
ValueError: too many values to unpack (expected 2)You have already used unpacking. a, b = b, a swaps two variables: the right side makes the tuple (b, a), and the left side unpacks it.
What does this print?
1a = 12b = 23a, b = b, a + b4print(a, b)
Python works out the whole right side first: the tuple (2, 1 + 2), which is (2, 3). Only then does it give a and b their new values.
Several results at once
Some functions give back a tuple when they have more than one answer. divmod(a, b) gives both a // b and a % b. Unpack the result and each answer gets its own name.
1print(divmod(17, 5))2boxes, left = divmod(17, 5)3print(boxes, "full boxes,", left, "ore left over")4hours, mins = divmod(150, 60)5print(hours, "h", mins, "min")
(3, 2) 3 full boxes, 2 ore left over 2 h 30 min
In lesson 16 you will write your own functions that return several values this way.
Tuple or list?
List [ ] | Tuple ( ) | |
|---|---|---|
| Can change | yes | no |
| Methods that change it | many (append, sort, ...) | none |
| Typical use | a group of similar things that grows or shrinks | a fixed record, like a position or a date |
| Example | the names on today's route | (4, 7), one lamp's position |
Turn one into the other with list() and tuple(). A common pattern: turn a tuple into a list, change it, and turn it back.
1date = (2026, 11, 1)2parts = list(date)3parts[2] = 24date = tuple(parts)5print(date)
(2026, 11, 2)
Tip: Tuples can be dictionary keys and set items, and lists cannot. You will see why that matters in the next two lessons.
Practice
Write the code, then press Check. A hint costs nothing; the answer is there when you need it.
Unpack a lamp
✓Unpack lamp into three variables and print them as shown.
Mill Lane 3 True
1lamp = ("Mill Lane", 3, True)2# Your code here
Move the lamp
✓Tuples cannot change, so build a new tuple for lamp with the column one bigger and the row the same.
(5, 7)
1lamp = (4, 7)2lamp[0] = lamp[0] + 13print(lamp)
Minutes and seconds
✓Use divmod to turn 135 seconds into minutes and seconds, and print them with an f-string.
2 min 15 s
1seconds = 1352# Your code here
What you learned
- A tuple is an ordered group of values that cannot change:
(4, 7). One item needs a comma:(5,). - Read it like a list: indexes, slices,
len,in,max,sorted. - Changing an item is a
TypeError; build a new tuple instead. - Unpacking gives each item its own name:
col, row = lamp. The counts must match. divmodreturns a tuple; unpack it to use the parts.- Lists are for groups that change; tuples are for fixed records.