Hello.
Welcome back.
In this lesson we're going to look at loops in Python.
So this our last project or do its names.
It's named conditions and loops.
We didn't explode loops and now so I'm adding this other lesson for us to see loops.
So I'm going to come over here.
New file and I'm going to call this loops by pressing control is to save and I call this number five
on this call loops.
And once that is done I can close this old one.
So python like any other programming language uses the for loop and the while one loop all the while
loop for loops can iterate very a given sequence and this young example we can say this time let's add
some comments to print out these simple examples we shall print in the end so I'll put a header here
I'll put a comment which I'll call.
Example 1
and this example shall we just print one through 8 like this.
Let's just put this here printing simple one resort.
This will make our output much more clearer.
Print example.
Right.
So now let's say we have a list of numbers.
I'm just going to have a variable here numbers.
And this is a list and it's going to be a list contained in 0 1 2 3 4 5 6 7 8.
Like this we've got a call mohair so we have these numbers.
So to look through this list we can just do four.
And then we have to create an iterator we use num here some as an integrator.
What is this.
It's rates are going to loop or look through.
So we're going to say for num in numbers like this.
So we're going to use this iterator to loop through this one here.
Once that is done we can see print.
Now
so this one is going to go through 0 1 2 3 4 5 6 7 8.
OK.
Let's print and see.
I'm just going to click over here to run module.
OK.
To save that I can see.
Example one resorts to a one two three four five six seven eight.
Right.
So with lubed through the sequence very simply also we can use the for loop to iterate or loop through
a range.
Let's what I mean.
So over here I'll just say example to put a comment here for a revision a sample to print the numbers
0 1 2 3 4.
So I can just come in and say print
sample to resorts.
Right.
Let's get to the actual code so we can see for x in range 5.
We can just use the word range here and pass the documents five.
And basically this means from zero through four So five is not included.
Right.
If we say range six six is not included is anything below six range 10 means the same thing zero through
nine range 20 zero through 19.
So for x in range five we can just say we have to put code on here we can just say print x and let's
do that and see what we've got or save by press and control and X and click over here to run the module
you can see we've got serial One two three four right let's see on that example so rather than just
print the range we can actually select certain numbers in the range that should be printed.
Let's see this example just copy this to prevent the typing noise put over here and I'll see.
Example three this time we're going to print Hubble would print 3 4 5 less print 3 4 5
we're going to print from 3 through 5 like this.
So we can just come here and see for x in range this time we'll can pass two argument three comma six
right.
Then we can see we put a semicolon her and then we can see print x let's just spaced this out a bit.
So this is a sample number three resort to our click over here to run hits enter to save.
Now you can see we have 3 4 5 printed here just like we passed over here 3 4 5 and this has got nothing
to do with the list really range just provides the numbers for us so it's print an X in this range three
four five without a six.
Right.
So let's see on that example.
And it doesn't have to be completely sequential.
Sometimes we can jump over sets in numbers in the sequence.
Let's do example for this time we're going to print something like 3 5 7 and we're going to use range
in a different way.
Copy this.
Then I'll bring it down.
This is just to make our printouts look good.
And this one is going to bring 3 5 7 and this is going to be Example number 4.
So to do that we can just come here and see 4 x 4 x in range.
This time we can pass three arguments.
We can pass three eight and then two.
So this is going to be the step size is going to print three and then jump two steps.
Bring the other number in then jump two steps.
Bring the other number.
So we see print within this range.
This is the lower boundary.
This the upper boundary.
And this should be the steps rather than go three four five six seven.
Go three jump two steps which will give us five.
And then when you reach five jump another two steps which would give us seven.
Then you print.
Let's see what I mean.
So I put a semicolon here and then I'll say print x like this.
And this is example for.
So let's observe for example for print control as to save click over here to run the module and we have
three four five three five seven.
Just like we stated here.
So we'll use this as the steps.
This is the range.
Three three through eight and then we use to us this step size right.