Now that we know a little bit about lists,
I thought I would use this video to introduce you
to loops, and in particular for loops.
And if we have time, we might do other types of loops.
But before we even do that, I want
to introduce you to a built-in Python function called Ranger.
When I say built-in, it means it comes with Python.
And it's really a way to generate
lists that have integers that increment.
So for example, if I just say, range of 6,
it generates a list from 0 through 5.
So it's 6 elements up to, but not including, 6.
If I wanted to include 6, I would have to do range of 7.
And then I go all the way up to, but not including 7.
But I get 6 in there.
If I want to have 1 through 6, if I
don't want to have that 0 there, I could do range starting at 1
and including 1, up to, but not including 7.
So the first item here will be included.
The last item will not be included.
And there we go.
We get 1, 2, 3, 4, 5, 6.
And what's cool about these integrated development
environments, these IDE's, this pie scripter that I'm running,
is that, when I type in a function that it recognizes,
it actually will tell me a little bit
something about that function, in case you forget it.
So I don't know if you can read this here,
but it says, range, start, stop, step.
So right now, the default is to increment by 1 each time.
But it says, hey, I could tell you
to increment by a different number.
So let's say you have a range.
Say you start at 0, and you want to go up to 8.
And I want to go up by 2, instead of 1.
So then I went to 0, 2, 4, 6-- I should have said it more
precisely-- up to, but not including 8.
The upper part of the range, you don't include it.
So it goes up to that, and it increments it by 2.
So you could do it like that.
So maybe you want to start at 3, and you want to go up to,
but not including-- I don't know.
Let me do up to, but not including 31.
And you want to go up by steps of 3.
So there you go.
It went up steps by 3, up to, but not including 31.
So it can generate a whole bunch of lists.
And the reason why this is useful
is because we can use these inside of loops.
And so the first type of loop I'm going to introduce you to
is a for loop.
So this was the interpreter down here.
Here, I'm going to actually write a program.
So this program is really just to show you a for loop does.
So I could say, for i-- and i is this variable that's often
used in for loops, in iterative loops.
And iterative means you keep doing something over and over
again-- for i in-- and let me just
do a simple one-- in range-- and I'm just
going to say 5-- range of 5.
So this will generate a list, 0, 1, 2, 3, 4.
So for i in range 5.
And this colon says, now we're going
to have a sub clause of what to do on each of those iterations.
Let's just print out what i is.
And so, before we even see what this is going to do,
let's just think about it.
It's saying, for each time we go through this loop,
for each of the things in the range, make i refer to them.
And then each of those loops is going to refer a different one.
And you want to print it out.
So let's just try this out.
So let me save this program.
And now, let me run it.
So there you go.
What it did is it created a loop.
So each time it goes to the loop,
it assigns i to a different element in this list.
As you remember, this list was 0, 1, 2, 3 and 4.
Did I say 0, 1, 2, 3, 4, 5 before?
No, it's 0, 1, 2, 3, 4, up to, but not including 5.
And so, each time you go through the loop,
it goes through each of those items.
And this program really just says, print out that i.
So the first time we go to the loop, i is 0.
So it prints it out.
Then it prints out 1.
Then it prints a 2.
Then it prints a 3.
Then it prints a 4.
So it just keeps going through it that many times.
If we said this was for range 10.
Now let's run this program, you'll
see it'll print 1 through 9.
I should say 0 through 9, because that's
the first element in this list.
But you could do more interesting things.
Let's say that you wanted to take the sum of 0 through 9.
So what you could do is you could define another variable
here, sum.
So before I do anything, the sum is just going to be 0.
And then what you could say is, each time you go
through the loop, why don't you define sum
to be the previous sum plus what i is in this part of the loop?
And just so that we can keep track of it,
I then want to print the sum.
Now let's see what this thing does.
So let's try it out.
So let me save it, and then let me run it.
So there you go.
So the first time we went to the loop, sum is zero.
Sum is 0 and i is 0.
You add them together.
You print the sum, you get 0.
The next time we go through the loop, i is 1.
We add that to the previous sum, which was 0, print it,
you get one.
Then the next time through the loop, i is now going to be 2.
Add that to the previous sum, 2 plus 1, you get 3.
Next time through the loop, i is 3.
You add that to the previous sum, you get 6.
So the sum of all the numbers, 0 through 9, is 45.
So just that, that's actually a kind of a useful program.
But just to make the point a little bit clearer,
let me actually walk you through this on my little drawing
tablet.
So let me close my-- let me paste my code that I just
wrote up here.
And let's just think about what this is doing.
So we have a couple of variables here.
We start off with this variable sum that is set to 0.
And there's two ways to visualize variables.
A common way to visualize it-- and this
isn't necessarily wrong, this is sometimes an easier way
to think about it-- is to view them as buckets.
But in Python, the really correct way-- and you
saw why that's important with lists--
is to view them as referring to something.
So if you view them as buckets, you can say, OK,
from the get go, the number in the sum bucket is 0.
But the reality is, is that the variable sum is now
referring to the numeric literal, the actual number 0,
sitting in memory someplace.
Now we enter into the for loop.
And what the for loop says is, let's iteratively
assign i to each of the elements in the list generated
by a range of 10.
So just to be clear, if we called this right over here,
range of 10, this will generate a list that looks like this.
So it will generate a list 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9, up
to, but not including 10.
This is 10 elements right over here.
And then we go into the for loop.
So the first time we go through the for loop--
and let me write it-- so I'll think about it
two different ways.
So one, I'll think about it in the bucket way.
So we have this variable, i, that can actually only
be used inside of this clause.
We'll talk more about scope or where a variable can be used.
But this i variable is only good inside of this clause.
The sum variable, the way I've defined it,
I can use it anywhere, outside of the clause, or inside of it,
because it was defined outside of the clause.
So you have your i.
So this is if you think of it as a bucket.
And so, through the first iteration,
i is assigned to this first item over here.
So i is going to be 0.
The better way to think about it is i
is actually referring to this first element, our first time
through the loop.
And then we go through the loop, and it says sum plus i.
Well, sum is 0.
And i is 0.
You add them together, you get 0.
So sum is still going to be 0, or still referring to 0.
And then it prints the sum out.
So then this line right over here prints sum.
It says, OK, the sum right now is just-- let
me do it in blue-- it's just 0.
And it printed it out.
Then it goes back to the for loop
and says, look, are there any more elements
that I can assign i to?
Well, sure there are.
There's all of these that we have to go through.
So then it makes i the next element in the sequence.
So now i is going to be the next element in the sequence.
Or another way to think about it is in the i bucket--
if you want to view them as buckets--
i is now set equal to 1.
And now we run this again.
And we say, OK, sum is 0 plus i.
I is 1.
So sum is now going to refer to 1.
Or another way to think about it is sum
is now going to refer to 1.
And when you print sum, it'll now print 1.
Then, the for loop will say, alright,
are there any more elements in this list right over here
to assign i to?
Well, sure there are.
There's all of these others, so let me keep going.
So i can now be equal to, or can refer to, 2.
So if you think of it as a bucket, i is now 2.
Or you could say, i is now referring to 2.
And you could ignore these right over here.
And we go through the loop again.
Sum is now 1.
1 plus i, i is now 2.
1 plus 2 is 3.
So sum is now going to be 3 this time through the loop.
So sum is now referring to 3.
You print sum, you get 3.
And you keep going all the way.
As we go through the loop, the loop
will still keep going, as there's
more things that i can refer to.
We can keep incrementing i to these other values over here.
And eventually, i is going to be 9.
We're going to evaluate this.
And the for loop says, hey, is there
anything left in this list that i can be?
And you're like, no, there isn't anything left in this list.
And then the loop is going to be done.
And we'll break out to it.
And the program will stop.
And that's exactly what we saw happening,
when we ran the program over here.