What I want to do in this video is step through the program
that we wrote in the previous video
to really understand what's happening, line by line.
And get a little bit of an intuitive understanding
for for loops and variables.
We're going to do it for a specific example.
We're going to do an example where the user inputs--
so when the user is prompted to input a number,
the user will input the number 3.
We're just going to assume that.
And I've picked a low number so that we
don't have to keep looping through this forever.
So this video can be of a reasonable length.
So right when that happens, when the user inputs the number 3,
it the assigns the variable number to be equal to 3.
And there's two ways that you can think about this.
I tend to, what I do in any programming language,
when I think of a variable, I tend to view it as bucket.
So I have a bucket called number.
And in that bucket, when the user inputs a 3,
if the user inputs a 3, I'm sticking a 3 in that bucket.
Python is a little bit different than some languages.
And the more correct way-- and they're both kind of intuitive,
I think, if you think about it-- of viewing of variable
in Python, is to think of the variable as referring to the 3.
And I say that because, in Python,
if you change a variable's-- let's say right under this we
wrote number is equal to 2.
We didn't do that, but if we said
number is equal to 2, what Python does
is it actually leaves that 3 there in memory.
And then it would point to the number 2 instead.
It wouldn't obliterate the 3 and just put a 2 there,
the 3 still exists.
And that's important when we start
doing more complex or nuanced things with more fancy data
types.
But let's rewind all of this.
For the sake of this program, you
can view it either way, a bucket with a 3 in it.
Or you could view it as a label that
is referring to a 3 someplace in the computer's memory.
Now the next thing that happens is
that we have another variable called product.
And we assign product to be equal to 1.
So I'm going to do the same two representations.
So we've set up another bucket here, called product,
and we are assigning, we are putting 1 in that bucket.
Or you could say that product is a variable.
It's a label that is referring to a 1.
And then we get to our for loop.
And this is probably the confusing part.
So we have for i in range of number.
So literally the interpreter says OK, what's number?
Well, in this case, number is 3.
So this is going to be evaluated as range of 3,
in our particular example.
Range of 3.
And once again, how did the interpreter know that?
It's said range of number, number's a variable,
look up the number variable.
OK.
3 is sitting in it, so I'm going to put a 3 right there.
Or look up the number variable.
The number variable is pointing to the number 3,
so I'm just going to put a 3 here and take a range of 3.
And we saw in the last video that if you evaluate
just range of 3, range of 3 is equal to a sequence of numbers,
0, 1, 2.
So it creates a sequence of three numbers,
starting at 0, up to that number minus 1.
And what the for loop does, is every time
we go through the for loop, it executes this code.
It executes this code that's inside the for loop.
And we know it's inside the for loop, because we indented it.
And the most used convention is Python,
is to don't use the Tab.
You'd press Space four times.
And that tells the interpreter, anything
at this level of indention happens every time
we go through the for loop.
And when we start the for loop, the i variable
is going to start at 0.
So the first time we go through the for loop, i
is going to be the first element in this sequence.
Then, when we go to the for loop again,
i is going to be the second element in the sequence.
And then when we go through the for loop again,
i is going to be the third element in the sequence.
And then we go to the for loop and it'll
try to assign i to another element in the sequence
and say, hey, wait!
There are no more elements in the sequence.
And then it'll break out of the for loop
and then continue on with the rest of the program.
So given that, let's think about what
happens for the particular example
where the user inputted a 3.
So range of number is going to evaluate
as-- I wrote up here, 0, 1, 2.
Let me do it this way.
Let me write it over here.
So it's going to be, 0, 1, 2.
And the first time through the loop,
i-- so I'm going to use this convention right over here.
The first time through the loop, i is going to refer to the 0.
Or if we use this convention over here, the first time,
as we go through the loop, i, you
could view it as a bucket that is
going to be assigned the first element of range of 3,
the first element of the sequence.
It is going to be 0.
And then we execute what's in the loop.
We reassign, so we have to reassign product
to be something new right over here.
And what is this something new the first time
we go through the for loop?
i is 0.
0 plus 1 is 1.
So the first time we go through the loop--
let me do that in a color you can see-- the first time we
go through the loop, this is going to be 1, 0 plus 1.
Product is 1 this first time we go through the loop.
Let me do it in the same colors.
So i is 0 plus 1, so we get 1.
1 times 1 is 1.
So product will be assigned to be 1.
So product will be reassigned to be 1, which really isn't going
to change anything, because it's already 1.
So it was 1 before.
You multiplied it by 1 and reassigned it to be 1.
So I could write, cross out 1, it's now going to be 1 again,
but I'll just leave it as 1.
Or we can just say it's going to be reassigned to be 1.
So nothing really has changed that first time
through the loop.
But now, interesting things are going to happen.
So now we'll go back to the beginning of the loop.
And now, i will be the next element in the loop.
So now, i-- let me delete this-- it will be reassigned
to be the next element in the loop.
So now i will be 1.
That's what the for loop does.
It makes i each of the items in the sequence in order,
going through them.
And now we execute it again.
Now we go look at product times i plus 1.
Product is still 1.
i is now 1.
So 1 plus 1 is now going to be 2.
It is now going to be 2.
So 1 times 2 is going to be our new product.
So the new product, it was 1, but now we're
multiplying it by 2 and assigning it again to product.
So the new product is now going to be 2.
So the product variable is now 2.
And then now we try to loop again.
And now this time, i will be the next element.
i is going to be the next element in the sequence,
right over here.
Or we could say i is going to be reassigned
to 2, the next element in the sequence.
And we try to evaluate it again.
So now we go into the for loop.
Product is now 2.
i is now 2.
2 plus 1 is 3.
So this whole expression over here is 3.
So we have 2, product is now 2, times i plus 1.
i is two, so i plus 1 is 3.
So you have 2 times 3 3 is now going to be 6.
So the new product times i plus one is now 6.
So this is now going to-- let me do that same blue color-- this
is now going to be 6.
And now we try to execute the for loop again.
And the for loop's going to say, hey,
let me reassign i to be the next item in this sequence.
It tries to find the next item in the sequence.
But it says, wait, there is no next item in the sequence.
So we're done executing our for loop.
So there's nothing left in our sequence,
so we're done with our for loop.
Now we break out of it.
And we go to the next line in the program.
So the next line in the program is, literally,
just print the product.
And at this point, we just want to output the product.
And the variable product is now 6.
And actually, one thing I should have done,
this entire time is, I was sticking
with the bucket representation.
But the product on this representation,
here, it started referring to 1.
It got reassigned to 1.
But then it got assigned to 2.
So then this reference went to 2.
And then it got reassigned to 6.
So if I were to be consistent with this representation
over here, every time product is referring to another number,
or you can view it as a bucket, for the sake of this program,
where it keeps getting reassigned.
And what was cool about this, is when
we're done with this for loop, the number that's
sitting in the bucket product, the variable product--
or you could say, the number that the variable product
is referring to-- is the factorial of the number
that the person inputted.
It's the factorial of 3.
And if you remember, 3, the symbol
for factorial is an exclamation mark, is equal to 3 times
2 times 1.