</>
Topic 6 of 10
Theory

Dictionaries and sets

A dictionary maps keys to values:

ages = {"Anna": 12, "Tim": 14}
ages["Leia"] = 11              # add
print(ages["Anna"])            # 12
print(ages.get("Max", 0))      # 0 — if the key is missing
for name, age in ages.items():
    print(name, age)

A set stores only unique values and can find what two sets share:

a = {1, 2, 3}
b = {2, 3, 4}
print(a & b)   # {2, 3} — intersection
print(a | b)   # {1, 2, 3, 4} — union

Phone book

Easy+10 XP

Return the person's number from the book dictionary. If the name is missing, return the string no number.

phone({"Anna": "123"}, "Anna") → "123"
main.pyLoading Python…
Output
Press "Check" to run the tests