In this tutorial we will talk about Paul a use for iterating over a set of statements repeatedly in
Python.
There are three types of Lew The for loop while and do y Lew a for loop is used for either dating or
what a sequence such as this a string a tuple or addiction.
And this is the syntax for the for loop for variable in sequence and we have a set of statements after
the colon so if you want to either iterate or what each of the characters in a string you can use the
for loop.
So here I have used a far loop to either did or what a string for i in Python.
And then like we do for it and if statement or the if clause use goal in here and then I have a print
statement indeed for loop as you can see we have indented the statement in default loop like we do for
it.
And if clause with the for loop we can execute a set of statements for each item in a list.
Tuple set out a string.
Now let's consider the second example here in this example I want to I did it or would a list.
So this is the statement that I'm going to use for x in this particular list containing the elements
1 2 3 4 5 so for all elements in the list I want to squared each of the items and then print the result
here.
So for each item in the list this action is performed.
And then finally it this printed the items of the sequence object are assigned one after the other to
the loop variable that is in case of our example the item in the string that is B and Y.
D hedge or n are assigned to the loop variable ie in a sequence first it points to the element B and
then it goes to Y and then t h and so on so to be precise the variable points to the items and for each
item the code in the for loop that executed.
So in the second example the loop variable X points to 1 first and then it moves to the second item
2 and then 3 and so on but let's look at two examples in the Jupiter note so in order to iterate over
the elements in the string I'm going to use the for loop.
And finally I'm going to print all of the elements in the string so let's execute this cell.
So the far loop has used do loop bearable to pick each of the elements in the string and then in the
body or the for loop we wanted to print each of these elements.
Now let's look at another example.
The for loop is going to pick each item in the list and apply the same set of statements for each item
in the list.
So another example it's going to pick the item one graded and then print the result it's going to Big
D second item two squared in number two and then print it as well.
And so on.
Not let execute the cell so as you can see the same set of statements have been applied on each element
of the list.
In the second example I have passed the Range object to the list constructor and using the for loop
I am going to iterate over the D sequence of items in the list fort I in list and I have passed the
argument range of 10 and in the for loop body I'm going to put in the items in the list.
So after I execute this said for loop has printed the items in the list beginning from 0 to 9.
Coming back to the for loop for loop can have an optional X block both related with it and the 8 block
executes only when the for loop has executed all the iterations.
So in this example I have used an alias block along with the for loop and the L statement is executed.
Then the for loop has exhausted either dating or were the items in the or the element in the string.
The IDS blog it executed with de loop I iteration complete normally.
So let's execute this piece of code in the Jupiter notebook.
Here in this example we are using an edited block with a for loop using the loop where it will go into
irate all the items in the trim print the items and after I iterating all over all the items in the
string the control goes to the else blog and the statement in the else block executed.
So when you execute this piece of code you have the items in the string printed first all of the items
and finally the print statement in the edit block is printed.
Next we're going to discuss about nested loops for loops present inside and not good for loop then it
does call a nested for loop when units do lose the outer loop takes control of the number of complete
iterations of the inner loop.
So let's understand this
so here is an example demonstrating nested loops.
So this is loop and Mike loop I've used a range function to generate the numbers and this range function
is going to generate the numbers 0 1 and 2 next.
This is the inner loop the inner loop I'd read or would a string containing the characters a B and C
so the inner loop has a print function that brings each of the elements.
And finally after we come out of the inner Lew I have given a print function for every single ideation
of the outer loop several ideations of the inner loop performed for one ideation of the outer loop.
The dire cycle of ideations is completed in the inner cycle.
This piece of code meaning the inner loop print the elements a b and c.
So this completes one cycle of the inner loop.
And here I have given a print function to show you that the outer loop has completed only the first
ideation.
So this brain function is going to print this text like this zero times and then the control goes back
to the inner loop again.
The second cycle again print a B and C and now control comes to the outer loop one time
and then so on.
So this is the first cycle.
Second cycle and we have three such cycles like that so let's execute this piece of code in the Jupiter
notebook.
So as this goes to earlier I have used two loops to demonstrate nested loops for one cycle.
That is for one complete iteration of the inner loop.
One ideation of the outer loop is performed let's execute this piece of code the inner loop has rendered
the each of the characters in the string ABC.
The first itemization of the outer loop print this text next.
The second cycle.
Comprising of three ideations of the inner loop is executed and then we have the second by iteration
of the outer loop.
Printing this text next again the third cycle of the inner blueprint.
This text A B and C.
And finally we have the outer loop printing text here which is the final iteration.