Lesson 2 of 20 · 50 min · UG / professional
Generators: yield and generator expressions
A function that yields the next row is a generator. A generator expression is a comprehension that does not build the whole list. Practise the prefix loop; copy the yield sample onto a laptop.
Weather station
Filter a table of readings.
You will be able to
- ✓ See yield as pause-and-hand-back, not return-a-whole-list
- ✓ Practise a prefix loop here; copy the yield sample onto a laptop
- ✓ Know a generator expression is a filter that does not build the whole list
A function that remembers where it stopped
The electronics lab wants heat alerts from a night of DHT-04 readings — not a second copy of every Celsius value. On a laptop a generator yields the next alert and pauses. The caller can stop after one beep. return would dump the whole tray at once.
This sandbox cannot yield. You still write the policy: a flag, print the keepers, leave the rest unvisited. That is the same conversation as yield, minus the syntax.
Generator expressions are laptop spelling
A generator expression looks like a comprehension but does not build the whole list. Useful when you will take a prefix. Useless when you will sort everything anyway — then make a real list.
Copy the sample below onto a campus laptop inside a venv. Do not paste yield into this Run box; the teaching interpreter will not honour it.
Laptop only — yield and a generator expression
def heat_alerts(temps, limit):
for t in temps:
if t >= limit:
yield t
temps = [28, 31, 34, 29, 36]
# take a prefix without building every alert first
for c in heat_alerts(temps, 32):
print("beep", c)
break
warm = (t for t in temps if t >= 32)
print("next warm", next(warm))
Words that matter
- yield
- Pause the function and hand one value back — laptop.
- Generator
- A function that yields over time instead of returning one list.
- Generator expression
- A comprehension-shaped stream that stays lazy — laptop.
Common mistakes
Avoid: Building a full alerts list when the kiosk will stop after the first beep.
Do this: Yield on a laptop (or a prefix flag here) so callers can stop early.
Run it step by step
Each box is a real program. Press Run, change a number, Run again — the output must follow your code.
1. Step 1 — produce on demand with a flag
Same policy as yield: stop asking once you have enough.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
2. Step 2 — a prefix of attendance rolls
The office printer wanted two rolls, not the whole hall.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Example program — One alert, then stop visiting
found is the analog of closing the generator.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Your turn — Prefix of three
temps = [28, 31, 29, 34]. Print the first two values, then print Stopped.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Self-assessment
Check your understanding before you mark the lesson complete.
Progress is stored in a browser cookie on this device.