FAQ: Generators - next() and StopIteration

This community-built FAQ covers the “next() and StopIteration” exercise from the lesson “Generators”.

Paths and Courses
This exercise can be found in the following Codecademy content:

FAQs on the exercise next() and StopIteration

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

so I have a question about the lesson material for generators. What exactly happens from start to finish in this code? Does the for loop iterate first and then does the yield statement act as a stopping point? How is the function getting what it needs to run? From the generator, if from the generator are the values from the yield statement is what is given to the function to run? Or is it generating numbers or code up to a certain point?

I am really confused.

Link to lesson material: https://www.codecademy.com/courses/learn-intermediate-python-3/lessons/int-python-generators/exercises/generator-functions-next-and-stopiteration

Generator functions return an iterator object that

contains traversable values. To retrieve the next value

from a generator object, we can use the Python built-in

function next() which will cause the generator function to

resume its execution until the next yield expression is

found. After the next yield expression is found, the

function will pause execution again.

(next(standing_values))

When a generator calls yield, it is momentarily passing

the control back to the code looping over generator values.

In a foor loop you pass control to the generator which

yields a value back to you.

This process goes back and forth back and forth until

the generator has run out of yields.