Dictionaries & Data — Lesson 1: Look up information by key
New skill: Dictionary lookup and get
Previously learned: Units 01–08
Why this matters
Apps use lookup tables for labels, inventory prices, and configuration values.
Step 1 — Explore working Python
Read the program below. Before running it, write down the result you expect.
prices = {'pen': 2, 'notebook': 5}
print(prices['pen'])
print(prices.get('eraser', 0))
Step 2 — Trace the code
- Identify a key and its value in each key-value pair.
- Use square brackets when the key is known to exist.
- Use
.get(key, default)to handle a key that may be missing.
Step 3 — Check your understanding
What does {'a': 3}.get('b', 0) return, and why?
Step 4 — Watch for this mistake
A missing key causes KeyError with square brackets, but .get() can supply a default.
Practice: Look up information by key
Complete price_for(prices, item) to return the item’s numeric price, or 0 if the item is not in the dictionary.
Function contract: price_for(prices, item)
Starter file: main.py
Work in the editor, using Run to explore and Check to compare against the published examples. Your submitted code, not the public check, determines the recorded score.
Hint 1 — Start
Use `.get()` on the dictionary.Hint 2 — Build
Pass `item` as the key.Hint 3 — Test
Use zero as the fallback value.Reflect
In 3–5 sentences, explain the algorithm you chose, one test case you predicted, and how you would detect an incorrect answer.
Optional reading
For another perspective on this skill, see How to Think Like a Computer Scientist (interactive textbook). This Playground lesson, worked example, and exercise are original; no textbook passages were reproduced.