Generators are a type of iterable, like lists or tuples.
Unlike lists, they don’t allow indexing with arbitrary indices, but they can still be iterated through with for loops.
They can be created using functions and the yield statement.
while i > 0:
i -= 1
for i in countdown():
Due to the fact that they yield one item at a time, generators don’t have the memory restrictions of lists.
In fact, they can be infinite!
for i in infinite_sevens():
Finite generators can be converted into lists by passing them as arguments to the list function.
for i in range(x):
if i % 2 == 0:
[0, 2, 4, 6, 8, 10] >>>
CLICK ON THE BUTTON BELOW TO GO TO THE PYTHON MAIN COURSE PAGE.
©️ License: All Rights Reserved
CONTACT HOLLA TECH – LEARN SUPPORT