Iterators and Generators
Iterators and Generators, cheat sheet
Memory-efficient streaming with generators, context managers, and type hints.
on this sheet
01
Generators & yield
def gen():
yield 1
yield 2Lazy evaluation pausing at yield.
item = next(my_gen)Advance generator to next yielded value.
02
Context managers & types
with open(path) as f: passGuaranteed cleanup on block exit.
def fn(x: int) -> str: passType hints for parameters and return.