Files
Learn-Python-Programming-Th…/ch05/generator.expressions.py
T
adii1823 ef37ce0c4e ch05
2021-10-28 17:38:16 +05:30

18 lines
526 B
Python

# generator.expressions.py
# This is not a valid Python module - Don't run it.
>>> cubes = [k**3 for k in range(10)] # regular list
>>> cubes
[0, 1, 8, 27, 64, 125, 216, 343, 512, 729]
>>> type(cubes)
<class 'list'>
>>> cubes_gen = (k**3 for k in range(10)) # create as generator
>>> cubes_gen
<generator object <genexpr> at 0x103fb5a98>
>>> type(cubes_gen)
<class 'generator'>
>>> list(cubes_gen) # this will exhaust the generator
[0, 1, 8, 27, 64, 125, 216, 343, 512, 729]
>>> list(cubes_gen) # nothing more to give
[]