Learning

Generators & Yield

Generators & Yield

Generator Functions

python
def count_up(n): i = 0 while i < n: yield i i += 1 for num in count_up(5): print(num, end=' ') # 0 1 2 3 4 # Returns a generator object, NOT a list! print(type(count_up(5))) # <class 'generator'>

yield vs return

python
def first_n(n): result = [] for i in range(n): result.append(i ** 2) return result # Returns FULL list at once def first_n_gen(n): for i in range(n): yield i ** 2 # Yields ONE value at a time print(first_n(5)) # [0, 1, 4, 9, 16] print(first_n_gen(5)) # <generator object> print(list(first_n_gen(5))) # [0, 1, 4, 9, 16]

Generator Expressions

python
# List comprehension — builds entire list in memory squares_list = [x**2 for x in range(1000000)] # ~8MB # Generator expression — lazy, ~0MB squares_gen = (x**2 for x in range(1000000)) # ~0MB print(sum(squares_gen)) # Consumes values one by one

yield from (Delegation)

python
def upper_letters(): yield from 'ABCDE' def lower_letters(): yield from 'abcde' def all_letters(): yield from upper_letters() yield from lower_letters() print(list(all_letters())) # ['A','B','C','D','E','a','b','c','d','e']

Infinite Generators

python
def fibonacci(): a, b = 0, 1 while True: yield a a, b = b, a + b import itertools print(list(itertools.islice(fibonacci(), 10))) # [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

send() Method

python
def accumulator(): total = 0 while True: value = yield total if value is not None: total += value gen = accumulator() next(gen) # Prime the generator (returns 0) print(gen.send(10)) # 10 print(gen.send(20)) # 30 print(gen.send(5)) # 35
Key Rules
  • •A function with yield is a generator function — calling it returns a generator OBJECT, not executing the body
  • •yield PAUSES the function and saves all local state — resumes from exact point on next() call
  • •Generator expressions use () instead of [] — they're memory-efficient lazy iterators
  • •yield from delegates to a sub-generator — equivalent to a for loop yielding each value
  • •Infinite generators are safe because values are produced on-demand — use itertools.islice to limit
  • •send(value) passes a value INTO the generator — must prime with next() first before sending
Your Task

Create a generator function `prime_numbers()` that yields prime numbers infinitely. Create a generator function `take(n, gen)` that yields only the first n values from any generator using yield from with islice. Create a generator expression `even_squares` that yields squares of even numbers from 0-20. Demonstrate: first 10 primes, even_squares as a list, and a pipeline where you filter primes > 100.

EditorPython · JSX
PreviewUpdates on Run Tests
Loading preview…
Tests
Should define prime_numbers generator with yield
Should have is_prime helper function
Should define take with yield from and islice
Should create even_squares as generator expression
Should demonstrate with list()
Should import itertools