Hi and Hello, everyone. This is your facilitator commander for the course, Python generator and yield statement. This video is about giving solution to the exercise that we discussed in, in the course. And I'll be explaining the steps involved in solution to the exercise. Okay, the question was write a generator function which returns the fiber Nikes series, they are calculated using the formula the first number of the series is equals to one. And each protective number in turn is the sum of the last two numbers.
So if we just visualize it, the first two numbers are going to be one that means the first number is one second number is one. The third number is going to be the sum of the first universal it is one plus one too and saw the Instead, we're given where was the first hint was we can use two variables in the five, five minute key generator function. So there would be two variables to solve this. Let's suppose a and b. And the next one was assignment can be done simultaneously so the values can be assigned simultaneously to these variables. So how it would be?
Let's discuss this it's logic. Let's suppose that we select a beans variable and we assign the values one comma one, a comma b equals to one comma, one. Okay? Then Then we're going to look through to add the values Now, what we are going to print First, we would be like printing or we would use Eve because we are using a generator function, we are going to yield a variable that has the value one, and then we're going to give it another value. The next number is also going to be one, so we would need to give it like a comma b equals two. Now, when it loops through the value of Abel remains the same initially.
So let's sign this B because b also has the value one and the value of b needs to be added. So we gave it a less feel so by this The value of B would be changing and it would add in to it whereas, a would be equal to b and b initially is just one. So, with this, we get the first two numbers as one because A is one, then a would become B, which is also a one, but B would become a plus b that is actually one plus one two. So in this case, when it loops through, it would give one then one and then two, then two plus one three and so on until the loop continues. Okay, here I have given, I have defined this function for you. I named it 500 key series generator.
The values are a comma V and they are assigned one comma one. Now why choose one Through shield a and then a comma b equals to b comma A plus B that means a has the value B which is also one. So, the next time I did manage to loop through a would still remain what the first time it is printing one second time in still it is printing one and the third time the B has become a plus b that is one plus one to the third time and when the a print, it has become actually two. So, it continues like this. Let's test Okay, let's run this code control and I run this code with the next slide. No less test this function Okay.
Okay. In order to test this function, I'm going to import the types library. Once imported, I'm going to test it. So if type function, we're going to take, check the type of 580 series generated function and compare it to type storage in data type. So if it is true, it's going to print the five in a key series. Signature native function is a generator.
Let's test this again. Yes, it is. Sure. Okay, no, this is the counter, we're going to use the five minute key series generated the counter. And we're going to test this for first 20 numbers. So what I'm doing, I'm using the for loop with the five series generator.
And, and writing the counter by one, if count is 20, and we break it. So it's a 20. I'll learn with first five numbers just in a minute. So you see, the first number is one, the second number one third number is the sum of the first number. Four number is the sum of first three numbers. First of all, the last two numbers, and the fifth part is the sum of last numbers.
So if I did this to 10 Okay. So, this was a simple exercise we use while for loop we generated the Fibonacci series generator function. And he tested it if it is the correct generator function and then we executed that function in to follow to generate results. And we got the results. I hope, the exercise, all the explanation and everything got you moving forward with potential greater may you have any queries or questions feel free to contact me or if you're looking to get connected, you can contact me on th e.ku M ai@gmail.com Thank you