= Generators = https://wiki.python.org/moin/Generators == Introduction == === Generator Expressions === {{{#!python # list comprehension doubles = [2 * n for n in range(50)] # same as the list comprehension above doubles = list(2 * n for n in range(50)) }}} === Generator Functions === Before: [[http://www.pythontutor.com/visualize.html#code=def+squares(n)%3A%0A++++result+%3D+%5B%5D%0A++++i+%3D+0%0A++++while+i%3Cn%3A%0A++++++++result.append(i*i)%0A++++++++i+%2B%3D+1%0A++++return+result%0A%0As+%3D+0%0Afor+i+in+squares(10)%3A%0A++++s+%2B%3D+i%0Aprint(s)&mode=display&cumulative=true&heapPrimitives=false&drawParentPointers=false&textReferences=false&showOnlyOutputs=false&py=3&curInstr=0|Visualize in Python Tutor]] {{{#!python def squares(n): result = [] i = 0 while i