Dictionaries: Values by Key
Scenario: A service configuration needs named fields rather than mysterious positions.
New words, in plain English
- Dictionary - a mutable mapping from unique keys to values
- Key - the label used to retrieve a value
- Value - the data stored under a key
Dictionaries use {key: value}. Read with brackets when a key is required and get when it may be absent. Add by assignment; remove with pop or del.
Analogy: A Python program is like a clear set of instructions for a careful helper: names label things, indentation groups steps, and errors explain where the helper became confused.
A small, real example
service = {"name": "api", "port": 8080}
service["healthy"] = True
print(service.get("owner", "unknown"))
Tip: Dictionary keys must be hashable, so strings, numbers, and tuples are common keys.
Goal: Practice this idea in the py-dict-basics challenge at/labs/python. Fix the broken program, run it yourself, then usecheckto prove the real end state.