-
What I want to do in this video
is introduce you to a “for loop.”
-
We are going to do this by writing a little program
that calculates the factorial of a number.
-
And in case you don't remember what
a factorial of a number does –
-
you normally write it as “1 factorial” –
as 1, then an exclamation mark. ( 1! ) Like that:
-
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 is going to be equal to
-
3 × 2 × 1, which is going to be equal to 6.
-
So, if you have any number’s factorial,
it is just 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.
-
The way I am going to do it is
I am 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 is going to calculate the factorial.
-
So what I am going to do is, I am 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 Python are built in at the time of this video.
-
But there is a Python 3 out there
that is kind of a newer version.
-
I clarify that a bit in a future video.
But anyway –
-
We are going to take an input from the user,
and we are going to prompt them with a message.
-
We are going to say, ”Enter a
non-negative integer to take the factorial of.”
-
And just so that we familiarize
ourselves with functions and all,
-
we are 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.
-
And it will present this text to the user,
and it will give them a little input box.
-
And the user will put something into the input box.
-
It will evaluate what is in that input box.
-
And then, you can 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 are using Python 3,
you will want to call “eval”
-
on what’s returned from this function.
-
This is if you are doing Python 3.
-
I am running Python 2 right now.
-
And if you want to do it
exactly the way I'm doing it,
-
I am using the PyScripter IDE —
Integrated Development Environment.
-
It’s a free open-source project on a PC.
-
But there are 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.
-
And 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.
-
I'm going to 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 a 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. —
-
And we are going to talk more
about what that means in the future.
-
So I'm going to define
another variable called product.
-
And I'm going to start product at 1.
-
And what we are going to do
is start from 1 up to whatever number this is,
-
and keep multiplying the product
by each successively larger number
-
So we are going to start –
-
So I am going to set up a for loop here —
And in the next video I'm going to really step through
-
exactly what a for loop does.
-
Within the for loop, we are
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.
-
And I will 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 will assign i to to be 1.
-
Sorry. It will 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 am going to multiply it
times i + 1.
-
We are going to step through
it carefully in the next video.
-
But I want you to think
about what it is 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 are 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 plus 1 is 2, times 1
is now going to be 2.
-
And then it will go – it will set i to be 2.
-
So it is going to be 2+1 times what product
was from the last iteration – which was 2.
-
And so it is 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.
-
Let’s see if what we
did here actually works.
-
Let’s see if it actually works.
-
So let – actually – let me write it 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'm] assuming 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 the 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.