Lopes permit us to repeat that action and the Python programming language.
We have two main kinds of loops.
We have the wire loop and the FA loop.
Now, if the number of actions we are about to take out and known initially, we generally use the while
loop and the case where the number of actions are already known.
We generally use the for loop, but there are ways of actually using any of them in either case, although
the follow up is more limited as compared to the while loop.
Now let's take some examples of how we use this in the Python programming language.
So we want to print out.
I am at position.
Position one.
And then do this.
So we want to point out my position two, three, four and five, one to do this five times rapidly.
Now, if this and or in this case we have an equals five is we want to do this and times and this gives
any cause five.
So we do this five times.
Now, if this entered to 500, we'll have to repeat this 500 times.
And that's not very efficient.
So to solve this problem, we could use a follow up where we just simply say for I in range.
Five.
We have Prince.
I am at a position.
I would you have to put I.
And there we go.
A good place.
This would end.
And is this for now we have that our large this we have this.
We have I am a position this is an area we haven't put this the former but format of I take this away
or run this tortoise and arrow take this away.
We have this so we'll find out.
We have this now for the follow up.
Why do you have this for whatever in a range?
Generally what happens here is the range has a start index.
And and and index ones you have only one value in your the stat index by default is zero.
So in this case it's the same as put in this.
So actually this is.
All right.
I would have comma.
So we have this.
So doing this is the same as having.
Range, zero comma and.
No, this is the same as doing this simply syringe.
And now that we have this.
What actually this loop does, it is goes from zero right up to N minus one.
This is very similar to when we were working with lists where we had a list C one.
Three four.
And if one of the lays out some elements, this would take us from 0 to 2 instead of from 0 to 3.
So doing this gave us zero, this and this.
So we'll print out this.
We're going to have one, three, four.
Now, coming back to the followup, the simple means if we walk in here, we are going to have four
I in range.
Recall the in operator where we want to take this variable we're creating so we're going to take four
I for all this I's in this range.
Now this range goes from zero to n minus one.
So what we're seeing here is for all I, which goes from zero to end, minus one.
Repeat this statement, I'll repeat this action.
So for all I going from zero to n minus one, we are going to print I am at position I and so when I
equals zero we're going to print.
I am at position zero.
When I equals one, I am at position one and so on and so forth.
So we'll see that we actually replace this.
Now with this, if you notice, yeah, we're printing from zero and that's because the default, that
index is zero.
So let's modify this.
We have one and we print that out.
Now let's think of this.
We run this that we go from 1 to 4.
And that's because we have right up to the end index minus one.
So if we want to bring up to five, we simply put here and plus one and doing this now we have from
1 to 5 and if wanted to go to 500, I want it to do is to run this and we simply go with see how we
able to go from this to 500.
And so that's it.
That's how the following works.
I should use this.
And we have that now.
We can take this off.
Now, it's important to note that we loops generally we have an initial statement and then we have.
And increments.
So in this case, we have the initial statement.
All we have the initial value of I to be equal equals zero on this case one.
So in this case, the initial value of AI is one.
And now we increment his value up to where we reach and plus one minus one.
So if we have this will basically increment this value from zero to and minus one.
So let's look at the while loop taking the Y loop.
It actually works a little bit different for a while loop.
If we want to do this same action that is, print this to the end times.
We have any cause.
Five In this case we could simply have while n is less than five.
So while we have this condition, we repeat whatever we have here.
So any less than five, we have print.
I am at position.
This.
So we have that format.
I now notice this is a little bit different.
Like the eye doesn't come in this.
I like to follow where this.
I came with afar.
Yeah, we just have this while in less than five, so we just have this condition.
Now we have to declare the eye separately so we can have eye initially at zero.
And then whenever we reach in this, we could put this eye plus equals one.
Now this eye plus equals one is simply seen eyes equals eye plus one.
So if eye is zero, any guess and you're Italian to one since it ends at one, it's going to continue,
and so on and so forth.
So let's run this and see what we have.
Running this.
We see that not in this printed and that is logical.
Because anything was five.
So we are seeing it and any less than five we should continue doing this given that end is always equals
five and is not changing actually when we get into this.
We are never going to get into this.
So actually wiser which is here and it's less than five we don't have any out of place where antennas
of five or and turns to a value less than five.
So we're never going to get into this loop.
So what we can do is we can modify this.
We have your eye.
So why I is less than five and now we keep on incrementing and I will see that initially I is already
less than five since I is equal to zero.
So we are obviously going to get into this loop and we're going to print this out.
So what I did in five would keep on doing this and once I turn this five, since Iceman increment,
increment it, we are going to go out of this loop.
So let's run this and see what we get.
So this is exactly what we wanted to have.
So we have this let's reduce its value.
Oh, that's the end to be a equals three.
I would take it as an end.
So we'll run this now on this.
Well.
Now let's go step by step and see what happens.
Exactly.
At the beginning, we have an equals three and I equals zero.
So we have.
Then when this comes here, we have I0 and three zero is obviously less than three.
So we're going to come in here and print out.
So we print out.
There's value with zero.
So that's why we have this.
Once this is printed out, I turn to one.
And since I is one, I is less than three.
So with the coming year and print is out, once that is printed out, it prints out this one.
That is the print out.
I am at position zero.
So this actually represents I am at position.
So let's write this out like this.
I am at position.
Wherever I am at position.
We have this.
I am at position.
Okay.
So we have that.
And once it prints this one out, it sends it to two.
And that's because I'm incremented here.
So with a while, we actually do this increment by hand, of which with a follow, we didn't have to
do this.
It does automatically.
So we have that.
And then since I is equal to two, is less than three, that's really less than three.
So he so gets into the loop and then we print out I am at position two.
Once it pans out, it tends to three.
But the reason strictly it isn't three.
So it goes out of the loop.
And that's why I always get pinned areas this three different outputs.
Now we could simply modify this to I plus one I will find here that we have we have this so we have
one, two, three as expected.
Now we could send this to 5%, this to 500.
And as was expected.
So let's modify this.
There we go.
So that's basically how the while in the follow ups work.
Now let's come back to the problem we had initially where we had the different parts.
And we wondered that if a player actually plays any of those parts, it should be printed out.
We saw how difficult it was to go using only conditions.
So let's look for a new way of solving that problem.
So we could simply see here that for I in range the line of spots.
I wish.
I understand why we're using this line of sports.
So for our little sports, we're going to see, for example, Prince I so run in this will see that
we have this.
This tells us that L.A. sports actually equals to because this is for iron range.
This is the same as I in Orange, too.
That's because sports actually contains this two elements.
So is this list with two elements or is Len is actually two?
So that's why we have the zero one.
Let's take this away.
We could even add the other sports here.
So we have volleyball.
And that's it.
So yeah, we have four iron range lens parts printed as I put it again.
See that we have this now we could actually even print land sports before.
So we have land sports.
As it tells this story.
Now consider ed if that that's for particular I if supports I now we know that but zero for example
is basically this football string.
Sports one is this.
And sports two is this.
So it is a particular sport.
I is it was very important to put this brackets it makes it work and it.
So if sports I is actually equals football.
Well then I print out.
I am a footballer.
And then we have l if sports I is equals.
Basketball.
Print out.
So this is actually the one space front we have print.
I am a basketballer.
And finally, we have Alief Sparks.
I equals volleyball.
So let's put a string volleyball.
Well, print out.
I am a volley baller and that is it.
So let's run this and see what we get.
Now, given that we have football, basketball and volleyball, we have I am a footballer, I'm a basketball
and I am a volleyball.
Now, if we take off one of this, we simply have this.
So this shows how easy it is to work with loops while trying to solve that problem.
What can replace here?
Simply have is.
So if this is this, see that it doesn't change anything.
So that was it.
Now, there's no way of looking at those loops and sort of working like this.
Let's couple this year.
Up and this year we have instead of saying four I arranged learn of this lives for going going through
each and every index will seem to see for I in sports I could see a for sport does a singular and then
there's a plural for so for sport in sports if a particular sport is football in this sport is basketball
playing that if that's part as volleyball in this so that's how we go and we see that we have exactly
the same results.
So instead of going through the in this is sports I and whatever are in the index we simply go by each
of the elements or we'll see a for sport in sports.
So what happens here is we're going to start with the first this and then we'll try it this year, this
year and Thursday and then we go to the next we're going to test this test.
So that's how it works.
Now, if, for example, make an area you can have this.
So see that since this is not the same as this, that doesn't work out well.
So let's put this back and that's it.
Now you can add other sports, tennis, what example and simply just put out this so we have live if
you put else print this should give you I am a tennis player.
So we have this and that is because.
This case when we does this, for example, that for the first case since sport equals football and
the beginning it goes into this.
And then for the next case since part equals volleyball test and it works out with this for the next
case since there's since part actually equals tennis.
It doesn't match with this it doesn't match with this.
It doesn't match with this or else actually it takes this.
And that's why we have I am a tennis player now.
Let's look at the break.
Pass and continue.
Keywords which are rarely used in loops.
Now suppose we have this.
We want that every time we have a footballer, we should simply break so we can have a break.
And doing this once again into this and you have this break would go out of this for a loop.
So we shall see it.
I won't print this.
We have just I am a footballer and that is because we've also got this took a break and we would left
this for a loop.
Now, if you take this, take this as a break, see that brings this out.
Since there is no basketball here, he doesn't even get here.
So his everything is printed out.
Now, if this brig is actually put here oh per year, for example, you see that football is printed
basketball.
I spend it all right.
Our football has printed, volleyball is printed, but tennis player is not printed because at this
point we leave the loop.
Now let's look at how continue our pass works.
So suppose we reach this point, we could have continue.
So run in this we see that everything works actually fine.
Now suppose we want to have a certain value.
So we see.
We take this A to B equals true.
So initially is true and we see it at we're going to continue from this point.
So we have e equals false and then we're going to print out.
E so let's run this.
We see that actually is equals.
True.
The reason why is true.
Whereas we actually got into this and we sent a to false is simply because once we get to this continue,
what happens is this continue.
It tells us to go to the next iteration.
Now notice I recall that walking with loops is basically I the region of our this induces.
So we start with this, we go to this and we go to this.
So once we come to this board and we have this continue automatically after printing this, we go directly
to the next add the reason we don't bother about the remaining part of it.
So no matter where you put out here.
So if you put this hello, normally you shouldn't have anything here.
And that's because this continue tells us to go to the next iteration immediately.
So that's how Continue works.
And then we also have pass.
So with this, suppose you have this and you don't want to actually print out whatever.
So you could just simply say pass because if you take this off, you're going to have an error.
And now sometimes you may not have anything to put here, so you may not have any action to take.
So you just want to have this condition.
And then maybe another condition, say, l if spark is.
Handball.
Also your hand.
And yeah, you want to print something?
So you see on a print.
Hello?
And in this case, the gist of.
Things out.
And once we get into this condition, we don't want to print any value or print out whatever.
So we just simply have this pass.
So that's it for this section.
The next section, which I've been working on Python functions.