Python Expert · Lesson 1 · On the house

Generators

yield — one value at a time. Lesson 1 is free — on the house.

Lessons · Python Expert · Lesson 1 of 10 · On the house

Generators

yield — one value at a time.

Illustration for the Python course
yield — one value at a time.

Expert assumes Introductory through Medium. Now: generators, decorators, contextlib, typing, asyncio taste, slots, enter, pytest-style asserts, logging, and argparse.

A generator function uses yield instead of return to produce a sequence lazily. def count(n) with a while that yields i one at a time gives an iterator. list(count(3)) materializes [0, 1, 2] so you can see every value while learning the shape. Zero-based indexing feels weird for a day, then it becomes muscle memory. When something looks "one off," count on your fingers from zero before you blame Python. Name the idea once, reuse it cleanly. If you find yourself copy-pasting the same three lines, that is a function (or a generator) knocking politely. Before you run anything, predict what Generators should do on the still's inputs. Prediction turns running into confirmation.

yield pauses the function, hands one value to the caller, and resumes on the next iteration. Local state survives between yields, which is why generators can model streams without building giant lists first. Zero-based indexing feels weird for a day, then it becomes muscle memory. When something looks "one off," count on your fingers from zero before you blame Python. Name the idea once, reuse it cleanly. If you find yourself copy-pasting the same three lines, that is a function (or a generator) knocking politely. Before you run anything, predict what Generators should do on the still's inputs. Prediction turns running into confirmation.

return inside a generator ends iteration. yield from delegates to another iterable. Master plain yield in a loop before stacking fancy forms so your mental model stays honest and debuggable. Name the idea once, reuse it cleanly. If you find yourself copy-pasting the same three lines, that is a function (or a generator) knocking politely. Trace one full iteration on paper with a tiny example. If you can't simulate a single pass, a thousand passes won't magically clarify it. Before you run anything, predict what Generators should do on the still's inputs. Prediction turns running into confirmation.

Generator expressions like (n*n for n in range(4)) are lazy cousins of list comprehensions. They shine when you iterate once. Call list(...) when you must reuse results more than once. Zero-based indexing feels weird for a day, then it becomes muscle memory. When something looks "one off," count on your fingers from zero before you blame Python. Trace one full iteration on paper with a tiny example. If you can't simulate a single pass, a thousand passes won't magically clarify it. Before you run anything, predict what Generators should do on the still's inputs. Prediction turns running into confirmation.

Reach for generators on large or incremental sequences and pipelines. Prefer real lists when you need random access or multiple passes over the same data without recomputing. Zero-based indexing feels weird for a day, then it becomes muscle memory. When something looks "one off," count on your fingers from zero before you blame Python. Trace one full iteration on paper with a tiny example. If you can't simulate a single pass, a thousand passes won't magically clarify it. Before you run anything, predict what Generators should do on the still's inputs. Prediction turns running into confirmation.

While learning, exhaust small generators into lists so values are visible. Remove list() later when laziness is the actual goal of the design rather than a teaching crutch. Zero-based indexing feels weird for a day, then it becomes muscle memory. When something looks "one off," count on your fingers from zero before you blame Python. Trace one full iteration on paper with a tiny example. If you can't simulate a single pass, a thousand passes won't magically clarify it. Before you run anything, predict what Generators should do on the still's inputs. Prediction turns running into confirmation.

Put Generators in a sentence you could teach a friend: what changes in the program, what stays the same, and what output you expect from the still. Say that back in your own words before you scroll — if you can't, the still for Generators will feel like typing karaoke. Before you run anything, predict what Generators should do on the still's inputs. Prediction turns running into confirmation.

If you are skimming, stop and re-read the still's lines in order while pointing at each token. Generators is not a vocabulary flashcard — it is a behavior you should be able to simulate on paper.

When an exercise asks you to match the still, match it honestly: same names, same structure, same output idea. Clever shortcuts that skip the concept will leave a hole that the next lesson falls into.

Open-book does not mean guess. The right answer is on this page in plain language. Wrong choices are usually near-misses that sound technical; pick the claim that matches what we actually traced.

gen

def count(n):
    i = 0
    while i < n:
        yield i
        i += 1

print(list(count(3)))
yield produces values lazily; list exhausts.

count(3) returns a generator object, not a list. Each step yields 0, then 1, then 2. list(...) pulls those values into [0, 1, 2] for printing. The while plus yield is the heartbeat of the still.

Pitfalls: expecting def to return a list immediately; forgetting to iterate; reusing an exhausted generator; hiding heavy side effects between yields. Trace one yield at a time on paper first.

Quiz

What does yield do?

Quiz

list(count(3)) is?

Quiz

Why use generators?

Quiz

After exhaustion, iterating again?

Quiz

Generator expression vs list comp?

Check

Match the generator still.

Lazy sequences unlocked. Next: decorators — wrap a function without rewriting it.

Open-book: the answers are on this page. Pass every quiz and check (6) to mark the lesson done. This visit: 0/6.

Generators · Python Expert (free) — The Gold Standard