Skip to content

Latest commit

 

History

History
26 lines (17 loc) · 782 Bytes

File metadata and controls

26 lines (17 loc) · 782 Bytes

Generators

Memory-efficient iterables that produce values one at a time using the yield keyword. See Generator (computer programming) — Wikipedia.

yield vs return

  • return sends a single value back and ends the function.
  • yield produces a value and suspends the function, resuming from the same point on the next call.

Why use generators?

A list stores all values in memory at once. A generator computes each value on demand — essential for large or infinite sequences.

Covered

  • Generator function with yield
  • Generator expression (round brackets instead of square)
  • Large range processing
  • Infinite sequence
  • Infinite Fibonacci sequence

Run

python generators.py