Lesson 008: List and dict comprehensions
Focus
Assume a reviewer executes this verbatim: Intermediate drills List and dict comprehensions; spin token 76258 makes this page unlike its neighbours.
Key ideas
- Angle
Intermediate: micro cadence8mixesList and dict comprehensions; spin31319. - Ritual: Describe falsifying evidence that would veto this pattern.
- Guardrail: name the surprise that triggered when literals shifted.
Example (LESSON_UID = "intermediate-008")
# Intermediate drill L008 topic-0 micro-7 pattern-10
LESSON_UID = "intermediate-008"
spin_a, spin_b, spin_c = 180, 873, 15
def gate(v):
if v < spin_a + 7:
return "low", v ** 2
if v > spin_b + 8:
return "high", v // max(1, 7 + 1)
return "mid", v + spin_c
cand = [21, 8, 266]
for candidate in cand:
lbl, val = gate(candidate)
print(candidate, lbl, val)
from pathlib import Path
import tempfile
with tempfile.TemporaryDirectory() as scratch:
target = Path(scratch) / "scratch-8.txt"
snap = [21, 212, 403, 594, 785, 976]
target.write_text("\n".join(str(x) for x in snap), encoding="utf-8")
print("scratch_bytes", target.stat().st_size, "rolling", sum(snap) % (15 + 131))
Practice
Practice 29: Promote hottest literal into named constant; justify naming aloud. Literal nudge 29.
Fingerprints
- lesson_uid:
intermediate-008 - umbrella band:
List and dict comprehensions(8/10) - lesson_index:
274