Kodewick
Lesson 10 of 31Contents

Tuples

Lesson 10 of 31 · Part 2 · Collections and loops · about 15 minutes

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.

main.py
1lamp = (4, 7)2print(lamp)3print(type(lamp))4print(len(lamp))
Output
(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.

main.py
1size = 3, 52print(size)3one = ("coal",)4print(one, len(one))5empty = ()6print(empty, len(empty))7print(tuple(["a", "b"]))
Output
(3, 5)
('coal',) 1
() 0
('a', 'b')
Guess first

What does this print?

1x = (5)2print(type(x))

Reading a tuple

Everything that reads a list also reads a tuple: indexes, slices, len, in, min, max, sum, sorted.

main.py
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))
Output
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:

main.py
1lamp = (4, 7)2print(lamp[0])3lamp[0] = 5
Output
4
Traceback (most recent call last):
  File "main.py", line 3, in <module>
    lamp[0] = 5
TypeError: 'tuple' object does not support item assignment

To get a different tuple, build a new one. + joins tuples into a new tuple, and you can give the old name the new value.

main.py
1lamp = (4, 7)2lamp = (5, lamp[1])3print(lamp)4path = (1, 2) + (3,)5print(path)
Output
(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.

main.py
1lamp = (4, 7)2col, row = lamp3print("Column", col, "row", row)4name, age, town = ("Wick", 9, "Lampton")5print(name, "is", age)
Output
Column 4 row 7
Wick is 9

The number of names must match the number of items. Otherwise you get a ValueError:

main.py
1col, row = (4, 7, 2)
Output
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.

Guess first

What does this print?

1a = 12b = 23a, b = b, a + b4print(a, b)

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.

main.py
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")
Output
(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 changeyesno
Methods that change itmany (append, sort, ...)none
Typical usea group of similar things that grows or shrinksa fixed record, like a position or a date
Examplethe 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.

main.py
1date = (2026, 11, 1)2parts = list(date)3parts[2] = 24date = tuple(parts)5print(date)
Output
(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.

Exercise 1

Unpack a lamp

Unpack lamp into three variables and print them as shown.

It should print
Mill Lane 3 True
exercise1.py
1lamp = ("Mill Lane", 3, True)2# Your code here
Output
Press Run or Ctrl+Enter
Exercise 2

Move the lamp

Tuples cannot change, so build a new tuple for lamp with the column one bigger and the row the same.

It should print
(5, 7)
exercise2.py
1lamp = (4, 7)2lamp[0] = lamp[0] + 13print(lamp)
Output
Press Run or Ctrl+Enter
Exercise 3

Minutes and seconds

Use divmod to turn 135 seconds into minutes and seconds, and print them with an f-string.

It should print
2 min 15 s
exercise3.py
1seconds = 1352# Your code here
Output
Press Run or Ctrl+Enter

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.
  • divmod returns a tuple; unpack it to use the parts.
  • Lists are for groups that change; tuples are for fixed records.