Generator Simple Examples

5 minutes
Share the link to this page
Copied
  Completed
Simple generator example with discussion on how it works

Transcript

The first example that we are going to use is very simple. And it generates number one by one. Let's see this, I defined the generator function and it equals to one, and it is going to print this is first and it is going to result. And so that is going to be one. The next time, the function the Generate function would be called, it is going to start from this point because this is the feature of generator and it's going to add one more into n it is going to be two and it has been introduced that and so on. And let's do this I defined the really boom operation return and assigned the generator function to it.

Let's run this. Okay. We actually we did not execute this generator functions. So we need to execute this first so that the notebook knows that it is already defined when I executed it and move to the next slide. Now, it gives me my chain is not defined. So, the notebook did not know about that generator function was already defined, because I did not run it.

Now, I'm going to run this line of code and it is going to assign the value. Now, when I call this we build within the next function it is going to execute, and it produces one. Next time it executes, it produces two and the next time it executes, it produces three. Now the all the function has come to an end. So it could give me certification error. Yeah, get it this so Let's move to next exercise.

So you see it is very simple generic function definition, our syntax is very easy to define. It's normal that normal function that we defined in Python, just the yield keyword makes it a generator. And the has a very specific purpose that it produces the output and then stops function and the next end function runs which is that the execution of our code after that yield statement. Another example this is also simple a bit but the next level simple it's a level up. Now we have clear idea of what was happening in the background how the function and oxygen rate function works, and what are its use cases. Now let's run the generator function with the loop having the switch was terminated.

Terminating condition. This is the content function that we are defining. The name of the function is countdown it takes the variable num in it, it begins countdown begins, it prints countdown begins. And then while num is greater than zero it it yields num. And then it subtracts minus one from it. So it is going to continue running until the number becomes zero.

So what is it is going to take it as an input is going to be a number greater than, obviously zero. And it is going to continue producing the results until the zero is reached. Let's execute this line of code. And now I define another way up and pass this common function in it. It would be boo With the number seven, so it is going to run seven times and produce a result. Let's run this line of code and the next line.

Now when I print this the result of an A within the next function, let's see what output creates. gonna begin seventh. So we're going to go down six five. Now, let's loop over all the results for so I defined i in range five, and I'm going to print next countdown underscore a and let's see what result it should produce it produces 4321 and then stopped it did not produce any results and it gave an iteration edited. The fifth value in the range has been becomes zero and this zero should not execute because the condition of the wind has turned false. So, it should have produced a truck stop nutrition error and it did I intentionally used five to to show you this.

So, how simple and clear when the use cases the purpose the examples are taking you and explain it to you step by step. Let's move to next exercise

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.