Welcome to this lecture in this lecture we're going to learn about the looping concept.
So first of all what is loops or loops that is going to be iterate over a given sequence.
So we provide the data set and looping is basically it is going to loop until and unless the condition
is met.
If the condition is not met if it is at the end of the data set then looping will be indeed.
So let's see the loop index and so first of all let's make use of the brain's funds and so lists as
so this end up 5.
And let me show you if I see a transmitter.
So this is the inbuilt Python method so it should be rings.
And if I say five.
Okay so stop point would be five.
Then it is going to give us a tuple from 0 to 5.
So just remember that range is going to give us a tuple from the starting point to the endpoint.
So if you just provide a single letter a single argument then it would be they stop point if you provide
the two argument let Mrs. Raines and Ford do it.
It is going to give us from the starting for to end it.
So this is the Raines matter will make use of this transmitter now.
So first of all let me say you something like Ok so first of all if you want to print your name also
start from the basics.
So if you want to print your name and that this is a way to print.
So let's save this and execute.
So you can see the search in the sprint over here.
But if you want to print these multiple times so let me show you that if you want to print these maybe
10 times.
So all you need to do is simply copy paste copy paste although it would be overhead but all you need
to do is copy and paste.
But if I told you to 20 times or 30 times or a thousand times so this would be tedious tasks you have
to simply copy everything and paste it over again and again although this is quite boring tasks but
you need to do this.
If you save this place you'll be getting your output but it will dict sometimes.
So this can be done by the loops.
So let's see.
So loop will be executed from this for this is the keyword basically so far all in lowercase.
So I'll say I for I use basically any variable in keyword is needed in the rains.
So Raines is going to give us a tuple or a list and let me sit down over here and give it a column.
So Colon is important.
And after that it will get some indentation if you just press enter from here all into these pages press
enter it will just give a certain portion of is indentation and he loses a to name or William
if I just saved this simply survive.
It is going to give us stem times such in here.
So what we just did here was with this loop in this and so what this Raines 10 is going to give us is
let me show you over here.
So Raines standing is going to give us all sometime like is a tuple and it starts from zero one two
three four five six seven eight nine is the last one would be admitted over here if it is five year
11.
It would be 10 that means loving will be eleven times the loop will be executed.
But for now we have ten.
That means it will be executed ten times from zero to nine.
So first of all this I value will be zero.
And the next time it will be one end to this loop until we reach over here.
And after this we don't have anything that means the loop will be ended and such and will be printed
only there 10 times.
If you want a hundred times that should be hundred over here less comment this out why this has saved
this place and five you'll see the hundred times in the business splintered over here.
So instead of the sprinting your name on the times you will simply make use of this loop and loop will
do the tasks repeatedly for you.
So this is the importance of the loops.
Let's see something interesting program now.
So let's say
I want to say basically the numbers so let's make the number.
And we haven't destroyed the list yet really study in the next lecture.
But just remember list is basically the number.
So collection of a number we have instead of just making the variable we have the numbers in the list.
So we have the collection of a number in this number.
So I went to the sloop now for num isles and number in num.
I'll just take if that number is modulus of 2 is equal equal 0.
That means if it is even then I want to print that number.
My formatting is even less.
Format this with our number so close this.
Let's save this first.
Press F5 it is going to give us 2 is even four is even six and eight is even.
And if you one other then you can simply say else and is this print whatever is over here.
So let's go over here and print this is odd except this press of 5 you are going to see 1 is hard to
use even threes are in everything away.
So what we did hear was we make use of the conditionals with our for loop so every time these for loop
is executed it is going to get us a single number from this num so num is a list in the first loop.
This number will be 1 in the second loop.
This number will be 2 in the taht it will be 3 and in the fourth it will be for only one number at a
time and we are going inside this loop with only that one number with this one at the first.
So this is the first loop.
This number will be 1 and we are taking.
If one of modulus of 2 so this expression will return that is 1 because it is not even and it is not
divisible.
So it will be false over here.
That means this won't be executed.
It will go to the else part and it will just print that.
This is art in the second loop.
This number will be 2 and will just take again 2 modulus of 2 is 0.
Obviously this will be executed in the third loop.
This will be 3 because in this number list the third element is 3.
So it is going to get us 3 and this is again the false it will go to this part.
So this is the way for loop work and we have now a program that will simply get us a value to take for
the number that is even our order.
You cannot step further or that the alphabet that is viable or not so you can do the program for yourselves.
So if you do something like num is modulus of 2 is equal equal 0 dysplasia 5 then it is going to give
us the either saying this num is unsupported appearance type because this num is not what we just require
this num is basically it is taking hold up this list.
So we want to take only with the single number that means for loop will just take every element from
the start sequence and we can just execute our logic to the stick for that data.
Single data from the least it can be from the tuple or any other data sequence.
So this is the importance of the for loop.
So for loop can iterate over a sequence of number.
We have seen over here and we can just make use of the arenas method or any other method and we can
thus perform an operation over here.
So if you have to do something like take for the prime number or take for the reins printing some numbers
you can do something like buy the transmitter.
Let me stop you for yuks in rain so I absorb knew that transmitter will just give a sequence of numbers
so I'll say I want the number from 3 to 5 because the six is not included so rains three comma six will
print the tuple number of three four five because the six want to be included over here.
Just remember that and after we just do this we want to print that X to save this place.
5 You are going to get 3 4 5.
This is the way.
So we have something that is let me show you here real quick.
So if I say print and do this you are getting this in the tribute over here and sip attribute over here.
So if I say yucks and comma in their tribute to be only this but not less in the end if I say sip this
and print you are going to get three four five because this in this attribute that is going to give
us if I say something like Okay I want this much space between each of this number.
Well printing let me say this then you are getting these spaces in each of these numbers.
Like we just pass in this end.
So if you want in the middle of is no it means a dollar.
If I say this you'll see these number you'll have their dollar sign.
So we are just formatting with this new attribute over here with the end.
So we have now completed about the for loop.
So next one is a while loop so it is similar to the for loop let's see about the while loop.
So all in lowercase is starting with a while typing the while and you'll see some variable over here.
Let's make first variable.
Let me do the same tasks is we have done in the before like printing the name.
So I'll say count equals zero.
So I'll just take with the condition discount is less than five.
That means this will be executed five times.
I want to print that name which is five times if we just do this.
And if it's a sprint now it will be executed in finite times because this count zero value in here is
less than five.
It is always true it is a naval force.
That means this for a while loop will be executed in finite times and you'll have the console that will
be hanged after some moment because this is never going to be terminated because discount value is never
going to be greater than 5 which is always zero.
So if you want to the star minute after fifth loop you have to change the value of the count every time
you execute the first or second loop.
So if I say save this ratio 5 it is going to give us five times slots in which is obviously the condition
when this condition is true until and unless this count becomes greater than five it will terminate
or lose.
But if we just remove this if I just press has and if I save this precious five it is going to execute
all in finite time so it is not going to this terminate in any time.
So you have to disclose this now we have to kill it because this is never going to be terminated.
The county to this condition is true every time.
That means according said this while will be executed in finite time so this can be done something like
with this statement while true print such and so if you want to your program to have the infinite loop
you can do something like this.
It will be executed for the infinite loop.
We can do this in some programs making the loop that won't be terminated at any time.
This is the infinite loop.
Now let's learn about the break and continue statement.
So Brick is the name suggests is used to exit from the loop and continue is used to escape the cut into
block.
So let's see how this break in continues statement works with the funds and or with the program over
here.
So let me say count to zero.
Let me make while true which is our infinite loop.
And here I am going to print my count which is basically your count.
Now I'm going to say don't add every time this loop work how to count and if count is greater recall
loop 5.
You have to break.
So let's see first the break statement.
So what we're doing here is I've already shown you in the couple of minutes before that while Drew is
going to execute in infinite loop.
So this is going to do in finite but this breaking statement is used to break your loop whenever this
count is greater than equal to five brick is going to distort minute this loop and it will just get
out of this loop.
Let's see if this works or not.
So let's say this first press a five.
Then it is going to give us the five values because if this condition is true then it will be break
they see.
If I just remove this break or if I said this press of five.
OK.
We haven't had anything over here.
It's a print.
Hey
let's save this press then it is going to execute in finite times again because we don't have any condition
that will just start minute this loop that is going to be done by our breaking statement.
If this is the condition if this condition becomes true then break a statement will be executed.
So this is the way to just terminate out of this infinite loop using the break.
So let's see about the continuous statement now so continue is very easy and very important.
So let's make the program with a for loop.
So for yuks in range.
So I have already shown you about the rains it will this create as a tuple from 0 to 9.
So let's take these six E's even are not we have done this program again before.
So if it's the case then I just want to continue or else I want to print my ex.
So what do you think this program is all about.
Is it going to print us or even a murderer or no.
Let's save this and see.
And obviously this is going to print yes or no because in the case of even if we get the even number
where the sin continue.
Don't print that number does continue.
This is keep it that number and if it is defaults then print x that means it will just print odd if
for case.
Let me just quickly make here the rain so rains over here from 0 1 2 3 4 5 6 7 8 and 9.
So this will be in the printed value.
So at first it will be zero.
So whereas taking zero modulus up to zero which is obviously this part is true.
That means it will just endure in this if loop.
And here it is saying continue that means nothing to do with this zero.
Second it is saying 1 in this condition is false.
That means it'll go out here and it will print one for the next statement said to don't do anything
for the two just go in the another loop.
So that is staying with this continue continue is basically going to skip that starting block or a current
block and break is going to exit from the loop.
If the condition is met which we just saw in the previous example.
So this is our flow control part to what we just learned now looping concept and break and continuous
statement and we'll learn about the fun sense and more interesting part of fight in the upcoming lectures
in the next one.