In this tutorial we will talk about generators.
Generators in Python are just another way of creating.
I did a bill objects and they are usually used when we need to create a digital objects quickly without
the need of creating a class.
Implementing the idea data protocol.
Just like we do for I.T. raters and in order to create a generator we just need to define a function
and then use the key word and instead of the return statement
including the yield statement in a function automatically signals to Python that this function will
create a generator in order to create a custom identical object using either it does we define a class
and then implement the special methods I did and next coming to a generator function we define the generator
function using the def key word and the genitive to function accepts parameters
and instead of using a written statement in the generator function we use the yield statement the yield
statement is similar to a return statement in a normal function but whereas a return statement causes
the current environment to be destroyed after the function exits the statement.
Suspend these functions execution and sends a value back to the caller but it retains this date to enable
the function to resume vetted left of and when the function continues execution.
Immediately after the last yield run.
So this allows the its code to produce a series of values or what time rather than computing them at
once and sending them back like a list we should use the yield statement when we want to either it or
word or sequence but don't want to store the entire sequence in the memory so we will discuss two examples.
The first example is a normal function which is going to return the value of the constant squared and
then we also have a generator function.
Here you can see the yield keyword has been used in this function making it a generator function.
So we will first execute did not mean a function and then we will make a function called so when we
define a normal function and call this function the code inside this function gets executed immediately
but coming to a generator function let's see what happens.
So we will first we have defined the generator function.
Now we will make a function call so we have made a function call and assign this function call to a
variable lit display this variable in so when we call a generator function it will not immediately execute
the code in the function but instead it will return a generated object.
Now if you want to execute the code inside the generator function you need to call the next function
or on the generator object.
So let's parse the object just to then execute the new call the next function for the first time on
the generator object.
Execution begins at the start of the function body and continues until the next statement.
And the value to the right of the evil statement is returned and this evil statement inside the function
causes the environment to be saved until the next time the next function is called so that the generator
can keep track of the ideation st so in this example we are not actually generating a sequence.
This is only to show you the difference between a normal function and a generator function so when we
call the next function on the generator object again since there are no widely used to be computed the
stop iteration Edward is raised so this informs us that all the values have been yielded the special
methods ITER and next are implemented automatically so we can either through these items generated using
the generated object using next so the generator function has a lot cleaner code compared to an idea
later.
Let us now consider another example.
And this generator function has multiple yield keyword module statements.
So we need to first create a generator object and then in order to execute the code inside the generator
function out in order to compute the sequence generated by the generated object.
We need to call the next function which gets implemented automatically so let's parse the generated
object to the next function and the first statement and about generator function is an E statement which
is going to return the value of the integer value 1 and we know the E statement inside the function
causes the environment to be saved until the next time the next function is called so the function is
paused after returning the integer value one from the first e statement and then after yielding this
value the function is parsed and the control is transferred to the call.
Now once again when you call the next function on the generator object the execution picks from Betty
Previously executed yield statement.
Left off so the execution begins from the second e-mailed statement and this continues until the next
yield statement is encountered so the second yield statement is going to return the integer value to
and the function is passed.
The control is transferred to the caller.
Now when the call the next function again the execution of the function begins from detoured yield statement
which is going to return the integer while you three and they function is again post.
So this is the generator function that we have discussed about.
So we are going to execute this generator function.
We will first create the generator object so days to underscore or b g is a generated object and we
will pass this object to the next function so when we call the next function for the first time the
first statement in the function body which is the evil statement has returned the value 1 and the function
has been temporarily suspended and the control is given back to the caller.
Now let's call the next function again.
When you call these next function for the second time the function is resumed where it left off.
So the function execution begins from the second statement in the function body which is the yield statement
again and this is returned the integer value to the function gets temporarily suspended and the control
goes back to the caller.
Now let's call the next function for the third time and the last e to statement as eternity the integer
value.
3 No let's call the next function again because there are no values to you from this generated function.
The next function has raised a stop ideation error and here here's another example earlier we have generated
an edit medic sequence using an AI data.
In this example we are generating an added metric sequence using a generator.
We are using a counter in this example in order to keep track of the number of terms to be generated
in the sequence so let us go ahead and create a generated object let's pass the argument the first two
as one.
The number of terms to be generated as some 5 and the difference between the consecutive terms should
be three sigma this.
Now let's call the next function on this object.
So when you call the next function on the generated object the function body gets executed.
Under the first yield statement is encountered.
So we have initialized accounted with devalue one in order to keep track of the number of storms that
are getting generated.
Now since the value of the counter is less than the number of terms to be generated the while loop gets
executed and the value in the arithmetic sequence is generated using this formula and this value is
yielded so that is the value one year.
Now let's make a call to the next function again so subsequent calls to the next function continue from
the Eedle statement to the end of the function and loop around and continue from the start of the function
body until another field statement is called.
When you call the next function for the second time the execution begins.
From this point so the counter is incremented by 1.
We're going to print the value of this counter which is 2 and then since the value of the counted is
less than the number of terms to be generated with just 5 we're going to calculate the next value in
this sequence.
Using this formula and then by yielding this value so subsequent calls to the next function continue
from the yield statement to the end of the function and loops around and continue from the start of
the function body until the Elan statement is encountered let's call the next function again the next
value in the sequence is 7 and the counter has been incremented to 3 calling the next function.
Again the next value generated in the sequence is 10 to 14 because the counter cannot be greater than
the number of terms to be generated.
The total number of terms to be generated is five after generating all the values and if there are no
more values to be generated the stop ideation method is being raised.
And this is about generators in Python.