Generators are functions that behave like iterators, thus can be used in for loops. For example, a generator that produces even numbers will look like:
def even_numbers(upperlimit):
current = 0
while current < upperlimit:
yield current
current += 2
Generators are also created using a syntax similar to list comprehension:
even …