Tip:
Highlight text to annotate it
X
The answer is 0.
The reason is because calling gensquares on 11
actually made this object again.
So we've sort of reset everything and calling next just gives us our first item, zero.
If we want to keep incrementing--0, 1, 4, and so on--
we'll need to make an assignment.
Now we've assigned G to this generator object by calling gensquares.
I can find next--0, 1, and so on. Just like we expect.
We decided that the function was too messy to solve our original problem.
List comprehensions were okay, but used a bit too much memory.
The best answer was really the generator.
We showed how to use a generator expression and how to use a generator function.
The generator is a really powerful tool to add to your arsenal of Python techniques.
I hope that you understand it a little better than you did before.