Memory-efficient iterables that produce values one at a time using the yield keyword. See Generator (computer programming) — Wikipedia.
returnsends a single value back and ends the function.yieldproduces a value and suspends the function, resuming from the same point on the next call.
A list stores all values in memory at once. A generator computes each value on demand — essential for large or infinite sequences.
- Generator function with
yield - Generator expression (round brackets instead of square)
- Large range processing
- Infinite sequence
- Infinite Fibonacci sequence
python generators.py