>>> mygenerator = (x*x for x in range(3))
>>> for i in mygenerator:
... print(i)
0
1
4
# generators
gen_1= (x*x for x in [1,2,3]) # [1, 4, 9]
gen_2 = (x+x for x in gen_1) # [2, 8, 18]
for i in gen_2:
print(i)
# 2
# 8
# 18
for i in gen_1:
print(i)
# Nothing is displayed
# With generators, instead of storing data in the variable gen_1 (and therefore in memory), you are going to generate them on the spot (e.g. only when you need them).
# Careful: Generating data on the spot does not allow to read them several times. And if you try to do so, no error will be raised to warn you.