Build a dict
A dictionary, or dict, stores pairs. Each key points to one value, like a word and its meaning. Line 1 builds ages from two pairs, written key: value inside curly braces.
Line 2 adds a pair. Python first hashes the key: hash("Cy") turns the text into a whole number. That number tells Python where the pair belongs, so it never has to search.
There is no "Cy" in ages yet, so = adds the pair. ages now holds three pairs, and it keeps them in the order their keys were first added.
Keys are unique. "Ben" is already a key, so line 3 does not add a second "Ben": it replaces his value, 25, with 26. The pair keeps its place.
Look up keys
ages["Ana"] looks up a value by its key. Python hashes "Ana", goes straight to its pair and reads 31, which print() shows. This stays fast however many pairs the dict holds.
There is no "Zed". Asking ages["Zed"] would stop the program with a KeyError. get() is the safe way: it finds no pair and gives back the default you passed, 0.
Sets
A set is a collection of values with no keys and no order, and each value appears once. Line 6 builds pets: Python hashes "cat", adds it, then does the same for "dog".
The second "cat" hashes to the same number and lands on the same spot. pets already has "cat", so nothing is added. The set still holds two members.
len(pets) counts the members: 2. "dog" in pets asks whether "dog" is a member, and Python answers True or False. Like a dict lookup, it jumps straight to the spot.
Try it yourself
;; step 1/9 · line 1: ages = {"Ana": 31, "Ben": 25}
>>> ages = {"Ana": 31, "Ben": 25}
>>> ages
{'Ana': 31, 'Ben': 25}
>>> len(ages) # two pairs
2
>>> type(ages)
<class 'dict'>
Dicts keep their keys in the order they were first added (guaranteed since Python 3.7); changing a value keeps the key in its place. Sets keep no order, so a printed set may list its members in any order. Keys and set members must be hashable: numbers, strings and tuples are; lists, dicts and sets are not, because they can change. {[1, 2]} fails with a TypeError that says unhashable type: 'list'.