In this situation, we're discussing python generators.
So iterators in Biton incurred loss of overrates, implementing a class with underscore, underscored,
itr, underscore, underscore method and the underscore, underscore, next underscore, underscore
method and then keeping track of internal stiff's race stop iteration when there was no values to be
returned, etc..
So there's too much overhead in Kessab iterator so that things will be minimized, that overhead will
get minimized in our generators.
So this is both lyndy and counter intuitive.
So generator comes with more simplicities.
So what are the simplicities we're going to enjoy in our generators?
So Biton generators are a simple way of creating iterators and all the overate we mentioned above are
automatically handled by the generators in our Biton coding.
So simply speaking, a generator is a function that returns an object that is the iterator object,
which we can iterate over one value at a time.
So this is the simplest way to handle that.
That is pretty iterator object.
Difference between the generator function and a normal function.
What is the basic difference between a normal function and a generator function?
A generator function contains one or more statement.
When called, it returns an object that is iterator, but does not start execution immediately, methods
like your underscore underscored Peter underscore, underscore, method and underscore, underscore
next underscore the underscore method are implemented automatically in our generators so we can iterate
through the items using next.
So not that very one that is underscored on the next underscore.
On the second method, we can use this next method.
Once the function is, the function is paused and the control is transferred to the caller and local
variables and their states are remembered between successive calls.
And finally, when the function terminates, stop iteration is raised automatically on the father column.
So here to see in case of Iterator, we're having one very special statement that is known as that you
stickman, which will accumulate all these respective values which will be obtained from these iterations.
So I think for the better idea, let us go.
For one example, during the explanation and during the writing of the coding will be going for further
explanation, for better understanding.
So just to watch the demonstration of.
In the previous video where we have discussed this iterator, there is an iterator feature in our Python
programming.
We have seen that we are supposed to deal with Huze of what it was supposed to define the ITR method.
We are supposed to define the next method.
We are supposed to keep the track of the internal states.
There is the stop iteration when there is there are no values to be returned and so many different issues.
And this is our code, more and more lendee and counter intuitive.
And Jeneda comes into to rescue in those situations, python generators are a simple way of creating
iterators and all the overate we mentioned here in our discussion will be automatically handled by the
generators in Python.
And simply speaking, a generator is a function that returns an object that is the iterator object,
which we can iterate over.
That means we can return value one at a time.
So it is fairly simple to create a generator in Python and it is as easy as defining a normal function
with the E Teckman instead of having the return statement.
That difference is that while a return statement dominates, a function internally terminates at that
very statement.
But Inca's of each statement.
Thus the statement pauses the function, saving all its debts and later continues from there on successive
cuts.
And that is the basic difference between the return and the statement.
So let us go for this practical example.
For the better understanding here.
You have defined one method that is a mind generated in Israel.
One, this is printed first ild n.
So this particular state is giving is getting saved here.
So in blast equal to one print, this is printed second ild n and then we're having this in place equal
to one.
So N is getting increased.
Now it has got the value three here because initially it was having the value one and next, next time
it has got increased by one.
So and so now the N has got the value to.
So any Segoline plus one the value up in has become three.
This is printed at last and ild and is there.
So here you see instead of using the return statement to abuse the instrument, not what is the purpose
of this.
So now using this particular method, the return we are sending to this e so e nothing but one iterator
object.
So to have to, to access the elements from this eight at using two is one using one for loop and another
one using the next method.
So next of a so let me go to code so you can get output.
So next up it is printing.
This is the printed first.
Next A it is printing that this is printed sergant next it is printing that this has printed at the
last note.
We are going for this using for loop.
So this particular message has got printed here.
So now iterating using the for loop here.
So for item in, I'm just calling this method that is my underscore generator method.
You are calling and it is returning that their respective iterator.
So print item.
So I'm going for this, I am printing this item here to add having this for each loop.
So each and every time it will retrieve the next element from this iterator which is being returned
by this my generator method.
So it is printing in this way.
This is printed first one, this is their second two and this is printed at last.
That is the battery.
So in this way, you can find that in this generative method I didn't discuss, I didn't find any other
method or next method.
The iterator is getting handled automatically by this generator.
So let us go for another example for the more clarity so generated with the loop.
So here do you find one method?
There is a reverse string which will take this my string as input parameter.
So Lindop that might string will be will be kept in length where we use a method linear.
So for I in range so lenth minus one it is.
Suppose I have given this particular string.
There is a word.
So how many letters are there in this world without having this.
Five letters.
So the length will be five here.
So.
So now minus one means what.
So that is our fault.
Minus one.
We know that whenever we were having this range so it would reach up to zero zero four to zero with
the step minus one.
So this is our step minus one.
So for Basilicata range and because that's why I've given here that the next parameter as our minus
one to this French method.
So now here this Leontes, I mean, of five, so five minus one is for four to zero with the minus one.
And then the respective characters will be added from this my string, and that is we are returning
it using this ILD.
So now we are just accumulating that, returning at the return sets of this particular method.
That is the reverse string.
So now if we go for Fourcade.
In a reverse string world, and if you go for the sprint card, you see the reverse string is actually
returning one iterator and this iterator from this integrated retrieving character by character, using
this for each loop, using this Falu and we are printing this deal are all w there is a reverse of the
that is a character's reverse order of the characters existing in the world in the world text.
So in this way, I've shown you that there is no need to handle arrays, any stop iteration.
There is no need to handle this next method or over this or define this item method.
Everything will be handled by this generator and there is a nice way to handle our in Python.
Thanks for watching this video.