A dictionary in Python stores key-value pairs. Each key maps to a value, like a real dictionary where each word (key) maps to a definition (value).
scores = {"Alice": 95, "Bob": 87, "Charlie": 72}
Dictionaries use curly braces {}. Keys and values are separated by colons :, and pairs are separated by commas.
person = {"name": "Alice", "age": 25, "city": "NYC"}
print(person["name"]) # "Alice"
print(len(person)) # 3
You can add new key-value pairs after creation:
scores = {}
scores["Alice"] = 95
scores["Bob"] = 87
Assignment
Follow the Coding Tutorial and create a dictionary scores with three student scores!
Hint
Use scores = {"Alice": 95, "Bob": 87, "Charlie": 72}.
Free plan: 30 tutor messages. PRO is unlimited.
Saved
Ctrl + Enter to check
