generator expression in python code example
Example 1: generator expressions python
>>> even_squares = (x * x for x in range(10)
if x % 2 == 0)
Example 2: python generator
def grepper_gen():
yield "add"
yield "grepper"
yield "answer"
grepper = grepper_gen()
next(grepper)
> add
next(grepper)
> grepper
next(grepper)
> answer
Example 3: generator expressions python
>>> iterator = ('Hello' for i in range(3))
>>> for x in iterator:
... print(x)
'Hello'
'Hello'
'Hello'
Example 4: generator expressions python
>>> genexpr = ('Hello' for i in range(3))
>>> list(genexpr)
['Hello', 'Hello', 'Hello']