Generators

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
Lesson 1 is on the house. The rest opens with the paper, the Daily, or a gift.

This is Python · expert, lesson 1: Generators. Read the teaching and the stills before the checks. The quizzes are open book and test the subject — Python itself — not product rules or layout trivia.

yield turns a function into a generator.

list(count(3)) materializes [0,1,2].

Generators save memory; they are single-pass.

for-loops handle StopIteration.

gen

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

print(list(count(3)))
Match this when you type. Open book.

Sit with that still. Trace it top to bottom. Name each piece. The exercise asks you to match its essence.

If something fails when you try it yourself, change one thing. Read the error. Return to the still. I will not rush you.

Quiz

What does yield do?

Quiz

What is list(count(3))?

Quiz

Are generators multi-pass by default?

Quiz

Based on today's teaching, which claim is right?

Quiz

Based on today's teaching, which claim is right?

Quiz

Based on today's teaching, which claim is right?

Check

Match the still for: Generators.

That is the lesson. When every quiz and exercise on this page is green, mark it passed. Next lesson when you are ready.

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

Generators · Python Expert (free) — The Gold Standard