Dictionaries: Labeled Boxes ๐
A list remembers things by position (0, 1, 2...). Sometimes that's annoying โ who wants to remember that Ana's score is at position 3?
A dictionary remembers things by label. Every entry is a pair: a key (the label) and a value (the thing).
scores = {"Ana": 12, "Bo": 9, "Cy": 15}
print(scores["Ana"]) # โ 12
Curly braces { } instead of square brackets, and a colon between each
key and its value. Look things up by key, not by number!
Adding a new entry is easy โ just make up a new key:
scores["Dev"] = 20
print(len(scores)) # โ 4
Your mission
Build a superhero database!
- Make a dictionary called
powerswith 3 heroes โ names as keys, their power as the value - Look up ONE hero's power by their key and print it
- Add a 4th hero using
powers["Name"] = "their power" - Print the whole dictionary and how many heroes are in it
Your code
Press โถ Run to see what your code does!