Generators and Iterators
Generators are a type of iterable like lists or tuples but unlike lists, they don’t store all values in memory. They yield items one at a time using the yield
keyword.
def countdown(n):
while n > 0:
yield n
n -= 1
for number in countdown(5):
print(number)
Iterators are objects with __iter__()
and __next__()
methods. Generators are a simpler way to create iterators.