Good place to stop if you're short on time — step 3 picks up here.
Lesson idea Tap to fold
How do you safely read an optional dictionary field?
Show the answer
Use record.get("field", default).
Follow nested keys and distinguish missing values
A nested dictionary groups related facts. record["evaluation"]["score"] first selects evaluation and then reads its score. A direct lookup raises KeyError if a required key is absent.
get(key, default) uses the default only when that key is absent. It does not replace a present empty string, zero, False or None. This lesson assumes the parent dictionary exists; get is not a substitute for validating an untrusted parent type.
repr(value) returns a debugging representation. For text it shows surrounding quotes, so the worked example makes an empty string visible instead of printing an unexplained blank line.
# Follow nested keys and distinguish missing values.
record = {
"evaluation": {"score": 5, "notes": ""},
}
evaluation = record["evaluation"]
print(evaluation["score"])
print(repr(evaluation.get("notes", "no notes")))
print(evaluation.get("reviewer", "not assigned"))
5, then the repr of an empty string (two single quotes), then not assigned. Missing and present-empty are different states.
Explain each lookup boundary. Next, turn explicit required-field and range rules into boolean helpers.
Predict, explain, then test
These three brief checks are self-reviewed, not a scored quiz. Answer before opening the comparison. Coding checks follow the stated task assumptions on unfamiliar inputs.
Trace keys
Which parent must exist before record["evaluation"].get("notes", "no notes") works?
Compare your answer · self-reviewed
evaluation must be present and a dictionary; this expression only handles missing notes.
Predict empty
What does {"notes": ""}.get("notes", "no notes") return?
Compare your answer · self-reviewed
The empty string, because the key is present.
Predict None
Does a present notes value of None trigger the default?
Compare your answer · self-reviewed
No. get returns None; only an absent key triggers the default.
Choose an exercise to load its prompt.
Loading exercises…
Noted on your route map, with the step you were on. Nothing is lost by parking it — the next review day will bring this idea back, and the flag tells the course where to slow down.
The tutor mounts here when JavaScript is available. The lesson above stays readable without it.