Hello welcome to this material on I traitors and generators before jumping into traitors and generators.
We will first discuss about.
I did a bit and we have worked on a number of item builds in a previous tutorials.
So what is an item.
And I did a bill is an object that can be looped or word using a for loop.
All I tribals have the magic method double underscored ITER method defined and most of the built in
containers in python like Lists Tuples strings set dictionaries and files and generators.
Add examples of attributes.
Let us now iterate over the elements in a list using a for loop.
So in this example we have defined a list with four elements default loop retrieves each item from the
list perform some operations and displays the output and when they for loop reaches the end of the list
that is if there are no more items in the list.
The for loop gets dominated.
So let us execute this code in the cell.
And here is the output.
Now let's see another example.
So we have defined a dictionary dict 1 and using a for loop we want to loop over the keys that are defined
in the dictionary.
So using the for loop here we will traverse over the keys in the dictionary.
So let's execute the code in this cell.
So the for loop has retrieved each of the keys in the dictionary and then displayed them in the output.
Next we can also I did it over the content in a file object using a for loop.
So here in this example we have first opened the file and then using the for loop we are going to read
the contents of the file line by line and then return the same.
Using the for loop so let's execute the code in this cell and see the output so the for loop read the
file line by line and then returns the same.
We have also seen a number of examples maybe used a for loop to iterate or what the sequence of numbers
generated using the range function.
The range function returns a Range object which again is an item.
So we can use a for loop to iterate over the sequence of numbers.
So let's execute the code in this is so the for loop has either dated or what the sequence of numbers
generated by the range function.
A while loop can also be used to loop or word and I durable and this action the ideation continues until
the test expression in our example.
This is the test expression so until this takes expression becomes false at this point the program execution
proceed to the first statement after the loop body.
So we have defined a list here with three elements and we want to iterate over a loop or what the elements
in the list.
Using a while loop then the while loop reaches the end of the list the program execution should be terminated.
So for that we have this condition here and in denote the index number of the list that has X denote
the length of the list.
So let's execute this piece of code.
So the while loop as I iterated all the elements in the list and then display the items next.
The membership operator in can also be used to loop or would I dribbled say for example you want to
check if the integer 1 exists in the list containing the elements C a string a and then floating point
number three 2 at Ford and then string again and then the numbered or the integer 1 so let's execute
this piece of code.
In this example the in operator has either rated or what the elements in this list not to check if the
integer 1 exists in the list here and it has returned true.
So we have seen how we can use a for loop to either it or word a number of containers built in containers
like lists dictionaries etc..
Now let us try to.
I did it or were an integer object.
C for x in 0 5 int x.
So here Python has stored a type error seeing integers.
Object is not ideal.
So the for loop.
Can I did it all would I durables only and not any other object.
So what makes an object and I travel all like the rebels shared a common thread which is they implement
the double underscored ITER method the Dunder method or the magic method ITER is implemented by all
AI durable objects.
So basically a neat object within a tenable method is called an item in this particular object can be
used in a for loop and like with most magic method we cannot call the double underscored I did method
directly if you want to call a method defined in a class on an instance you can use the dot operator
but most magic method should not be called directly.
Instead we can use the function ie the e
the idea function in done called the magic method w underscored ITC.
Now if you call the I do the function and pass and I did a bill like a list.
This is it don't and I iterated.
So what is and I did don't.
So what does an I did it a night to day did is an object which represents a stream of data and a night
the data allows us to.
I did it or what the contents of a container a nitrate that as an object in Python that behaves something
like a list so it is a collection of values one after the other.
And the item data object can be invoked or were to produce a series of values and someone using the
double underscore next method but then I did it that object differs from a list in that the values are
not stored in your computer memory.
Instead they're calculated on the fly which saves us a lot of memory.
Say for example when you define a list or a set dictionary to pull out a string in Python this action
allocate a specific block of memory so all these data structures live in the memory and also hold all
their values in the memory.
But coming to AI traitors they do not compute the value of each item when instantiated the line used
in a CDO or the sequence are computed only when you ask for it.
And this is known as lazy evaluation.
And lazy evaluation is useful when you have a very large dataset to compute.
It allows us to start using the data immediately while the whole dataset is being computed so it allows
us to manipulate large amounts of data while keeping a very little bit of actually in the memory.
So I did does act like this but they do not take up space so a Knight-Ridder looks like a sequence when
we use or generate items or elements from it.
But there are some major differences so if you want to construct a sequence like object.
So in order to construct a sequence like object you need to write a function.
Every element of the custom either data object is actually produced only when we ask for it.
And then unlike the sequences or I troubles and I iterator sequence can be infinitely long.
So they can generate while use which infinitely long
in python I did it those that implemented as classes say for example you want to build a custom.
I did it object that looks like a sequence and this object generate infinite numbered all values.
And for that you need to construct a class let's see the class name is best and then you need to define
do method in this class.
One is the double underscore either method and the other is double underscored.
Next method.
So in order to define a class which is an idea to build the class must implement these two methods the
ITER method and then next met the SO OFTEN defining the different methods in the class we create an
instance of this class test.
Say for example our instance is object or B J one which is an instance of this class test.
Now next you want to generate a series of values.
Now if you want to generate a value from and then find entities like this we should call the magic method.
Next on the instantiated object in order to generate a value so we need to use the next function instead
of directly calling the magic method.
Double underscore next.
So we will call the next function and parse the instance of the class or b 1.
The next function or the next method that you have to find in the class has logic.
In order to compute the first while you in the infinite sequence and this will return the first element
in the infinite sequence which is 1.
Now if you want to generate the next value in the sequence we need to call the next method.
Again using the next function and pass the instance to this function the next method that you have defined
in the class compute the next value in the sequence and returns forward so we can loop all word custom
iterator objects using the double underscore ITER and then double underscore next methods.