Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

README.md

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