Generator Definition

In Python, a generator is a function or expression that "generates" items to loop over. For example, you could write a generator that would let you iterate over all of the perfect squares. Generators save memory because you don't have to compute the elements to loop over first. You just need to create a function that will compute the items to loop over.


This video covers two types of generators in Python: generator functions and generator expressions. Generator functions use the "yield" keyword, while generator expressions look like a "tuple comprehension."