< Return to Video

Writing a Simple Factorial Program. (Python 2)

  • 0:00 - 0:04
    What I want to do in this video is introduce you to
  • 0:04 - 0:08
    a foreloop we are going to do this by writing a little
  • 0:08 - 0:11
    program that calculates the factorial of a number
  • 0:11 - 0:14
    and in case you don't remember what a factorial of an
  • 0:14 - 0:16
    number does if I were to tell you what one factorial
  • 0:16 - 0:21
    you normally write is as one then an exclamation mark
  • 0:21 - 0:25
    it basically just means well that would just evaluate to one
  • 0:25 - 0:28
    If you say two factorial that is going to be equal to two
  • 0:28 - 0:33
    times one if you say three factorial that is going to be
  • 0:33 - 0:40
    equal to three times two times one and which is going
  • 0:41 - 0:43
    to be equal to six and so if you have any number of
  • 0:43 - 0:45
    factorials it is just going to be that number times
  • 0:45 - 0:47
    one number less than that times one number less than
  • 0:47 - 0:50
    that all the way down to one so with that out of the way
  • 0:50 - 0:53
    lets implement our factorial program and the way I
  • 0:53 - 0:56
    am going to do it is I am going to take an input from
  • 0:56 - 0:58
    the user its going to prompt the user to put a number
  • 0:58 - 1:01
    that they want to take the fatorial of and then it is going
  • 1:01 - 1:03
    to calculate the factorial and so what I am going to do
  • 1:03 - 1:07
    is I am going to use a function that python provides for us
  • 1:07 - 1:11
    and I'm using Python 2 in this video you will do it slightly
  • 1:11 - 1:14
    differnt, acctually I'll talk about that in a second if you are
  • 1:14 - 1:18
    using Python 3 Python 2 is what most implementation of
  • 1:18 - 1:21
    Python are built in at the time of this video but there is
  • 1:21 - 1:24
    a Python 3 out there that is kind of a newer version
  • 1:24 - 1:29
    I will clarify about that in a future video but anyway
  • 1:29 - 1:31
    we are going to take an input form the user and we
  • 1:31 - 1:33
    are going to prompt them with a message we are going
  • 1:33 - 1:42
    to say enter a non-negative integer to take the factorial
  • 1:42 - 1:47
    of and just so that we get familiarize ourselves with
  • 1:47 - 1:50
    functions and all we are passing an argument to a function
  • 1:50 - 1:53
    and the argument that we are passing function, to the input
  • 1:53 - 1:57
    input function right over here is a string and string
  • 1:57 - 1:59
    sounds like a very complicated thing but it really is
  • 1:59 - 2:02
    just a string of characters you can view it as a bunch of
  • 2:02 - 2:04
    text and it will present this text to the user and it will
  • 2:04 - 2:06
    give them a little input box and the user will put something
  • 2:06 - 2:12
    into the input box, it will evaluate what is in that input box
  • 2:12 - 2:14
    an then you can view it as have the variable number
  • 2:14 - 2:17
    refer to it or put it in the variable number, depending
  • 2:17 - 2:21
    on how you want to visualize the variable if you are
  • 2:21 - 2:29
    using Python 3 you will want to call eval on whats returned
  • 2:29 - 2:31
    from this function this is if you are doing Python 3
  • 2:31 - 2:35
    I am running Python 2 right now and if you want to do
  • 2:35 - 2:39
    it exactly the way I'm doing it I am using the PyScripter
  • 2:39 - 2:42
    IDE- Intergrated Development Enviornment its free
  • 2:42 - 2:45
    open source project on a PC but there is many many ways
  • 2:45 - 2:48
    that you can edit Python you can really just do on any
  • 2:48 - 2:52
    text editor so anyway, so far all we've done is we've taken
  • 2:52 - 2:56
    input from the user and we are putting it in the variable
  • 2:56 - 2:58
    number and whats really cool about Python is we can
  • 2:58 - 3:01
    just run this program as is and just see what happens
  • 3:01 - 3:03
    so lets do that I'm going to save it and I'm going to
  • 3:03 - 3:08
    run it, this little play button runs it in this IDE and
  • 3:08 - 3:10
    just like we said it prompts the user to enter a
  • 3:10 - 3:13
    non-negative integer take the factorial of so I don't know
  • 3:13 - 3:17
    we type in 10 and I type ok nothing happened well of
  • 3:17 - 3:20
    corse nothing happend because I didn't really tell this
  • 3:20 - 3:24
    to do anything but if we type in number we will see
  • 3:24 - 3:27
    that it is now assigned to 10 the variable is now refering
  • 3:27 - 3:31
    to the number 10 so now we can use this number and
  • 3:31 - 3:33
    take the factorial of it so what I'm going to do and we
  • 3:34 - 3:36
    are going to do it iteratively and we are going to talk
  • 3:36 - 3:40
    more about what that means in the future so I'm going
  • 3:40 - 3:43
    to define another variable called product and I am going
  • 3:43 - 3:46
    to start product at 1 and we are going to do is we are
  • 3:46 - 3:50
    going to start from 1 up to whatever number this is
  • 3:50 - 3:53
    and keep multiplying the product by each sucessive larger
  • 3:53 - 3:57
    number so we are going to start so I am going to set up a
  • 3:57 - 3:59
    for loop here and in the next video I'm going to
  • 3:59 - 4:03
    really step through exactly what a for loop does
  • 4:03 - 4:06
    but we are going to, within the for loop we are going
  • 4:06 - 4:09
    to define a variable i this is kind of the clasic variable
  • 4:09 - 4:12
    to define in forloops and that variable is going to keep
  • 4:12 - 4:15
    incramenting to larger and larger value as we go through
  • 4:15 - 4:21
    it so for i in range and forloops in Python are a little bit
  • 4:21 - 4:24
    differnt than if you were doing it in javascript or java
  • 4:24 - 4:26
    and i will do a future video where we do that but range
  • 4:26 - 4:29
    of number and just to understand what range of number
  • 4:29 - 4:33
    does is it provides a list of numbers starting with
  • 4:33 - 4:37
    0 and go up to 1 less than that number so over here
  • 4:37 - 4:40
    we already define what number is, number is 10 so
  • 4:40 - 4:43
    if I type in range of 10 or let me do range of number
  • 4:43 - 4:46
    it gives me a list starting with 0 all the way
  • 4:46 - 4:48
    to 9 all the way to one less than then 10 so that the
  • 4:48 - 4:52
    same thing as range of 10 and what this forloop does
  • 4:52 - 4:57
    is it asigns the i to each term of the sequence so it starts
  • 4:57 - 5:03
    with one and then it will go to what ever you tell it to do
  • 5:03 - 5:06
    in the forloop with i is equal one then it will assign i to
  • 5:06 - 5:11
    to be 1 sorry it will start with i equals 0 then it will go
  • 5:11 - 5:13
    to i equals 1 then i equals 2 then i equals 3 so on and
  • 5:13 - 5:16
    so forth all the way untill you gone all the way through
  • 5:16 - 5:26
    the list and so what we can do here is within the forloop
  • 5:26 - 5:33
    we can redefine product to be equal to the product
  • 5:33 - 5:37
    the former product so our new product is going to be our
  • 5:37 - 5:39
    old product times, an I am going to multiply it times
  • 5:39 - 5:45
    i plus 1 times i plus one we are going to step through
  • 5:45 - 5:48
    carefully in the next video but I want you to think about
  • 5:48 - 5:49
    what it is doing already so right when we start
  • 5:49 - 5:55
    lets say that number ends up being 3 so product gets
  • 5:55 - 5:57
    set to 1 and we say for i in range an then if we do range
  • 5:57 - 6:05
    of 3 then i is first going to be 0 and so we are going to
  • 6:05 - 6:10
    take our old product so 1 times i + 1 well i is
  • 6:10 - 6:14
    going to be 0 so 0 + 1 is 1 so it is going to be 1 times
  • 6:14 - 6:16
    1 is going to be 1 then we are going to assign
  • 6:16 - 6:20
    i to be 1 this is what the forloop does it itterates
  • 6:20 - 6:23
    it keeps incramenting, well it dosen't allways have to
  • 6:23 - 6:25
    incrament and it keeps changeing the value of i
  • 6:25 - 6:28
    as it goes through this list and so the next time around
  • 6:28 - 6:32
    our product is one but now i is 1 so 1 + 1 is 2
  • 6:32 - 6:36
    time 1 is now going to be 2 an then it will go, it will set
  • 6:36 - 6:40
    i to be 2 so it is going to be 2+1 times what product was
  • 6:40 - 6:43
    from the last iteration which was 2 and so it is going to
  • 6:43 - 6:48
    be 2 times 3 which is 6 and so when we finally done
  • 6:48 - 6:52
    with this forloop inside of product or I guess what we can
  • 6:52 - 6:56
    say product will be refering to the acctual factorial of
  • 6:56 - 7:01
    an number inputed so then we can say print product
  • 7:01 - 7:06
    and in Python in this case right here the interpreter
  • 7:06 - 7:08
    knows to break out of this forloop once it runs out of
  • 7:08 - 7:12
    numbers to assign i to so thats why it doesn't run forever
  • 7:12 - 7:18
    so lets see if what we did here acctually works lets see
  • 7:18 - 7:23
    if it acctually works so let me write it like this so lets
  • 7:23 - 7:27
    see of this things acctually works so I'll save it and then
  • 7:27 - 7:32
    let me run it and so it is asking me to enter a non-negative
  • 7:32 - 7:36
    integer to take the factorial of so lets try it with 3
  • 7:36 - 7:39
    enter it gave me the right answer lets try it again
  • 7:39 - 7:44
    so enter a non-negative integer to take the factorial
  • 7:44 - 7:48
    of I don't know lets try something large 10, it worked
  • 7:48 - 7:51
    assuming that you can verify this for yourself so what
  • 7:51 - 7:53
    we have done here is write a very simple program but
  • 7:53 - 7:55
    it already does something neat it takes a factorial
  • 7:55 - 7:57
    of an arbitary number and then next video we are
  • 7:57 - 8:00
    going to step through much more carefully in case
  • 8:00 - 8:03
    this kind of confused you what this forloop did
  • 8:03 - 8:05
    so just hold with me for the next video and watch that
  • 8:05 -
    and that has a little bit of a more careful explanation
Title:
Writing a Simple Factorial Program. (Python 2)
Description:

Writing a simple factorial program with a "for" loop

more » « less
Video Language:
English
Duration:
08:09

English subtitles

Revisions Compare revisions