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

Lesson 01

Iterators, Generators, and Decorators — Demand-Driven Python

Learn iterators with a Netflix analogy, generators as a water tap, and decorators as gift wrapping—then map iter(), yield, and @ to Python.

Intermediate25 min

![Netflix water tap gift wrap module hero](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%20640%20220%27%3E%20%3Cdefs%3E%20%3ClinearGradient%20id%3D%27igdov2h%27%20x1%3D%270%27%20y1%3D%270%27%20x2%3D%271%27%20y2%3D%271%27%3E%20%3Cstop%20offset%3D%270%25%27%20stop-color%3D%27%237C3AED%27%2F%3E%20%3Cstop%20offset%3D%2755%25%27%20stop-color%3D%27%232563EB%27%2F%3E%20%3Cstop%20offset%3D%27100%25%27%20stop-color%3D%27%230F172A%27%2F%3E%20%3C%2FlinearGradient%3E%20%3C%2Fdefs%3E%20%3Crect%20width%3D%27640%27%20height%3D%27220%27%20rx%3D%2720%27%20fill%3D%27url%28%23igdov2h%29%27%2F%3E%20%3Crect%20x%3D%2736%27%20y%3D%2736%27%20width%3D%27360%27%20height%3D%27148%27%20rx%3D%2714%27%20fill%3D%27%230F172A%27%20opacity%3D%270.45%27%2F%3E%20%3Ctext%20x%3D%2752%27%20y%3D%2768%27%20fill%3D%27%23C4B5FD%27%20font-size%3D%2712%27%20font-weight%3D%27bold%27%3EMODULE%2013%20%C2%B7%20STORY%20FIRST%3C%2Ftext%3E%20%3Ctext%20x%3D%2752%27%20y%3D%2798%27%20fill%3D%27%23FFFFFF%27%20font-size%3D%2720%27%20font-weight%3D%27bold%27%3ENetflix%20%C2%B7%20Water%20Tap%20%C2%B7%20Gift%20Wrap%3C%2Ftext%3E%20%3Ctext%20x%3D%2752%27%20y%3D%27122%27%20fill%3D%27%23E2E8F0%27%20font-size%3D%2712%27%3EHumans%20understand%20stories%20before%20syntax%3C%2Ftext%3E%20%3Ctext%20x%3D%2752%27%20y%3D%27148%27%20fill%3D%27%23A5B4FC%27%20font-size%3D%2711%27%3EThen%20map%20to%20iter%28%29%20%C2%B7%20yield%20%C2%B7%20%40decorator%3C%2Ftext%3E%20%3Crect%20x%3D%27420%27%20y%3D%2748%27%20width%3D%27184%27%20height%3D%27124%27%20rx%3D%2712%27%20fill%3D%27%23FFFFFF%27%20opacity%3D%270.12%27%2F%3E%20%3Ctext%20x%3D%27440%27%20y%3D%2778%27%20fill%3D%27%23FDE68A%27%20font-size%3D%2710%27%3EIterator%20%3D%20Next%20episode%3C%2Ftext%3E%20%3Ctext%20x%3D%27440%27%20y%3D%2798%27%20fill%3D%27%23BBF7D0%27%20font-size%3D%2710%27%3EGenerator%20%3D%20One%20glass%3C%2Ftext%3E%20%3Ctext%20x%3D%27440%27%20y%3D%27118%27%20fill%3D%27%23E0F2FE%27%20font-size%3D%2710%27%3EDecorator%20%3D%20Wrapped%20gift%3C%2Ftext%3E%20%3Ctext%20x%3D%27440%27%20y%3D%27144%27%20fill%3D%27%23FBCFE8%27%20font-size%3D%2710%27%3EPlayground%20%2B%20quiz%3C%2Ftext%3E%20%3C%2Fsvg%3E

Like Netflix, a water tap, and gift wrapping

Most iterator and generator lessons start with syntax. Humans understand stories first. Imagine your daily life instead of code — then map each story to Python.

Use the playground and quiz at the bottom to turn these stories into runnable code.

What you will learn

  • Iterator — Netflix Next Episode: iter() is the remote; next() is the button
  • Generator — Water tap: yield pours one glass at a time, not the whole tank
  • Decorator — Gift wrap: @ adds presentation without changing the phone inside
  • How iter(), yield, and @decorator work together in Python

One simple picture

![iterator walks data generator creates decorator adds extra](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%2096%27%3E%20%3Crect%20width%3D%27320%27%20height%3D%2796%27%20rx%3D%2716%27%20fill%3D%27%23F8FAFC%27%2F%3E%20%3Crect%20x%3D%2710%27%20y%3D%2718%27%20width%3D%2790%27%20height%3D%2760%27%20rx%3D%2710%27%20fill%3D%27%23DBEAFE%27%20stroke%3D%27%232563EB%27%2F%3E%20%3Ctext%20x%3D%2755%27%20y%3D%2736%27%20font-size%3D%2710%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%231D4ED8%27%3EIterator%3C%2Ftext%3E%20%3Ctext%20x%3D%2755%27%20y%3D%2750%27%20font-size%3D%278%27%20text-anchor%3D%27middle%27%20fill%3D%27%231E40AF%27%3EOne%20by%20one%3C%2Ftext%3E%20%3Cpath%20d%3D%27M100%2048%20H118%27%20stroke%3D%27%2364748B%27%20stroke-dasharray%3D%273%203%27%2F%3E%20%3Cpolygon%20points%3D%27116%2C44%20122%2C48%20116%2C52%27%20fill%3D%27%2364748B%27%2F%3E%20%3Crect%20x%3D%27122%27%20y%3D%2718%27%20width%3D%2790%27%20height%3D%2760%27%20rx%3D%2710%27%20fill%3D%27%23D1FAE5%27%20stroke%3D%27%23059669%27%2F%3E%20%3Ctext%20x%3D%27167%27%20y%3D%2736%27%20font-size%3D%2710%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%23047857%27%3EGenerator%3C%2Ftext%3E%20%3Ctext%20x%3D%27167%27%20y%3D%2750%27%20font-size%3D%278%27%20text-anchor%3D%27middle%27%20fill%3D%27%23065F46%27%3ECreates%20on%3C%2Ftext%3E%20%3Ctext%20x%3D%27167%27%20y%3D%2760%27%20font-size%3D%278%27%20text-anchor%3D%27middle%27%20fill%3D%27%23065F46%27%3Edemand%3C%2Ftext%3E%20%3Cpath%20d%3D%27M212%2048%20H230%27%20stroke%3D%27%2364748B%27%20stroke-dasharray%3D%273%203%27%2F%3E%20%3Cpolygon%20points%3D%27228%2C44%20234%2C48%20228%2C52%27%20fill%3D%27%2364748B%27%2F%3E%20%3Crect%20x%3D%27234%27%20y%3D%2718%27%20width%3D%2776%27%20height%3D%2760%27%20rx%3D%2710%27%20fill%3D%27%23FDE68A%27%20stroke%3D%27%23D97706%27%2F%3E%20%3Ctext%20x%3D%27272%27%20y%3D%2736%27%20font-size%3D%2710%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%2392400E%27%3EDecorator%3C%2Ftext%3E%20%3Ctext%20x%3D%27272%27%20y%3D%2750%27%20font-size%3D%278%27%20text-anchor%3D%27middle%27%20fill%3D%27%2378350F%27%3EAdds%20extra%3C%2Ftext%3E%20%3Ctext%20x%3D%27160%27%20y%3D%2788%27%20font-size%3D%278%27%20text-anchor%3D%27middle%27%20fill%3D%27%2364748B%27%3EWalks%20data%20%C2%B7%20Creates%20data%20%C2%B7%20Enhances%20functions%3C%2Ftext%3E%20%3C%2Fsvg%3E

Story Python idea Real-life action
Netflix Next Iterator One episode when you ask
Water tap Generator One glass when you open
Gift wrap Decorator Same item, extra layer

1. Iterator = Netflix "Next Episode"

You do not download all 500 episodes. You press Next, and Netflix gives you one episode.

![Netflix press Next get one episode 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%2716%27%20fill%3D%27%23111827%27%2F%3E%20%3Ctext%20x%3D%2716%27%20y%3D%2718%27%20font-size%3D%2710%27%20font-weight%3D%27bold%27%20fill%3D%27%23F59E0B%27%3ENETFLIX%3C%2Ftext%3E%20%3Crect%20x%3D%2716%27%20y%3D%2734%27%20width%3D%2780%27%20height%3D%2756%27%20rx%3D%278%27%20fill%3D%27%23DC2626%27%2F%3E%20%3Ctext%20x%3D%2756%27%20y%3D%2752%27%20font-size%3D%278%27%20text-anchor%3D%27middle%27%20fill%3D%27%23FFFFFF%27%3EEpisode%201%3C%2Ftext%3E%20%3Ctext%20x%3D%2756%27%20y%3D%2766%27%20font-size%3D%277%27%20text-anchor%3D%27middle%27%20fill%3D%27%23FECACA%27%3Eplaying%3C%2Ftext%3E%20%3Cpath%20d%3D%27M96%2062%20H106%27%20stroke%3D%27%239CA3AF%27%20stroke-dasharray%3D%273%203%27%2F%3E%20%3Cpolygon%20points%3D%27104%2C58%20110%2C62%20104%2C66%27%20fill%3D%27%239CA3AF%27%2F%3E%20%3Crect%20x%3D%27112%27%20y%3D%2734%27%20width%3D%2780%27%20height%3D%2756%27%20rx%3D%278%27%20fill%3D%27%23374151%27%2F%3E%20%3Ctext%20x%3D%27152%27%20y%3D%2752%27%20font-size%3D%278%27%20text-anchor%3D%27middle%27%20fill%3D%27%23FFFFFF%27%3EEpisode%202%3C%2Ftext%3E%20%3Cpath%20d%3D%27M192%2062%20H202%27%20stroke%3D%27%239CA3AF%27%20stroke-dasharray%3D%273%203%27%2F%3E%20%3Cpolygon%20points%3D%27200%2C58%20206%2C62%20200%2C66%27%20fill%3D%27%239CA3AF%27%2F%3E%20%3Crect%20x%3D%27208%27%20y%3D%2734%27%20width%3D%2780%27%20height%3D%2756%27%20rx%3D%278%27%20fill%3D%27%23374151%27%2F%3E%20%3Ctext%20x%3D%27248%27%20y%3D%2752%27%20font-size%3D%278%27%20text-anchor%3D%27middle%27%20fill%3D%27%23FFFFFF%27%3EEpisode%203%3C%2Ftext%3E%20%3Ctext%20x%3D%27160%27%20y%3D%27108%27%20font-size%3D%278%27%20text-anchor%3D%27middle%27%20fill%3D%27%23D1D5DB%27%3EPress%20Next%20%E2%80%94%20get%20one%20episode%20%28next%28%29%29%3C%2Ftext%3E%20%3C%2Fsvg%3E

Python version:

python
movies = ["Ep1", "Ep2", "Ep3"]
it = iter(movies)
print(next(it))   # Ep1
print(next(it))   # Ep2
print(next(it))   # Ep3

What's happening?

  • iter() creates the remote control
  • next() presses the Next button
  • When episodes finish → StopIteration

Try the mixed-type list vals = [10, "RS", 23.45, True] in the playground below.

Deep dive: iter() and next().

2. Generator = Water Tap

A water tank holds 1000 liters. Your tap gives only the water you ask for — not all 1000 liters at once.

![water tank tap yields one glass 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%20140%27%3E%20%3Crect%20width%3D%27320%27%20height%3D%27140%27%20rx%3D%2716%27%20fill%3D%27%23ECFDF5%27%2F%3E%20%3Crect%20x%3D%2722%27%20y%3D%2718%27%20width%3D%2784%27%20height%3D%2754%27%20rx%3D%278%27%20fill%3D%27%2393C5FD%27%20stroke%3D%27%232563EB%27%2F%3E%20%3Ctext%20x%3D%2764%27%20y%3D%2734%27%20font-size%3D%279%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%231D4ED8%27%3EWater%20Tank%3C%2Ftext%3E%20%3Ctext%20x%3D%2764%27%20y%3D%2746%27%20font-size%3D%278%27%20text-anchor%3D%27middle%27%20fill%3D%27%231E40AF%27%3E1000%20L%3C%2Ftext%3E%20%3Cpath%20d%3D%27M106%2045%20H230%27%20stroke%3D%27%2364748B%27%20stroke-width%3D%274%27%20stroke-linecap%3D%27round%27%2F%3E%20%3Crect%20x%3D%27230%27%20y%3D%2736%27%20width%3D%2740%27%20height%3D%2718%27%20rx%3D%274%27%20fill%3D%27%239CA3AF%27%2F%3E%20%3Crect%20x%3D%27242%27%20y%3D%2754%27%20width%3D%278%27%20height%3D%2716%27%20rx%3D%273%27%20fill%3D%27%236B7280%27%2F%3E%20%3Cpath%20d%3D%27M246%2078%20C242%2086%2C%20250%2092%2C%20246%20100%20C242%2092%2C%20242%2086%2C%20246%2078%20Z%27%20fill%3D%27%232563EB%27%20opacity%3D%270.9%27%2F%3E%20%3Cpath%20d%3D%27M254%2088%20C250%2096%2C%20258%20102%2C%20254%20110%20C250%20102%2C%20250%2096%2C%20254%2088%20Z%27%20fill%3D%27%232563EB%27%20opacity%3D%270.7%27%2F%3E%20%3Cpath%20d%3D%27M262%2098%20C258%20106%2C%20266%20112%2C%20262%20120%20C258%20112%2C%20258%20106%2C%20262%2098%20Z%27%20fill%3D%27%232563EB%27%20opacity%3D%270.5%27%2F%3E%20%3Ctext%20x%3D%27160%27%20y%3D%27124%27%20font-size%3D%278%27%20text-anchor%3D%27middle%27%20fill%3D%27%23047857%27%3EWater%20flows%20only%20when%20opened%20%28yield%29%3C%2Ftext%3E%20%3C%2Fsvg%3E

Python version:

python
def water():
    yield "Glass 1"
    yield "Glass 2"
    yield "Glass 3"

tap = water()
print(next(tap))   # Glass 1
print(next(tap))   # Glass 2

Every yield is like opening the tap for one glass. The function pauses until the next next() or for step.

The getcourses() generator with PYTHON, HTML, C, C++ uses the same pattern in the playground.

Deep dive: Generators and yield.

3. Decorator = Gift Wrapping

Your friend gives you a phone. You wrap it in gift paper. The phone is unchanged — you only added something around it.

![phone wrapped as gift same inside extra outside](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%2716%27%20fill%3D%27%23FFFBEB%27%2F%3E%20%3Crect%20x%3D%2718%27%20y%3D%2742%27%20width%3D%2762%27%20height%3D%2754%27%20rx%3D%2710%27%20fill%3D%27%23E5E7EB%27%20stroke%3D%27%236B7280%27%2F%3E%20%3Crect%20x%3D%2728%27%20y%3D%2750%27%20width%3D%2742%27%20height%3D%2736%27%20rx%3D%276%27%20fill%3D%27%239CA3AF%27%2F%3E%20%3Ctext%20x%3D%2749%27%20y%3D%27110%27%20font-size%3D%278%27%20text-anchor%3D%27middle%27%20fill%3D%27%23374151%27%3EPhone%3C%2Ftext%3E%20%3Cpath%20d%3D%27M80%2069%20H118%27%20stroke%3D%27%2364748B%27%20stroke-dasharray%3D%273%203%27%2F%3E%20%3Cpolygon%20points%3D%27114%2C65%20120%2C69%20114%2C73%27%20fill%3D%27%2364748B%27%2F%3E%20%3Crect%20x%3D%27120%27%20y%3D%2728%27%20width%3D%2782%27%20height%3D%2782%27%20rx%3D%2712%27%20fill%3D%27%23F59E0B%27%2F%3E%20%3Cpath%20d%3D%27M161%2028%20V110%27%20stroke%3D%27%23FFFFFF%27%20stroke-width%3D%274%27%2F%3E%20%3Cpath%20d%3D%27M120%2069%20H202%27%20stroke%3D%27%23FFFFFF%27%20stroke-width%3D%274%27%2F%3E%20%3Ccircle%20cx%3D%27161%27%20cy%3D%2769%27%20r%3D%278%27%20fill%3D%27%23FFFFFF%27%2F%3E%20%3Ctext%20x%3D%27161%27%20y%3D%27122%27%20font-size%3D%278%27%20text-anchor%3D%27middle%27%20fill%3D%27%2392400E%27%3EWrapped%3C%2Ftext%3E%20%3Cpath%20d%3D%27M202%2069%20H240%27%20stroke%3D%27%2364748B%27%20stroke-dasharray%3D%273%203%27%2F%3E%20%3Cpolygon%20points%3D%27236%2C65%20242%2C69%20236%2C73%27%20fill%3D%27%2364748B%27%2F%3E%20%3Crect%20x%3D%27242%27%20y%3D%2742%27%20width%3D%2760%27%20height%3D%2754%27%20rx%3D%2710%27%20fill%3D%27%2378350F%27%2F%3E%20%3Ctext%20x%3D%27272%27%20y%3D%2760%27%20font-size%3D%278%27%20text-anchor%3D%27middle%27%20fill%3D%27%23FDE68A%27%3EGift%3C%2Ftext%3E%20%3Ctext%20x%3D%27272%27%20y%3D%2772%27%20font-size%3D%278%27%20text-anchor%3D%27middle%27%20fill%3D%27%23FFFFFF%27%3EReady%3C%2Ftext%3E%20%3C%2Fsvg%3E

Python version:

python
def gift_wrap(func):
    def inner():
        item = func()
        return "Gift: " + item
    return inner

@gift_wrap
def phone():
    return "iPhone"

print(phone())   # Gift: iPhone

The decorator does not rewrite phone(). It only wraps what comes back.

The @square example on getval() (returns 5.0, squares to 25.0) is in the playground — same wrapping idea with math instead of a bow.

Deep dive: Decorators overview.

yield vs return — The Delivery Guy Story

![return dumps all boxes yield one package per trip](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%20340%20120%27%3E%20%3Crect%20width%3D%27340%27%20height%3D%27120%27%20rx%3D%2716%27%20fill%3D%27%23F8FAFC%27%2F%3E%20%3Crect%20x%3D%2716%27%20y%3D%2716%27%20width%3D%27148%27%20height%3D%2788%27%20rx%3D%2712%27%20fill%3D%27%23FEE2E2%27%20stroke%3D%27%23EF4444%27%2F%3E%20%3Ctext%20x%3D%2790%27%20y%3D%2736%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%3Ctext%20x%3D%2790%27%20y%3D%2754%27%20font-size%3D%278%27%20text-anchor%3D%27middle%27%20fill%3D%27%237F1D1D%27%3EAll%20boxes%20at%20once%3C%2Ftext%3E%20%3Crect%20x%3D%2734%27%20y%3D%2762%27%20width%3D%2728%27%20height%3D%2720%27%20rx%3D%274%27%20fill%3D%27%23DC2626%27%20opacity%3D%270.8%27%2F%3E%20%3Crect%20x%3D%2766%27%20y%3D%2762%27%20width%3D%2728%27%20height%3D%2720%27%20rx%3D%274%27%20fill%3D%27%23DC2626%27%20opacity%3D%270.8%27%2F%3E%20%3Crect%20x%3D%2798%27%20y%3D%2762%27%20width%3D%2728%27%20height%3D%2720%27%20rx%3D%274%27%20fill%3D%27%23DC2626%27%20opacity%3D%270.8%27%2F%3E%20%3Crect%20x%3D%27130%27%20y%3D%2762%27%20width%3D%2728%27%20height%3D%2720%27%20rx%3D%274%27%20fill%3D%27%23DC2626%27%20opacity%3D%270.8%27%2F%3E%20%3Ctext%20x%3D%2790%27%20y%3D%2798%27%20font-size%3D%277%27%20text-anchor%3D%27middle%27%20fill%3D%27%23991B1B%27%3EHeavy%20memory%3C%2Ftext%3E%20%3Crect%20x%3D%27176%27%20y%3D%2716%27%20width%3D%27148%27%20height%3D%2788%27%20rx%3D%2712%27%20fill%3D%27%23D1FAE5%27%20stroke%3D%27%23059669%27%2F%3E%20%3Ctext%20x%3D%27250%27%20y%3D%2736%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%3Ctext%20x%3D%27250%27%20y%3D%2754%27%20font-size%3D%278%27%20text-anchor%3D%27middle%27%20fill%3D%27%23065F46%27%3EOne%20package%20per%20trip%3C%2Ftext%3E%20%3Ccircle%20cx%3D%27220%27%20cy%3D%2772%27%20r%3D%2714%27%20fill%3D%27%2310B981%27%2F%3E%20%3Ctext%20x%3D%27220%27%20y%3D%2776%27%20font-size%3D%278%27%20text-anchor%3D%27middle%27%20fill%3D%27%23FFFFFF%27%3E1%3C%2Ftext%3E%20%3Cpath%20d%3D%27M238%2072%20H268%27%20stroke%3D%27%23059669%27%20stroke-dasharray%3D%273%203%27%2F%3E%20%3Cpolygon%20points%3D%27264%2C68%20272%2C72%20264%2C76%27%20fill%3D%27%23059669%27%2F%3E%20%3Ccircle%20cx%3D%27286%27%20cy%3D%2772%27%20r%3D%2714%27%20fill%3D%27%2310B981%27%20opacity%3D%270.5%27%2F%3E%20%3Ctext%20x%3D%27250%27%20y%3D%2798%27%20font-size%3D%277%27%20text-anchor%3D%27middle%27%20fill%3D%27%23047857%27%3EEfficient%20and%20scalable%3C%2Ftext%3E%20%3C%2Fsvg%3E

Feature return yield
Delivery style Everything at once One package per trip
Ends function Yes Pauses — can resume
Memory on huge data Heavy Efficient

When to pick which:

Situation Best choice
10 numbers return
10 million records yield
CSV file reading yield
API pagination yield

How they work together

![data source through iterator generator decorator to output](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%20220%27%3E%20%3Cdefs%3E%20%3Cmarker%20id%3D%27igdov2pip%27%20markerWidth%3D%278%27%20markerHeight%3D%278%27%20refX%3D%274%27%20refY%3D%274%27%20orient%3D%27auto%27%3E%20%3Cpath%20d%3D%27M0%2C0%20L8%2C4%20L0%2C8%20Z%27%20fill%3D%27%2364748B%27%2F%3E%20%3C%2Fmarker%3E%20%3C%2Fdefs%3E%20%3Crect%20width%3D%27320%27%20height%3D%27220%27%20rx%3D%2716%27%20fill%3D%27%23F8FAFC%27%2F%3E%20%3Crect%20x%3D%2740%27%20y%3D%2710%27%20width%3D%27240%27%20height%3D%2726%27%20rx%3D%278%27%20fill%3D%27%23DBEAFE%27%20stroke%3D%27%231D4ED8%27%2F%3E%20%3Ctext%20x%3D%2752%27%20y%3D%2721%27%20font-size%3D%279%27%20font-weight%3D%27bold%27%20fill%3D%27%231D4ED8%27%3EData%20Source%3C%2Ftext%3E%20%3Ctext%20x%3D%27268%27%20y%3D%2721%27%20font-size%3D%278%27%20text-anchor%3D%27end%27%20fill%3D%27%231D4ED8%27%3EMovies%2C%20CSV%3C%2Ftext%3E%20%3Cline%20x1%3D%27160%27%20y1%3D%2736%27%20x2%3D%27160%27%20y2%3D%2744%27%20stroke%3D%27%2364748B%27%20stroke-dasharray%3D%273%203%27%20marker-end%3D%27url%28%23igdov2pip%29%27%2F%3E%20%3Crect%20x%3D%2740%27%20y%3D%2750%27%20width%3D%27240%27%20height%3D%2726%27%20rx%3D%278%27%20fill%3D%27%23EDE9FE%27%20stroke%3D%27%236D28D9%27%2F%3E%20%3Ctext%20x%3D%2752%27%20y%3D%2761%27%20font-size%3D%279%27%20font-weight%3D%27bold%27%20fill%3D%27%236D28D9%27%3EIterator%3C%2Ftext%3E%20%3Ctext%20x%3D%27268%27%20y%3D%2761%27%20font-size%3D%278%27%20text-anchor%3D%27end%27%20fill%3D%27%236D28D9%27%3EReads%20one%3C%2Ftext%3E%20%3Cline%20x1%3D%27160%27%20y1%3D%2776%27%20x2%3D%27160%27%20y2%3D%2784%27%20stroke%3D%27%2364748B%27%20stroke-dasharray%3D%273%203%27%20marker-end%3D%27url%28%23igdov2pip%29%27%2F%3E%20%3Crect%20x%3D%2740%27%20y%3D%2790%27%20width%3D%27240%27%20height%3D%2726%27%20rx%3D%278%27%20fill%3D%27%23D1FAE5%27%20stroke%3D%27%23047857%27%2F%3E%20%3Ctext%20x%3D%2752%27%20y%3D%27101%27%20font-size%3D%279%27%20font-weight%3D%27bold%27%20fill%3D%27%23047857%27%3EGenerator%3C%2Ftext%3E%20%3Ctext%20x%3D%27268%27%20y%3D%27101%27%20font-size%3D%278%27%20text-anchor%3D%27end%27%20fill%3D%27%23047857%27%3ECreates%20one%3C%2Ftext%3E%20%3Cline%20x1%3D%27160%27%20y1%3D%27116%27%20x2%3D%27160%27%20y2%3D%27124%27%20stroke%3D%27%2364748B%27%20stroke-dasharray%3D%273%203%27%20marker-end%3D%27url%28%23igdov2pip%29%27%2F%3E%20%3Crect%20x%3D%2740%27%20y%3D%27130%27%20width%3D%27240%27%20height%3D%2726%27%20rx%3D%278%27%20fill%3D%27%23FDE68A%27%20stroke%3D%27%2392400E%27%2F%3E%20%3Ctext%20x%3D%2752%27%20y%3D%27141%27%20font-size%3D%279%27%20font-weight%3D%27bold%27%20fill%3D%27%2392400E%27%3EDecorator%3C%2Ftext%3E%20%3Ctext%20x%3D%27268%27%20y%3D%27141%27%20font-size%3D%278%27%20text-anchor%3D%27end%27%20fill%3D%27%2392400E%27%3EAdds%20extra%3C%2Ftext%3E%20%3Cline%20x1%3D%27160%27%20y1%3D%27156%27%20x2%3D%27160%27%20y2%3D%27164%27%20stroke%3D%27%2364748B%27%20stroke-dasharray%3D%273%203%27%20marker-end%3D%27url%28%23igdov2pip%29%27%2F%3E%20%3Crect%20x%3D%2740%27%20y%3D%27170%27%20width%3D%27240%27%20height%3D%2726%27%20rx%3D%278%27%20fill%3D%27%23F3F4F6%27%20stroke%3D%27%23374151%27%2F%3E%20%3Ctext%20x%3D%2752%27%20y%3D%27181%27%20font-size%3D%279%27%20font-weight%3D%27bold%27%20fill%3D%27%23374151%27%3EFinal%20Output%3C%2Ftext%3E%20%3Ctext%20x%3D%27268%27%20y%3D%27181%27%20font-size%3D%278%27%20text-anchor%3D%27end%27%20fill%3D%27%23374151%27%3EUser%20sees%20result%3C%2Ftext%3E%20%3C%2Fsvg%3E

Iterator reads one item from a source. Generator creates one item on demand. Decorator enhances what the user finally sees.

Remember it in 30 seconds

![recall Netflix tap gift map to next yield decorator](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%20340%20100%27%3E%20%3Crect%20width%3D%27340%27%20height%3D%27100%27%20rx%3D%2716%27%20fill%3D%27%23FFFFFF%27%20stroke%3D%27%23E5E7EB%27%2F%3E%20%3Crect%20x%3D%2712%27%20y%3D%2714%27%20width%3D%27100%27%20height%3D%2772%27%20rx%3D%2710%27%20fill%3D%27%23111827%27%2F%3E%20%3Ctext%20x%3D%2762%27%20y%3D%2734%27%20font-size%3D%278%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%23F59E0B%27%3ENETFLIX%3C%2Ftext%3E%20%3Ctext%20x%3D%2762%27%20y%3D%2750%27%20font-size%3D%277%27%20text-anchor%3D%27middle%27%20fill%3D%27%23D1D5DB%27%3Enext%28%29%3C%2Ftext%3E%20%3Ctext%20x%3D%2762%27%20y%3D%2772%27%20font-size%3D%277%27%20text-anchor%3D%27middle%27%20fill%3D%27%239CA3AF%27%3EIterator%3C%2Ftext%3E%20%3Crect%20x%3D%27120%27%20y%3D%2714%27%20width%3D%27100%27%20height%3D%2772%27%20rx%3D%2710%27%20fill%3D%27%23ECFDF5%27%20stroke%3D%27%23059669%27%2F%3E%20%3Ctext%20x%3D%27170%27%20y%3D%2734%27%20font-size%3D%278%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%23047857%27%3ETAP%3C%2Ftext%3E%20%3Ctext%20x%3D%27170%27%20y%3D%2750%27%20font-size%3D%277%27%20text-anchor%3D%27middle%27%20fill%3D%27%23065F46%27%3Eyield%3C%2Ftext%3E%20%3Ctext%20x%3D%27170%27%20y%3D%2772%27%20font-size%3D%277%27%20text-anchor%3D%27middle%27%20fill%3D%27%2364748B%27%3EGenerator%3C%2Ftext%3E%20%3Crect%20x%3D%27228%27%20y%3D%2714%27%20width%3D%27100%27%20height%3D%2772%27%20rx%3D%2710%27%20fill%3D%27%23FFFBEB%27%20stroke%3D%27%23D97706%27%2F%3E%20%3Ctext%20x%3D%27278%27%20y%3D%2734%27%20font-size%3D%278%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%2392400E%27%3EGIFT%3C%2Ftext%3E%20%3Ctext%20x%3D%27278%27%20y%3D%2750%27%20font-size%3D%277%27%20text-anchor%3D%27middle%27%20fill%3D%27%2378350F%27%3E%40decorator%3C%2Ftext%3E%20%3Ctext%20x%3D%27278%27%20y%3D%2772%27%20font-size%3D%277%27%20text-anchor%3D%27middle%27%20fill%3D%27%2364748B%27%3EDecorator%3C%2Ftext%3E%20%3C%2Fsvg%3E

Story Keyword Action
Netflix iter() + next() Press Next → one episode
Water tap yield Open tap → one glass
Gift wrap @decorator Wrap → same gift, better presentation

Each Python keyword maps to a real-life action you already know:

  • iter() → create the remote control
  • next() → press Next
  • yield → pour one glass
  • @decorator → wrap the gift

Complete example

Run the playground for the full script — list iterator, getcourses, @square, and StringIO:

python
from io import StringIO

vals = [10, "RS", 23.45, True]
itr = iter(vals)
print(type(vals), type(itr))
print(next(itr))
print(next(itr))

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

g = getcourses()
print(type(g), next(g), next(g))

def square(m):
    def calculate():
        n = m()
        return n, n ** 2
    return calculate

@square
def getval():
    return 5.0

n, sq = getval()
print("Square({})={}".format(n, sq))

names = StringIO("Swathi\nRahul\nPriya\n")
print(list(names))

Expected output (simplified):

text
10
RS
PYTHON HTML
Square(5.0)=25.0
['Swathi\n', 'Rahul\n', 'Priya\n']

Interview questions

  1. What is the difference between an iterable and an iterator?

    An iterable (like a list) can create an iterator. An iterator remembers its position and produces one item at a time using next().

  2. Why is yield more memory efficient than return?

    yield generates values only when needed — like one glass from the tap, not emptying the whole tank.

  3. What does @decorator actually do?

    @decorator is shorthand for function = decorator(function). It replaces the original function with the wrapped version.

Key takeaway

Do work only when needed.

  • Iterator (Netflix) → reads existing data one episode at a time
  • Generator (tap) → creates data one glass at a time with yield
  • Decorator (gift wrap) → extends behavior without rewriting the original function

Next: iter() and next().

Loading playground…

Check your understanding

Iterators generators decorators overview quiz

Why do the iterator notes exist?

1 / 10