-
What I want to do in this video is introduce you to
-
a foreloop 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 an
-
number does if I were to tell you what one factorial
-
you normally write is as one then an exclamation mark
-
it basically just means well that would just evaluate to one
-
If you say two factorial that is going to be equal to two
-
times one if you say three factorial that is going to be
-
equal to three times two times one and which is going
-
to be equal to six and so if you have any number of
-
factorials 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 one so with that out of the way
-
lets implement our factorial program and the way I
-
am going to do it is I am going to take an input from
-
the user its going to prompt the user to put a number
-
that they want to take the fatorial of and then it is going
-
to calculate the factorial and 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 will do it slightly
-
differnt, acctually I'll talk about that in a second if you are
-
using Python 3 Python 2 is what most implementation 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 will clarify about that in a future video but anyway
-
we are going to take an input form 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 get familiarize ourselves with
-
functions and all we are passing an argument to a function
-
and the argument that we are passing function, to the input
-
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
-
an then you can view it as have 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 whats 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- Intergrated Development Enviornment its free
-
open source project on a PC but there is many many ways
-
that you can edit Python you can really just do on any
-
text editor so anyway, so far all we've done is we've taken
-
input from the user and we are putting it in the variable
-
number and whats really cool about Python is we can
-
just run this program as is and just see what happens
-
so lets 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 to enter a
-
non-negative integer take the factorial of so I don't know
-
we type in 10 and I type ok nothing happened well of
-
corse nothing happend because I didn't really tell this
-
to do anything but if we type in number we will see
-
that it is now assigned to 10 the variable is now refering
-
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
-
are 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 am going
-
to start product at 1 and we are going to do is we are
-
going to start from 1 up to whatever number this is
-
and keep multiplying the product by each sucessive 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
-
but we are going to, within the for loop we are going
-
to define a variable i this is kind of the clasic variable
-
to define in forloops and that variable is going to keep
-
incramenting to larger and larger value as we go through
-
it so for i in range and forloops in Python are a little bit
-
differnt 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 and go up to 1 less than that number so over here
-
we already define 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 one less than then 10 so that the
-
same thing as range of 10 and what this forloop does
-
is it asigns the i to each term of the sequence so it starts
-
with one and then it will go to what ever you tell it to do
-
in the forloop with i is equal one 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 untill you gone all the way through
-
the list and so what we can do here is within the forloop
-
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, an I am going to multiply it times
-
i plus 1 times i plus one we are going to step through
-
carefully in the next video but I want you to think about
-
what it is doing already so right when we start
-
lets say that number ends up being 3 so product gets
-
set to 1 and we say for i in range an 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 times
-
1 is going to be 1 then we are going to assign
-
i to be 1 this is what the forloop does it itterates
-
it keeps incramenting, well it dosen't allways have to
-
incrament and it keeps changeing the value of i
-
as it goes through this list and so the next time around
-
our product is one but now i is 1 so 1 + 1 is 2
-
time 1 is now going to be 2 an 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 times 3 which is 6 and so when we finally done
-
with this forloop inside of product or I guess what we can
-
say product will be refering to the acctual factorial of
-
an number inputed so then we can say print product
-
and in Python in this case right here the interpreter
-
knows to break out of this forloop once it runs out of
-
numbers to assign i to so thats why it doesn't run forever
-
so lets see if what we did here acctually works lets see
-
if it acctually works so let me write it like this so lets
-
see of this things acctually works so I'll save it and then
-
let me run it and so it is asking me to enter a non-negative
-
integer to take the factorial of so lets try it with 3
-
enter it gave me the right answer lets try it again
-
so enter a non-negative integer to take the factorial
-
of I don't know lets try something large 10, it worked
-
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 arbitary number and then next video we are
-
going to step through much more carefully in case
-
this kind of confused you what this forloop did
-
so just hold with me for the next video and watch that
-
and that has a little bit of a more careful explanation