Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 04

Python Generators and yield — Values On Demand

Learn Python generators with YouTube autoplay: yield pauses, return finishes, next() resumes, kvrange patterns, and StopIteration when empty.

Intermediate30 min

generators yield YouTube autoplay hero banner: yield "PYTHON" yield "HTML" yield "C" yield "C++"

g = getcourses() print(next(g)) # PYTHON print(next(g)) # HTML

code

Every `next(g)` asks for **one more value**.

## What you will learn

- A **generator** is a function that contains **`yield`**
- **`return`** finishes the function; **`yield`** pauses and can continue
- Generator objects: pull with `next()` or `for`; extra `next()` → **`StopIteration`**
- Four `yield`s vs four `return`s — generator vs normal function
- **`kvrange`**: three ways to drain a generator

## `return` vs `yield`

![return ends at PYTHON yield gives each course checkpoint](data:image/svg+xml;charset=utf-8,%3Csvg%20font-family%3D%27ui-sans-serif%2C-apple-system%2Csystem-ui%2CArial%27%20width%3D%27100%25%27%20xmlns%3D%27http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%27%20viewBox%3D%270%200%20320%20140%27%3E%20%3Crect%20width%3D%27320%27%20height%3D%27140%27%20rx%3D%2714%27%20fill%3D%27%23F8FAFC%27%2F%3E%20%3Crect%20x%3D%2714%27%20y%3D%2718%27%20width%3D%27136%27%20height%3D%27104%27%20rx%3D%2710%27%20fill%3D%27%23FEE2E2%27%2F%3E%20%3Ctext%20x%3D%2782%27%20y%3D%2734%27%20font-size%3D%2710%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%23991B1B%27%3Ereturn%3C%2Ftext%3E%20%3Crect%20x%3D%2730%27%20y%3D%2746%27%20width%3D%27104%27%20height%3D%2724%27%20rx%3D%276%27%20fill%3D%27%23FCA5A5%27%2F%3E%20%3Ctext%20x%3D%2782%27%20y%3D%2762%27%20font-size%3D%278%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%237F1D1D%27%3EPYTHON%3C%2Ftext%3E%20%3Ctext%20x%3D%2782%27%20y%3D%2790%27%20font-size%3D%277%27%20text-anchor%3D%27middle%27%20fill%3D%27%237F1D1D%27%3EFunction%20ends%3C%2Ftext%3E%20%3Crect%20x%3D%27170%27%20y%3D%2718%27%20width%3D%27136%27%20height%3D%27104%27%20rx%3D%2710%27%20fill%3D%27%23D1FAE5%27%2F%3E%20%3Ctext%20x%3D%27238%27%20y%3D%2734%27%20font-size%3D%2710%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%23047857%27%3Eyield%3C%2Ftext%3E%20%3Crect%20x%3D%27186%27%20y%3D%2744%27%20width%3D%2788%27%20height%3D%2714%27%20rx%3D%274%27%20fill%3D%27%23A7F3D0%27%2F%3E%20%3Ctext%20x%3D%27230%27%20y%3D%2754%27%20font-size%3D%277%27%20text-anchor%3D%27middle%27%20fill%3D%27%23065F46%27%3EPYTHON%3C%2Ftext%3E%20%3Crect%20x%3D%27186%27%20y%3D%2762%27%20width%3D%2788%27%20height%3D%2714%27%20rx%3D%274%27%20fill%3D%27%23A7F3D0%27%2F%3E%20%3Ctext%20x%3D%27230%27%20y%3D%2772%27%20font-size%3D%277%27%20text-anchor%3D%27middle%27%20fill%3D%27%23065F46%27%3EHTML%3C%2Ftext%3E%20%3Crect%20x%3D%27186%27%20y%3D%2780%27%20width%3D%2788%27%20height%3D%2714%27%20rx%3D%274%27%20fill%3D%27%23A7F3D0%27%2F%3E%20%3Ctext%20x%3D%27230%27%20y%3D%2790%27%20font-size%3D%277%27%20text-anchor%3D%27middle%27%20fill%3D%27%23065F46%27%3EC%3C%2Ftext%3E%20%3Crect%20x%3D%27186%27%20y%3D%2798%27%20width%3D%2788%27%20height%3D%2714%27%20rx%3D%274%27%20fill%3D%27%23A7F3D0%27%2F%3E%20%3Ctext%20x%3D%27230%27%20y%3D%27108%27%20font-size%3D%277%27%20text-anchor%3D%27middle%27%20fill%3D%27%23065F46%27%3EC%2B%2B%3C%2Ftext%3E%20%3C%2Fsvg%3E

| Feature | `return` | `yield` |
|---------|----------|---------|
| Ends function | Yes | No |
| Continues later | No | Yes |
| Multiple values | No | Yes |
| Memory efficient | No | Yes |

**Golden rule:** `return` exits forever. `yield` pauses and remembers its place.

### Several returns (not a generator)

```python
def getcourses():
    return "PYTHON"
    return "HTML"
    return "C"
    return "C++"

nitcrs = getcourses()
print(nitcrs, type(nitcrs))  # PYTHON <class 'str'>

Only the first return runs. Later lines are dead code.

How yield pauses

Think of climbing stairs. Every landing is a yield point.

![yield pause points PYTHON HTML C C++ like stair landings](data:image/svg+xml;charset=utf-8,%3Csvg%20font-family%3D%27ui-sans-serif%2C-apple-system%2Csystem-ui%2CArial%27%20width%3D%27100%25%27%20xmlns%3D%27http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%27%20viewBox%3D%270%200%20320%20140%27%3E%20%3Crect%20width%3D%27320%27%20height%3D%27140%27%20rx%3D%2714%27%20fill%3D%27%23ECFDF5%27%2F%3E%20%3Crect%20x%3D%2720%27%20y%3D%2790%27%20width%3D%2754%27%20height%3D%2728%27%20rx%3D%276%27%20fill%3D%27%2334D399%27%2F%3E%20%3Ctext%20x%3D%2747%27%20y%3D%27107%27%20font-size%3D%277%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%23064E3B%27%3EPYTHON%3C%2Ftext%3E%20%3Cpath%20d%3D%27M74%20104%20L90%2088%27%20stroke%3D%27%23047857%27%20stroke-dasharray%3D%272%202%27%2F%3E%20%3Crect%20x%3D%2790%27%20y%3D%2772%27%20width%3D%2754%27%20height%3D%2728%27%20rx%3D%276%27%20fill%3D%27%2334D399%27%2F%3E%20%3Ctext%20x%3D%27117%27%20y%3D%2789%27%20font-size%3D%277%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%23064E3B%27%3EHTML%3C%2Ftext%3E%20%3Cpath%20d%3D%27M144%2086%20L160%2070%27%20stroke%3D%27%23047857%27%20stroke-dasharray%3D%272%202%27%2F%3E%20%3Crect%20x%3D%27160%27%20y%3D%2754%27%20width%3D%2754%27%20height%3D%2728%27%20rx%3D%276%27%20fill%3D%27%2334D399%27%2F%3E%20%3Ctext%20x%3D%27187%27%20y%3D%2771%27%20font-size%3D%277%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%23064E3B%27%3EC%3C%2Ftext%3E%20%3Cpath%20d%3D%27M214%2068%20L230%2052%27%20stroke%3D%27%23047857%27%20stroke-dasharray%3D%272%202%27%2F%3E%20%3Crect%20x%3D%27230%27%20y%3D%2736%27%20width%3D%2754%27%20height%3D%2728%27%20rx%3D%276%27%20fill%3D%27%2334D399%27%2F%3E%20%3Ctext%20x%3D%27257%27%20y%3D%2753%27%20font-size%3D%277%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%23064E3B%27%3EC%2B%2B%3C%2Ftext%3E%20%3Ctext%20x%3D%27160%27%20y%3D%27128%27%20font-size%3D%278%27%20text-anchor%3D%27middle%27%20fill%3D%27%23047857%27%3EEach%20landing%20is%20a%20yield%20point%3C%2Ftext%3E%20%3C%2Fsvg%3E

python
def getcourses():
    yield "PYTHON"   # pause here
    yield "HTML"     # pause again
    yield "C"
    yield "C++"

The function does not restart. It resumes exactly after the previous yield.

Several yields

python
def getcourses():
    yield "PYTHON"
    yield "HTML"
    yield "C"
    yield "C++"

nitcrs = getcourses()
print("Type of nitcrs=", type(nitcrs))  # <class 'generator'>
print(next(nitcrs))
print(next(nitcrs))
print(next(nitcrs))
print(next(nitcrs))
# next(nitcrs)  # StopIteration

Run the full script in the playground below.

What does a generator return?

A generator function does not return a string or list. It returns a generator object.

![getcourses function to generator object to next value](data:image/svg+xml;charset=utf-8,%3Csvg%20font-family%3D%27ui-sans-serif%2C-apple-system%2Csystem-ui%2CArial%27%20width%3D%27100%25%27%20xmlns%3D%27http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%27%20viewBox%3D%270%200%20320%20110%27%3E%20%3Crect%20width%3D%27320%27%20height%3D%27110%27%20rx%3D%2714%27%20fill%3D%27%23F5F3FF%27%2F%3E%20%3Crect%20x%3D%2714%27%20y%3D%2734%27%20width%3D%2786%27%20height%3D%2742%27%20rx%3D%278%27%20fill%3D%27%23DDD6FE%27%20stroke%3D%27%237C3AED%27%2F%3E%20%3Ctext%20x%3D%2757%27%20y%3D%2748%27%20font-size%3D%278%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%235B21B6%27%3Egetcourses%28%29%3C%2Ftext%3E%20%3Ctext%20x%3D%2757%27%20y%3D%2758%27%20font-size%3D%277%27%20text-anchor%3D%27middle%27%20fill%3D%27%236D28D9%27%3Efunction%3C%2Ftext%3E%20%3Cpath%20d%3D%27M100%2055%20H124%27%20stroke%3D%27%237C3AED%27%20stroke-dasharray%3D%273%203%27%2F%3E%20%3Cpolygon%20points%3D%27120%2C51%20128%2C55%20120%2C59%27%20fill%3D%27%237C3AED%27%2F%3E%20%3Crect%20x%3D%27128%27%20y%3D%2726%27%20width%3D%2776%27%20height%3D%2758%27%20rx%3D%2710%27%20fill%3D%27%23C4B5FD%27%20stroke%3D%27%236D28D9%27%2F%3E%20%3Ctext%20x%3D%27166%27%20y%3D%2742%27%20font-size%3D%278%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%234C1D95%27%3Egenerator%3C%2Ftext%3E%20%3Ctext%20x%3D%27166%27%20y%3D%2752%27%20font-size%3D%277%27%20text-anchor%3D%27middle%27%20fill%3D%27%235B21B6%27%3Eobject%3C%2Ftext%3E%20%3Cpath%20d%3D%27M204%2055%20H228%27%20stroke%3D%27%237C3AED%27%20stroke-dasharray%3D%273%203%27%2F%3E%20%3Cpolygon%20points%3D%27224%2C51%20232%2C55%20224%2C59%27%20fill%3D%27%237C3AED%27%2F%3E%20%3Crect%20x%3D%27232%27%20y%3D%2734%27%20width%3D%2774%27%20height%3D%2742%27%20rx%3D%278%27%20fill%3D%27%23A7F3D0%27%20stroke%3D%27%23059669%27%2F%3E%20%3Ctext%20x%3D%27269%27%20y%3D%2748%27%20font-size%3D%278%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%23047857%27%3Enext%28%29%3C%2Ftext%3E%20%3Ctext%20x%3D%27269%27%20y%3D%2758%27%20font-size%3D%277%27%20text-anchor%3D%27middle%27%20fill%3D%27%23065F46%27%3Evalue%3C%2Ftext%3E%20%3Ctext%20x%3D%27160%27%20y%3D%2798%27%20font-size%3D%277%27%20text-anchor%3D%27middle%27%20fill%3D%27%236D28D9%27%3EWaits%20quietly%20until%20next%28%29%20is%20called%3C%2Ftext%3E%20%3C%2Fsvg%3E

python
g = getcourses()
print(type(g))
# <class 'generator'>

The object waits quietly until someone calls next(g).

Three ways to use a generator

The kvrange() example shows three common patterns.

python
def kvrange(val):
    i = 0
    while i < val:
        yield i
        i += 1

Method 1 — next() manually

kvrange next manual 0 1 2 print(next(r)) print(next(r)) print(next(r))

code

### Method 2 — `while` + `StopIteration`

![while True try next except StopIteration loop](data:image/svg+xml;charset=utf-8,%3Csvg%20font-family%3D%27ui-sans-serif%2C-apple-system%2Csystem-ui%2CArial%27%20width%3D%27100%25%27%20xmlns%3D%27http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%27%20viewBox%3D%270%200%20320%2056%27%3E%20%3Crect%20width%3D%27320%27%20height%3D%2756%27%20rx%3D%2710%27%20fill%3D%27%23FEFCE8%27%2F%3E%20%3Crect%20x%3D%27108%27%20y%3D%2714%27%20width%3D%27104%27%20height%3D%2728%27%20rx%3D%276%27%20fill%3D%27%23FDE68A%27%20stroke%3D%27%23D97706%27%2F%3E%20%3Ctext%20x%3D%27160%27%20y%3D%2732%27%20font-size%3D%278%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%2392400E%27%3Enext%28r%29%3C%2Ftext%3E%20%3Cpath%20d%3D%27M92%2028%20C70%2010%2C%2044%2010%2C%2044%2028%20C44%2046%2C%2070%2046%2C%2092%2028%27%20stroke%3D%27%23A16207%27%20fill%3D%27none%27%20stroke-dasharray%3D%273%203%27%2F%3E%20%3Cpolygon%20points%3D%2788%2C22%2096%2C28%2088%2C34%27%20fill%3D%27%23A16207%27%2F%3E%20%3C%2Fsvg%3E

```python
while True:
    try:
        print(next(r))
    except StopIteration:
        break

for value in kvrange recommended pattern: print(value)

code

## Why generators save memory

Imagine a warehouse with one million boxes. You do not move everything home — you request **one box at a time**.

![warehouse millions of boxes vs generator one box at a time](data:image/svg+xml;charset=utf-8,%3Csvg%20font-family%3D%27ui-sans-serif%2C-apple-system%2Csystem-ui%2CArial%27%20width%3D%27100%25%27%20xmlns%3D%27http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%27%20viewBox%3D%270%200%20320%20120%27%3E%20%3Crect%20width%3D%27320%27%20height%3D%27120%27%20rx%3D%2714%27%20fill%3D%27%23F0FDFA%27%2F%3E%20%3Crect%20x%3D%2716%27%20y%3D%2718%27%20width%3D%2716%27%20height%3D%2716%27%20rx%3D%272%27%20fill%3D%27%2399F6E4%27%20stroke%3D%27%230F766E%27%2F%3E%20%3Crect%20x%3D%2738%27%20y%3D%2718%27%20width%3D%2716%27%20height%3D%2716%27%20rx%3D%272%27%20fill%3D%27%2399F6E4%27%20stroke%3D%27%230F766E%27%2F%3E%20%3Crect%20x%3D%2760%27%20y%3D%2718%27%20width%3D%2716%27%20height%3D%2716%27%20rx%3D%272%27%20fill%3D%27%2399F6E4%27%20stroke%3D%27%230F766E%27%2F%3E%20%3Crect%20x%3D%2782%27%20y%3D%2718%27%20width%3D%2716%27%20height%3D%2716%27%20rx%3D%272%27%20fill%3D%27%2399F6E4%27%20stroke%3D%27%230F766E%27%2F%3E%20%3Crect%20x%3D%2716%27%20y%3D%2740%27%20width%3D%2716%27%20height%3D%2716%27%20rx%3D%272%27%20fill%3D%27%2399F6E4%27%20stroke%3D%27%230F766E%27%2F%3E%20%3Crect%20x%3D%2738%27%20y%3D%2740%27%20width%3D%2716%27%20height%3D%2716%27%20rx%3D%272%27%20fill%3D%27%2399F6E4%27%20stroke%3D%27%230F766E%27%2F%3E%20%3Crect%20x%3D%2760%27%20y%3D%2740%27%20width%3D%2716%27%20height%3D%2716%27%20rx%3D%272%27%20fill%3D%27%2399F6E4%27%20stroke%3D%27%230F766E%27%2F%3E%20%3Crect%20x%3D%2782%27%20y%3D%2740%27%20width%3D%2716%27%20height%3D%2716%27%20rx%3D%272%27%20fill%3D%27%2399F6E4%27%20stroke%3D%27%230F766E%27%2F%3E%20%3Ctext%20x%3D%2760%27%20y%3D%2776%27%20font-size%3D%278%27%20text-anchor%3D%27middle%27%20fill%3D%27%23115E59%27%3EMillions%20of%20boxes%3C%2Ftext%3E%20%3Cpath%20d%3D%27M112%2056%20H182%27%20stroke%3D%27%230F766E%27%20stroke-dasharray%3D%274%204%27%2F%3E%20%3Cpolygon%20points%3D%27176%2C52%20184%2C56%20176%2C60%27%20fill%3D%27%230F766E%27%2F%3E%20%3Ccircle%20cx%3D%27238%27%20cy%3D%2750%27%20r%3D%2714%27%20fill%3D%27%2314B8A6%27%2F%3E%20%3Crect%20x%3D%27228%27%20y%3D%2762%27%20width%3D%2720%27%20height%3D%2716%27%20rx%3D%272%27%20fill%3D%27%230F766E%27%2F%3E%20%3Ctext%20x%3D%27238%27%20y%3D%2796%27%20font-size%3D%278%27%20text-anchor%3D%27middle%27%20fill%3D%27%23115E59%27%3EOne%20box%20only%3C%2Ftext%3E%20%3C%2Fsvg%3E

**Normal function** — builds everything at once:

```python
def numbers():
    return [0, 1, 2, 3, 4]

Generator — creates values only when needed:

python
def numbers():
    yield 0
    yield 1
    yield 2

Generators are ideal for large files and streaming data.

What happens after the last value?

Once every yield has been used, the generator is empty.

![getcourses exhausted extra next StopIteration](data:image/svg+xml;charset=utf-8,%3Csvg%20font-family%3D%27ui-sans-serif%2C-apple-system%2Csystem-ui%2CArial%27%20width%3D%27100%25%27%20xmlns%3D%27http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%27%20viewBox%3D%270%200%20320%2084%27%3E%20%3Crect%20width%3D%27320%27%20height%3D%2784%27%20rx%3D%2712%27%20fill%3D%27%23FEF2F2%27%2F%3E%20%3Ccircle%20cx%3D%2736%27%20cy%3D%2732%27%20r%3D%2714%27%20fill%3D%27%2322C55E%27%2F%3E%20%3Ctext%20x%3D%2736%27%20y%3D%2736%27%20font-size%3D%277%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%23FFFFFF%27%3EP%3C%2Ftext%3E%20%3Cpath%20d%3D%27M50%2032%20H58%27%20stroke%3D%27%2364748B%27%20stroke-dasharray%3D%272%202%27%2F%3E%20%3Ccircle%20cx%3D%2792%27%20cy%3D%2732%27%20r%3D%2714%27%20fill%3D%27%2322C55E%27%2F%3E%20%3Ctext%20x%3D%2792%27%20y%3D%2736%27%20font-size%3D%277%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%23FFFFFF%27%3EH%3C%2Ftext%3E%20%3Cpath%20d%3D%27M106%2032%20H114%27%20stroke%3D%27%2364748B%27%20stroke-dasharray%3D%272%202%27%2F%3E%20%3Ccircle%20cx%3D%27148%27%20cy%3D%2732%27%20r%3D%2714%27%20fill%3D%27%2322C55E%27%2F%3E%20%3Ctext%20x%3D%27148%27%20y%3D%2736%27%20font-size%3D%277%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%23FFFFFF%27%3EC%3C%2Ftext%3E%20%3Cpath%20d%3D%27M162%2032%20H170%27%20stroke%3D%27%2364748B%27%20stroke-dasharray%3D%272%202%27%2F%3E%20%3Ccircle%20cx%3D%27204%27%20cy%3D%2732%27%20r%3D%2714%27%20fill%3D%27%2322C55E%27%2F%3E%20%3Ctext%20x%3D%27204%27%20y%3D%2736%27%20font-size%3D%277%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%23FFFFFF%27%3EC%2B%2B%3C%2Ftext%3E%20%3Cpath%20d%3D%27M218%2032%20H226%27%20stroke%3D%27%2364748B%27%20stroke-dasharray%3D%272%202%27%2F%3E%20%3Ccircle%20cx%3D%27286%27%20cy%3D%2732%27%20r%3D%2716%27%20fill%3D%27%23DC2626%27%2F%3E%20%3Ctext%20x%3D%27286%27%20y%3D%2736%27%20font-size%3D%276%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%23FFFFFF%27%3ESTOP%3C%2Ftext%3E%20%3Ctext%20x%3D%27160%27%20y%3D%2766%27%20font-size%3D%278%27%20text-anchor%3D%27middle%27%20fill%3D%27%23991B1B%27%3EExtra%20next%28%29%20raises%20StopIteration%3C%2Ftext%3E%20%3C%2Fsvg%3E

python
g = getcourses()
next(g)
next(g)
next(g)
next(g)
next(g)   # StopIteration

This exception simply means: there are no more values left.

Real-world examples

![Instagram feed CSV row live stock price streaming examples](data:image/svg+xml;charset=utf-8,%3Csvg%20font-family%3D%27ui-sans-serif%2C-apple-system%2Csystem-ui%2CArial%27%20width%3D%27100%25%27%20xmlns%3D%27http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%27%20viewBox%3D%270%200%20320%20100%27%3E%20%3Crect%20width%3D%27320%27%20height%3D%27100%27%20rx%3D%2712%27%20fill%3D%27%23F8FAFC%27%2F%3E%20%3Crect%20x%3D%2716%27%20y%3D%2716%27%20width%3D%2788%27%20height%3D%2768%27%20rx%3D%278%27%20fill%3D%27%23EDE9FE%27%20stroke%3D%27%237C3AED%27%2F%3E%20%3Ctext%20x%3D%2760%27%20y%3D%2738%27%20font-size%3D%278%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%235B21B6%27%3EFeed%3C%2Ftext%3E%20%3Ctext%20x%3D%2760%27%20y%3D%2752%27%20font-size%3D%277%27%20text-anchor%3D%27middle%27%20fill%3D%27%236D28D9%27%3E10%20posts%3C%2Ftext%3E%20%3Ctext%20x%3D%2760%27%20y%3D%2764%27%20font-size%3D%277%27%20text-anchor%3D%27middle%27%20fill%3D%27%236D28D9%27%3Ethen%2010%20more%3C%2Ftext%3E%20%3Crect%20x%3D%27116%27%20y%3D%2716%27%20width%3D%2788%27%20height%3D%2768%27%20rx%3D%278%27%20fill%3D%27%23DBEAFE%27%20stroke%3D%27%232563EB%27%2F%3E%20%3Ctext%20x%3D%27160%27%20y%3D%2738%27%20font-size%3D%278%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%231D4ED8%27%3ECSV%3C%2Ftext%3E%20%3Ctext%20x%3D%27160%27%20y%3D%2752%27%20font-size%3D%277%27%20text-anchor%3D%27middle%27%20fill%3D%27%231E40AF%27%3EOne%20row%3C%2Ftext%3E%20%3Ctext%20x%3D%27160%27%20y%3D%2764%27%20font-size%3D%277%27%20text-anchor%3D%27middle%27%20fill%3D%27%231E40AF%27%3Eat%20a%20time%3C%2Ftext%3E%20%3Crect%20x%3D%27216%27%20y%3D%2716%27%20width%3D%2788%27%20height%3D%2768%27%20rx%3D%278%27%20fill%3D%27%23D1FAE5%27%20stroke%3D%27%23059669%27%2F%3E%20%3Ctext%20x%3D%27260%27%20y%3D%2738%27%20font-size%3D%278%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%23047857%27%3EStocks%3C%2Ftext%3E%20%3Ctext%20x%3D%27260%27%20y%3D%2752%27%20font-size%3D%277%27%20text-anchor%3D%27middle%27%20fill%3D%27%23065F46%27%3EEach%20price%3C%2Ftext%3E%20%3Ctext%20x%3D%27260%27%20y%3D%2764%27%20font-size%3D%277%27%20text-anchor%3D%27middle%27%20fill%3D%27%23065F46%27%3Eas%20it%20arrives%3C%2Ftext%3E%20%3C%2Fsvg%3E

  • Social feed — loads 10 posts now, then 10 more when you scroll
  • CSV reader — reads one row instead of loading millions into RAM
  • Live stock prices — each new price is yielded as it arrives

Interview cheat sheet

Question Answer
Keyword? yield
Returns what? Generator object
Ends with? StopIteration
Main benefit? Memory efficiency
Difference? return ends, yield pauses

30-second recap

![function yield generator next value recap flow](data:image/svg+xml;charset=utf-8,%3Csvg%20font-family%3D%27ui-sans-serif%2C-apple-system%2Csystem-ui%2CArial%27%20width%3D%27100%25%27%20xmlns%3D%27http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%27%20viewBox%3D%270%200%20320%20176%27%3E%20%3Crect%20width%3D%27320%27%20height%3D%27176%27%20rx%3D%2716%27%20fill%3D%27%23F8FAFC%27%2F%3E%20%3Crect%20x%3D%2770%27%20y%3D%2710%27%20width%3D%27180%27%20height%3D%2722%27%20rx%3D%276%27%20fill%3D%27%23DBEAFE%27%2F%3E%20%3Ctext%20x%3D%27160%27%20y%3D%2725%27%20font-size%3D%279%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%231D4ED8%27%3EFunction%3C%2Ftext%3E%20%3Cpath%20d%3D%27M160%2032%20V40%27%20stroke%3D%27%2364748B%27%20stroke-dasharray%3D%272%202%27%2F%3E%20%3Crect%20x%3D%2770%27%20y%3D%2742%27%20width%3D%27180%27%20height%3D%2722%27%20rx%3D%276%27%20fill%3D%27%23D1FAE5%27%2F%3E%20%3Ctext%20x%3D%27160%27%20y%3D%2757%27%20font-size%3D%279%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%23047857%27%3Eyield%3C%2Ftext%3E%20%3Cpath%20d%3D%27M160%2064%20V72%27%20stroke%3D%27%2364748B%27%20stroke-dasharray%3D%272%202%27%2F%3E%20%3Crect%20x%3D%2770%27%20y%3D%2774%27%20width%3D%27180%27%20height%3D%2722%27%20rx%3D%276%27%20fill%3D%27%23EDE9FE%27%2F%3E%20%3Ctext%20x%3D%27160%27%20y%3D%2789%27%20font-size%3D%279%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%236D28D9%27%3EGenerator%3C%2Ftext%3E%20%3Cpath%20d%3D%27M160%2096%20V104%27%20stroke%3D%27%2364748B%27%20stroke-dasharray%3D%272%202%27%2F%3E%20%3Crect%20x%3D%2770%27%20y%3D%27106%27%20width%3D%27180%27%20height%3D%2722%27%20rx%3D%276%27%20fill%3D%27%23FDE68A%27%2F%3E%20%3Ctext%20x%3D%27160%27%20y%3D%27121%27%20font-size%3D%279%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%2392400E%27%3Enext%28%29%3C%2Ftext%3E%20%3Cpath%20d%3D%27M160%20128%20V136%27%20stroke%3D%27%2364748B%27%20stroke-dasharray%3D%272%202%27%2F%3E%20%3Crect%20x%3D%2770%27%20y%3D%27138%27%20width%3D%27180%27%20height%3D%2722%27%20rx%3D%276%27%20fill%3D%27%23FCA5A5%27%2F%3E%20%3Ctext%20x%3D%27160%27%20y%3D%27153%27%20font-size%3D%279%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%23991B1B%27%3EValue%3C%2Ftext%3E%20%3C%2Fsvg%3E

Remember this sentence:

A generator is a function that pauses with yield and continues when next() is called.

Next: kvrange with start, stop, step.

Loading playground…

Check your understanding

Generators and yield quiz

A function with yield returns when called:

1 / 10