Search notes:

Python: generators

A generator creates iterators. That is, a generator creates the __iter__ and __next__ methods and raises the StopIteration exception automatically behind the scenes.
In order to define a generator function, the yield statement (or the equivalent(?) yield expression) is used.

Generator expression

A generator expression is basically an expression followed by a for and optional if clause:
>>> gen_expr = ( math.sqrt(i) for i in [3,-2,5,6] if i >= 0 )
>>> type(gen_expr)

<class 'generator'>

See also

iterators

Index