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:

    • yield pauses the function, returns a value.
    • Generator expressions: (x*2 for x in xs).
    • next() advances; loops iterate.
    • yield from delegates to another generator.

    Syntax reference

    Visual flow / code:

    python
    def count_up(limit: int):
    n = 0
    while n < limit:
    yield n
    n += 1
    for x in count_up(5):
    print(x) # 0..4
    # Generator expression — memory-efficient
    total = sum(x * x for x in range(1_000_000))
    # Stream a huge file line by line
    def read_lines(path):
    with open(path) as f:
    for line in f:
    yield line.strip()

    Execution workflow

    1Generators & yield Workflow
    1 / 4

    Step 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 itertools for 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.