Rocket keeps the cargo in two lists. One list has names: water, oats, tape. The other list has counts: 12, 8, 3. Raven asks, "How much tape do we have?"
Rocket finds tape at index 2 in the first list. Then he counts to index 2 in the second list. "Three! I think."
"What do you notice?" Raven asks. "You have to match up two lists every time." Nova projects a row of jars, each with a label. "Would you like a hint? What if every number wore its own name tag?"
A dictionary stores pairs. Each pair has a key and a value, with a colon between them.
Curly braces { } hold the dictionary. Commas go between the pairs.
To look up a value, put its key in square brackets.
inventory = {"water": 12, "oats": 8, "tape": 3}
print(inventory["oats"])
print(inventory) | The screen shows |
|---|
| 8 |
| {'water': 12, 'oats': 8, 'tape': 3} |
| Key | Value |
|---|---|
| "water" | 12 |
| "oats" | 8 |
| "tape" | 3 |
gates = {"Mars run": 4, "Moon hop": 2, "Ring tour": 7}
print(gates["Ring tour"]) | Statement about dictionaries | True or false? |
|---|---|
| A dictionary uses curly braces. | ? |
| A colon goes between a key and its value. | ? |
| inventory[0] gives the first value. | ? |
| Looking up a missing key gives a KeyError. | ? |
ships = {"Comet": 6, "Arrow": 4, "Drift": 9}
print(ships["Drift"])
print(ships["Comet"] + ships["Arrow"])
print(len(ships)) | Statement | True or false? |
|---|---|
| {"a": 1, "b": 2} has 2 pairs. | ? |
| Square brackets wrap a dictionary. | ? |
| A key can appear twice in one dictionary. | ? |
| ships["Arrow"] gives 4. | ? |
Great start! Tomorrow you will change the values in a dictionary.