Python Tutorial 0/52 lessons ~6 min read Lesson 16
Generators & yield
Generators produce values lazily using yield — memory-efficient for streams, large files, and infinite sequences.
Course progress0%
Focus
9 guided sections
Practice signal
Examples included
Career prep
Foundation builder
Introduction
Generators produce values lazily using yield — memory-efficient for streams, large files, and infinite sequences.
Understanding the topic
Core concepts to understand:
yieldpauses the function, returns a value.- Generator expressions:
(x*2 for x in xs). next()advances; loops iterate.yield fromdelegates to another generator.
Syntax reference
Visual flow / code:
python
def count_up(limit: int):n = 0while n < limit:yield nn += 1for x in count_up(5):print(x) # 0..4# Generator expression — memory-efficienttotal = sum(x * x for x in range(1_000_000))# Stream a huge file line by linedef read_lines(path):with open(path) as f:for line in f:yield line.strip()
Execution workflow
1Generators & yield Workflow
1 / 4Step 1
yield pauses the function, returns a value.
Apply this step while implementing generators & yield in real code.
Real-world use
Generators are how Python handles 50-GB log files without loading them into memory. ETL pipelines, web crawlers, and data streams all rely on them.
Best practices
- Use generators for any sequence > a few thousand items.
- Combine with
itertoolsfor power. - Prefer generator expressions to list comps when iterating once.
Common mistakes
- Generators are single-use — exhausted after iteration.
- Cannot index a generator (
g[0]fails).
Hands-on exercise
Interview preparation — practice these questions:
- yield vs return?
- Generator vs list — memory?
- What's lazy evaluation?
Summary
In summary: Lazy, memory-efficient sequences. yield = pause + emit.
Ready to mark this lesson complete?Track your journey across the entire course.