Raw data, clear context.

[
[
[

]
]
]

A departure board needs to show where a train is going and which platform to use. Keep the destination and platform together as one tuple, then give each item a name when you need to print it.

Writing ("York", 12) puts a string and an integer into the same tuple. Python calls this tuple packing.[1] Once you have a tuple, its individual items cannot be replaced.[1] A tuple can still contain a list whose contents can change.[1]

Save this as departure.py and run the command from the same folder. Tested with Python 3.14.9 on Linux.

departure.py
Python
departure = ("York", 12)
city, platform = departure
print("Destination:", city)
print("Platform:", platform)
Run departure.py
Bash
python3 departure.py
Output
Destination: York
Platform: 12
Two vertical arrows link the values in the departure tuple to variable names: 'York', the first item, goes to city, and 12, the second, goes to platform.
In the departure example, each name on the left of the assignment receives the tuple item in the same position.

Read the assignment by position

On the right of city, platform = departure is the tuple. On the left are the names that receive its items in order. This is unpacking.[2] If you write platform, city = departure instead, the labels stay the same when you print, but the destination comes out as 12 and the platform as York.

For a tuple with a single item, keep the comma: ("York",) is a tuple. With the comma missing, ("York") is a string.[3]

When the number of items changes

With the two plain names shown here, Python expects exactly two items on the right.[2] Add "09:30" as the third item in departure and leave the unpacking line alone. The program stops at that assignment before it prints anything. On the tested interpreter the last line of the traceback reads ValueError: too many values to unpack (expected 2, got 3).

Now fix the example: add time after platform on the left of the assignment. Keep the two existing print lines and add a third that prints the label Time: and the value of time. The final line should read Time: 09:30.

The next lesson uses a dictionary to look up a value by a key.

Sources

  1. Python Tutorial: Tuples and Sequences
  2. Python Language Reference: Assignment statements
  3. Python Language Reference: Parenthesized forms