18 lines
526 B
Python
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
|
|
[]
|