Generator
An infinite list cannot be enumerated because of the limits of ram. But we could use the generator to identify the principles of list generation.
Note: use the generator as the List Comprehensions but replace the [] by ()
l=[x*(x-1) for x in rang(100)]
g=(x*(x-1) for x in rang(100))
l is a list while the g is a generator
how to call a generator
- the function next() can help you obtain the next value of the generator
- you can also obtain the value by for loop