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.
is the remote;next()is the button - Generator — Water tap:
yieldpours one glass at a time, not the whole tank - Decorator — Gift wrap:
@adds presentation without changing the phone inside - How
iter(),yield, and@decoratorwork together in Python
One simple picture

print(next(it)) # Ep1
print(next(it)) # Ep2
print(next(it)) # Ep3What's happening?
iter()creates the remote controlnext()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.
:
yield "Glass 1"
yield "Glass 2"
yield "Glass 3"
tap = water()
print(next(tap)) # Glass 1
print(next(tap)) # Glass 2Every 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.
:
def inner():
item = func()
return "Gift: " + item
return inner
@gift_wrap
def phone():
return "iPhone"
print(phone()) # Gift: iPhoneThe 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
 + 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 controlnext()→ press Nextyield→ pour one glass@decorator→ wrap the gift
Complete example
Run the playground for the full script — list iterator, getcourses, @square, and StringIO:
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):
10
RS
PYTHON HTML
Square(5.0)=25.0
['Swathi\n', 'Rahul\n', 'Priya\n']Interview questions
-
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(). -
Why is
yieldmore memory efficient thanreturn?yieldgenerates values only when needed — like one glass from the tap, not emptying the whole tank. -
What does
@decoratoractually do?@decoratoris shorthand forfunction = 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