A departure board needs a platform number for each destination. If the train to York changes platforms, the board needs to update that entry. A Python dictionary stores pairs of keys and values and lets you look up a value by its key.[1]
Write the pairs inside braces, with a colon between each key and its value and commas between pairs.[1] Here the destination is the key and the platform number is the value. Save the example as platforms.py and run the command from the same folder. The Output block shows what the script printed with Python 3.14.9 on Linux.
platforms = {"York": 12, "Leeds": 4}print("York:", platforms["York"])platforms["York"] = 14print("York after change:", platforms["York"])print("Bath:", platforms.get("Bath", "Check the board"))
python3 platforms.py
York: 12York after change: 14Bath: Check the board

The square brackets in platforms["York"] tell Python to look up the key "York".[2] The first lookup prints 12. The assignment puts 14 at that same key, so the next lookup prints 14. Keys are unique within a dictionary: assigning to an existing key replaces its value.[1] Here, each destination has one platform at a time. Use separate keys if you need to distinguish two trains to the same city.
When a destination is missing
Try replacing platforms.get("Bath", "Check the board") on the last line with platforms["Bath"]. The first two lines still print. On the tested interpreter the last line of the traceback reads KeyError: 'Bath'. A bracket lookup on an ordinary dictionary raises KeyError when the key is missing.[2] The get method takes the key inside parentheses. With two arguments, it returns the stored value if the key exists and uses the second argument as the fallback if the key is missing.[2] Use brackets when a missing key should stop the lookup with an error. Use a fallback when you have something useful to display in its place.
Restore the original last line, then add a new entry for Durham on platform 6 using square brackets and an assignment. Add a print line with the label Durham: and a bracket lookup. The new last line should read Durham: 6.
The next lesson uses a set to keep a collection of distinct items.