-
What I want to do in this video is
-
introduce you to a “for" loop.
-
And we're going to do this by writing a little program
-
that calculates the factorial of a number.
-
In case you don't remember
-
what a factorial of a number does
-
If I were to tell you what "1 factorial" (is),
-
you normally write it
-
as 1 and an exclamation mark ( 1! ) like that.
-
It literally just means, well,
-
that will just evaluate to 1.
-
If you say "2 factorial" ( 2! ),
-
that’s going to be equal to 2 × 1.
-
If you say "3 factorial" ( 3! ),
-
that's going to be equal to 3 × 2 × 1,
-
and which is going to be equal to 6.
-
And so, if you have any number’s factorial,
-
it's going to be
-
that number times one number less than that,
-
times one number less than that,
-
all the way down to 1.
-
So, with that out of the way,
-
let's implement our factorial program.
-
And the way I'm going to do it is,
-
it's going to take an input from the user,
-
It's going to prompt the user
-
to put a number that they want to take the factorial of
-
and then it's going to calculate the factorial.
-
And so what I'm gonna do is,
-
I'm going to use a function that Python provides for us.
-
And I'm using Python 2 in this video.
-
You'd have to do it slightly different —
-
actually, I'll talk about that in a second —
-
if you are using Python 3.
-
Python 2 is
-
what most implementations of Pythoner are built in
-
at the time of this video.
-
But there is a Python 3 out there
-
that's kind of a newer version.
-
I'll clarify that a bit in a future video.
-
But anyway, we're going to take an input from the user,
-
and we're going to prompt them with a message.
-
We're going to say,
-
"Enter a non-negative integer to take the factorial of:".
-
And just so that
-
we get to familiarize ourselves with functions and all.
-
We're passing an argument to the function.
-
And the argument that we are passing to the function –
-
to the input function right over here is a string.
-
And "string" sounds like a very complicated thing,
-
but it really is just a string of characters.
-
You can view it as a bunch of text.
-
it'll present this text to the user,
-
and it'll give them a little input box.
-
And the user will put something into that input box.
-
It'll evaluate what is in that input box.
-
And then, you could either view it as
-
having the variable number refer to it,
-
or put it in the variable number,
-
depending on how you want to visualize the variable.
-
If you're using Python 3,
-
you'll want to call "eval"
-
on what's returned from this function.
-
This is if you're doing Python 3.
-
I'm running Python 2 right now.
-
And if you want to
-
do it exactly the way I'm doing it,
-
I'm using the PyScripter IDE —
-
Integrated Development Environment.
-
It's a free open-source project on a PC.
-
But there're many, many ways that you can edit Python.
-
You can really just do it on any text editor -- so anyway.
-
So, so far, all we've done is
-
we've taken input from the user
-
and we're putting it in the variable number.
-
What's really cool about Python is
-
we can just run this program as is
-
and just see what happens. So let's do that.
-
Save it, and I'm going to run it.
-
This little play button runs it in this IDE.
-
And just like we said, it prompts the user:
-
"Enter a non-negative integer to take the factorial of:".
-
So I don't know — le me type in 10.
-
And I [click] OK. Nothing happened.
-
Well of course nothing happened!
-
Because I didn't really tell this to do anything.
-
But if we type in "number",
-
we'll see that it is now assigned to 10.
-
The variable is now referring to the number 10
-
So now we can use this number
-
and take the factorial of it.
-
So what I'm going to do —
-
and we're going to do it iteratively —
-
we're going to talk more about what that means in the future.
-
So I'm going to define another variable called product.
-
I'm start product at 1.
-
What we're going to do is we're going to
-
start from 1 up to whatever number this is,
-
and keep multiplying the product by each successively larger number.
-
So we're going to start —
-
So I'm going to set up a for loop here —
-
In next video I'm going to
-
really step through exactly what a for loop does.
-
Within the for loop, we're going to define a variable i.
-
This is kind of the classic variable to define in for loops.
-
And that variable is going to keep incrementing
-
to larger and larger values as we go through it.
-
So "for i in range" —
-
And for loops in Python are a little bit different
-
than if you were doing it in Javascript or Java.
-
I'll do a future video where we do that.
-
But "range of number" —
-
And just to understand what range of number does is,
-
it provides a list of numbers starting with 0,
-
up to 1 less than that number.
-
So over here we already defined what number is —
-
number is 10. So if I type in range of 10 —
-
or let me do range of number.
-
It gives me a list starting with 0 all the way to 9.
-
All the way to 1 less than than 10
-
So that the same thing as range of 10.
-
And what this for loop does is
-
it assigns this i to each term of the sequence —
-
So it starts with 1 — or each term of this list.
-
It starts with 1, and then it'll —
-
whatever you tell it to do.In the for loop, with i is equal to 1,
-
then it'll assign i to to be 1.
-
Sorry. It'll start with i equals 0, then it will go to i equals 1,
-
then i equals 2 then i equals 3 — so on and so forth —
-
all the way until you have gone all the way through the list.
-
And so what we can do here is —
-
within the for loop — we can redefine product
-
to be equal to the product — the former product —
-
So our new product is going to be our old product times —
-
and I'm going to multiply it — times i + 1.
-
We're going to step through it carefully in the next video.
-
But I want you to think about what it's doing already.
-
So right when we start, let's say that
-
number ends up being 3. So product gets set to 1.
-
And we say "for i in range".
-
And then, if we do range of 3,
-
then i is first going to be 0.
-
And so we're going to take our old product — so 1 — times i + 1.
-
Well i is going to be 0 so 0 + 1 is 1.
-
So it is going to be 1 × 1, [which] is going to be 1.
-
Then we are going to assign i to be 1.
-
This is what the for loop does.
-
It iterates.
-
It keeps incrementing,
-
(Well it doesn't always have to increment.)
-
It keeps changing the value of i as it goes through this list.
-
And so the next time around our product is 1.
-
But now i is 1. So 1 + 1 is 2, times 1 is now going to be 2.
-
And then it'll go — it'll set i to be 2.
-
So it's going to be 2 + 1 times...
-
what product was from the last iteration — which was 2.
-
And so it's going to be 2 × 3 which is 6
-
And so when we are finally done with this for loop,
-
inside of product —
-
or I guess we can say product will be referring to
-
the actual factorial of the number inputted.
-
So then we can say "print product".
-
And in Python — in this case right here —
-
the interpreter knows to break out of this for loop
-
once it runs out of numbers to assign i to.
-
So, that's why it doesn't run forever.
-
So, let's see if what we did here actually works.
-
Let's see if it actually works.
-
So let me — actually — write like this.
-
So let's see if this things actually works.
-
So I'll save it. And then let me run it.
-
And so it's asking me,
-
"Enter a non-negative integer to take the factorial of:".
-
So let's try it with 3 — Enter.
-
It gave me the right answer.
-
Let's try it again.
-
So, "Enter a non-negative integer to take the factorial of:".
-
I don't know, let's try something large: 10.
-
It worked. [I] assume that you can verify this for yourself.
-
So what we have done here is write a very simple program.
-
But, it already does something neat —
-
it takes a factorial of an arbitrary number.
-
In next video, we're going to
-
step through it much more carefully,
-
in case this kind of confused you – what this for loop did.
-
So, just hold with me for the next video, and watch that.
-
That has a little bit of a more careful explanation.