python example

Python dictionary basics

Store key-value pairs, look up a score, and loop over items in a dict.

A dict maps keys to values. Use square brackets to read or write a key, and .items() when you want both.

If you ask for a missing key, Python raises KeyError — that is a good error to practice reading in Output.

scores = {"Asha": 12, "Ben": 9}
scores["Cara"] = 15

print(scores["Asha"])
print(scores.get("Dani", 0))

for name, score in scores.items():
    print(f"{name}: {score}")