(upbeat music)
- Repeating code over and over again,
via a construct known as a loop, is also
a fairly common scenario in programming.
Python provides a couple of ways of doing that,
which we're gonna take a look at now.
And you can see that I've got a stubbed out function
here named main,
and a variable named X, which I'm initializing to zero.
So let's start by taking a look at the while loop.
To write a while loop, I use the keyword while,
and then I give it a condition.
So I'll write, while (X < 5) and then my colon.
And again, you can see that the code is being indented
under the colon there.
And I'll write print X,
and then X = X + 1.
A while loop executes a block of code
while a particular condition evaluates to true.
So while (X < 5),
we're going to print the value of X.
And then we're going to increment X by one.
Some languages like C, provide a whole bunch of ways
of doing loops.
Python likes to keep things simple.
It's only got two ways of doing loops,
While and For.
And we'll get to for in a moment.
So, let's run this.
I'll go to the DEBUG tab and click run.
And you can see that X starts out at zero.
So I print zero, one, two, three, and four.
And then it increments to five.
The condition in the loop is no longer true.
And so the loop terminates.
All right, so now let's try a for loop.
So I'll write for X in range,
(5, 10).
And then I'll write, print x.
Then I'll comment out my while loop.
So four loops in Python work a little bit differently
than you might be used to in other languages.
For example, in JavaScript, you have the concept
of an index counter that controls the number
of iterations in a for loop.
For example, you might have something like for,
and then I = 0, and then I is less
than some condition, right?
And then I ++.
What you have is this loop counter variable
that's controlling the execution of the for loop.
So Python works a little bit differently.
Python's for loops are what are called iterators.
In this case, I want to have X loop over a range of numbers.
I use the range function to give me that range of numbers.
So I've got a range going from five to 10,
and I'm going to print X each time.
So it's probably easier to understand this
once you've seen it in action.
So let's go ahead and save.
And then I will run this.
And here in the output window,
you can see that the results are five, six, seven, eight
and then nine.
And 10 is not inclusive in the range.
So, the range starts at this number
and then stops just short of this one.
So it prints out five through nine.
But for loops operate over sets of things,
not just numbers.
So, let's try something a little bit different.
So let's suppose I had a ray, names including, Monday
Tuesday, Wednesday.
So just the days of the week.
I could do something like, for d in days,
and then print d.
Now, obviously this has nothing to do with numbers.
In this case, my for loop is iterating over each element
in the days list.
And in each iteration, d will be set to the current item
that it's looking at that time through the loop.
So if I save and run this, you can see in the output
that it's printing out Monday, Tuesday, Wednesday,
and so on.
So it's looping over the contents of the list.
So, now let's take a look at using the break
and continue statements.
Comment out some of the previous ones.
Right.
So now I'll create a for loop that says,
for X in range.
And once again, we'll use five to 10.
And I'll write if X =7, then break.
Otherwise print X.
So this is the same for loop we saw earlier
when we had X in the range of five to 10.
But now we're going to do a couple different things.
First, let's look at the break statement.
The break statement is used to break the execution
of a loop, if a condition is met.
So what I'm doing here is saying, if X = 7, then break.
The break statement will cause this for loop
to terminate and fall through to the next block of code.
In this case, that's the end of the function.
There's nothing else to execute.
So we'll save and run.
And you can see that what's happening is,
it prints five and six and then gets to seven,
and then the break statement kicks in
and causes the loop to terminate.
So it never gets to seven, eight, nine.
All right.
So now, let's look at the continue statement.
The continue statement skips the rest of the loop
for that particular iteration.
So, I'm gonna comment out this,
and I'm going to write if X % 2 = 0,
then continue.
Take X divided by two.
And if the value leftover is zero, then continue.
Continue basically means, skip the rest of the execution
of this loop.
So just go back up to the top of the loop
and start with the next value.
So, don't do this statement right here that's in the loop.
Let's go ahead and save and run this.
And you can see that six and eight were skipped
because those are even numbers,
and my loop only printed out five, seven, and nine.
All right.
Let's take a look at one more example using loops.
Comment this out here.
Right.
So let's go back and revisit the loop
we had up here where we were looping over the days.
So I'm going to copy this and I'm going
to paste it down here,
and I'll uncomment those lines.
Now, remember earlier, I said that normally,
the for loop in Python does not use an index variable.
Like there's no loop counter, like you'd see in JavaScript.
Well, you can get one if you really need it.
Let me add the code here.
I'll write for I,d in days.
But I'm going to put the word days inside
an enumerate function.
The enumerate function will iterate
over this collection like loop normally would.
But, in addition to returning the value of the item
being looked at, it also returns a value.
that is the index of the item in question.
This function is going to return two values.
It's going to return the index of the member
of the collection that we're looking at,
as well as the actual member of the collection.
And so I'm going to print something a little bit
differently this time.
So I'm going to print I,d right?.
And I'll save.
All right. So let's go ahead and run this.
And now you can see I'm printing out
both the index counter variable
as well as the member of the array.
So I'm getting zero in Monday, one in Tuesday and so on.
All right.
That's a brief look at loops in Python.
(upbeat music)