Generators are perfect for reading large datasets since they yield out that a single chunk at a time irrespective of the size of the input stream. So it can take in like a huge amount of data at a time, and producers reserves one by one. They can also result in cleaner code by decoupling the iteration process into smaller components. So the iteration runs not all together at once, but it can be run in smaller chunks. And we can do this by yield keyword within the generator function because whenever the code reaches the yield keyword, it produces the output and then stops. So, genitive work great for web scraping and crawling recursively.
For example, if you're looking to scrape For example, 50,000 URLs and we want to divide this into chunks of hundred we can easily do this using the yield keyword Now let's try. Now I just created the definition of generator function. And I created a list that has three members, one, two, and three, four eel in our yield em. So what it does, we already know the definition of generator, it stops at yield and it produces one and after that the stop the function stops. The next time This function is run, it is going to start again from this point, it's going to enter the folder it is going to produce you now this time it is going to be two. And the next time it is going to be starting again from the for loop.
And next time it will be producing three. Let's do this. It just and the god code and the code Then a degenerate variable and give it an assignment to this function. Now checking the type. Since the yield keyword is used within this function, it makes it a generator. So when I check this type, it is going to tell it's a generator.
So Python Python engine recognizes this as a generator. Now let's run this code. The Jen Jen bird that has a generator function within it, when called The next is into the first result that we estimate is going to be one. This is one. The next time when we run this function, it is going to produce two and another time when we use this function. It produces three now on three results have been produced, what would happen next time if we run this code what what happened next time when we run this code it is going to give a stop iteration error it is going to tell us the iteration has stopped the whole iteration is no more available the whole code has already run.
In this way, we come to note the whole generator function has run. The next time Next is called in the generator iterator it is the next step in a for loop. generator assumes execution from where it is called He'll that we already discussed it starts whenever we call the generator function within the next function it starts from where it was left the last time and when the whole equation is over it is going to give this error function start again we need to actually start from the line of the code where we define the generator function. So, the yield statement tells the Python engine that this is the output they are looking to get. And it does not start from the beginning again when this function is called unless the whole execution of the generator is over. And we control this using the next function