< Return to Video

Writing a Simple Factorial Program. (Python 2)

  • 0:00 - 0:05
    What I want to do in this video
    is introduce you to a “for loop.”
  • 0:05 - 0:10
    We are going to do this by writing a little program
    that calculates the factorial of a number.
  • 0:10 - 0:14
    And in case you don't remember what
    a factorial of a number does –
  • 0:14 - 0:21
    you normally write it as “1 factorial” –
    as 1, then an exclamation mark. ( 1! ) Like that:
  • 0:21 - 0:24
    That will just evaluate to 1.
  • 0:24 - 0:29
    If you say “2 factorial,” ( 2! ),
    that’s going to be equal to 2 × 1.
  • 0:29 - 0:33
    If you say “3 factorial,” ( 3! )
    that is going to be equal to
  • 0:33 - 0:41
    3 × 2 × 1, which is going to be equal to 6.
  • 0:41 - 0:44
    So, if you have any number’s factorial,
    it is just going to be that number,
  • 0:44 - 0:47
    times one number less than that,
    times one number less than that,
  • 0:47 - 0:49
    all the way down to 1.
  • 0:49 - 0:52
    So, with that out of the way,
    let’s implement our factorial program.
  • 0:52 - 0:56
    The way I am going to do it is
    I am going to take an input from the user.
  • 0:56 - 0:59
    It’s going to prompt the user to put
    a number that they want to take the factorial of;
  • 0:59 - 1:02
    and then it is going to calculate the factorial.
  • 1:02 - 1:07
    So what I am going to do is, I am going to use
    a function that Python provides for us.
  • 1:07 - 1:10
    And I'm using Python 2 in this video.
  • 1:10 - 1:13
    You'd have to do it slightly different —
    actually, I'll talk about that in a second —
  • 1:13 - 1:15
    if you are using Python 3.
  • 1:15 - 1:20
    Python 2 is what most implementations
    of Python are built in at the time of this video.
  • 1:20 - 1:23
    But there is a Python 3 out there
    that is kind of a newer version.
  • 1:23 - 1:28
    I clarify that a bit in a future video.
    But anyway –
  • 1:28 - 1:32
    We are going to take an input from the user,
    and we are going to prompt them with a message.
  • 1:32 - 1:44
    We are going to say, ”Enter a
    non-negative integer to take the factorial of.”
  • 1:44 - 1:48
    And just so that we familiarize
    ourselves with functions and all,
  • 1:48 - 1:50
    we are passing an argument to the function.
  • 1:50 - 1:53
    And the argument that we are
    passing to the function – to the input function
  • 1:53 - 1:56
    right over here is a string.
  • 1:56 - 2:00
    And “string” sounds like a very complicated thing;
    but it really is just a string of characters.
  • 2:00 - 2:02
    You can view it as a bunch of text.
  • 2:02 - 2:05
    And it will present this text to the user,
    and it will give them a little input box.
  • 2:05 - 2:08
    And the user will put something into the input box.
  • 2:08 - 2:10
    It will evaluate what is in that input box.
  • 2:10 - 2:15
    And then, you can either view it as
    having the variable number refer to it,
  • 2:15 - 2:21
    or put it in the variable number, depending
    on how you want to visualize the variable.
  • 2:21 - 2:27
    If you are using Python 3,
    you will want to call “eval”
  • 2:27 - 2:30
    on what’s returned from this function.
  • 2:30 - 2:31
    This is if you are doing Python 3.
  • 2:31 - 2:33
    I am running Python 2 right now.
  • 2:33 - 2:36
    And if you want to do it
    exactly the way I'm doing it,
  • 2:36 - 2:41
    I am using the PyScripter IDE —
    Integrated Development Environment.
  • 2:41 - 2:44
    It’s a free open-source project on a PC.
  • 2:44 - 2:47
    But there are many, many ways
    that you can edit Python.
  • 2:47 - 2:50
    You can really just do it
    on any text editor — so anyway.
  • 2:50 - 2:54
    So, so far, all we've done
    is we've taken input from the user
  • 2:54 - 2:56
    and we're putting it in the variable number.
  • 2:56 - 3:00
    And what’s really cool about Python
    is we can just run this program as is
  • 3:00 - 3:02
    and just see what happens.
    So let’s do that.
  • 3:02 - 3:06
    I'm going to save it and I'm going to run it.
    This little play button runs it in this IDE.
  • 3:06 - 3:10
    And just like we said,
    it prompts the user:
  • 3:10 - 3:12
    “Enter a non-negative integer
    to take the factorial of.”
  • 3:12 - 3:18
    So I don't know — le me type in 10.
    And I [click] OK. Nothing happened.
  • 3:18 - 3:20
    Well of course nothing happened!
    Because I didn't really tell this to do anything.
  • 3:20 - 3:26
    But if we type in a number, we'll
    see that it is now assigned to 10.
  • 3:26 - 3:28
    The variable is now
    referring to the number 10
  • 3:28 - 3:32
    So now we can use this number
    and take the factorial of it.
  • 3:32 - 3:36
    So what I'm going to do —
    and we're going to do it iteratively. —
  • 3:36 - 3:39
    And we are going to talk more
    about what that means in the future.
  • 3:39 - 3:42
    So I'm going to define
    another variable called product.
  • 3:42 - 3:44
    And I'm going to start product at 1.
  • 3:44 - 3:50
    And what we are going to do
    is start from 1 up to whatever number this is,
  • 3:50 - 3:54
    and keep multiplying the product
    by each successively larger number
  • 3:54 - 3:56
    So we are going to start –
  • 3:56 - 4:02
    So I am going to set up a for loop here —
    And in the next video I'm going to really step through
  • 4:02 - 4:04
    exactly what a for loop does.
  • 4:04 - 4:08
    Within the for loop, we are
    going to define a variable i.
  • 4:08 - 4:10
    This is kind of the classic
    variable to define in for loops.
  • 4:10 - 4:16
    And that variable is going to keep incrementing
    to larger and larger values as we go through it.
  • 4:16 - 4:22
    So “for i in range.” And for loops
    in Python are a little bit different
  • 4:22 - 4:24
    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.
  • 4:26 - 4:28
    But range of number —
  • 4:28 - 4:30
    And just to understand what
    range of number does is,
  • 4:30 - 4:34
    It provides a list of numbers
    starting with 0,
  • 4:34 - 4:36
    up to 1 less than that number.
  • 4:36 - 4:39
    So over here we already defined
    what number is — number is 10.
  • 4:39 - 4:43
    So if I type in range of 10 —
    or let me do range of number.
  • 4:43 - 4:47
    It gives me a list starting
    with 0 all the way to 9.
  • 4:47 - 4:48
    All the way to 1 less than than 10
  • 4:48 - 4:51
    So that the same thing as range of 10.
  • 4:51 - 4:57
    And what this for loop does is
    it assigns this i to each term of the sequence –
  • 4:57 - 5:01
    So it starts with 1 — or each term of this list.
    It starts with 1, and then it'll –
  • 5:01 - 5:08
    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.
  • 5:08 - 5:12
    Sorry. It will start with i equals 0,
    then it will go to i equals 1,
  • 5:12 - 5:16
    then i equals 2 then i equals 3 –
    so on and so forth – all the way
  • 5:16 - 5:19
    until you have gone all
    the way through the list.
  • 5:19 - 5:29
    And so what we can do here is — within the for loop — we can redefine
  • 5:29 - 5:34
    product to be equal to the
    product – the former product –
  • 5:34 - 5:39
    So our new product is going to be
    our old product times –
  • 5:39 - 5:44
    and I am going to multiply it
    times i + 1.
  • 5:44 - 5:46
    We are going to step through
    it carefully in the next video.
  • 5:46 - 5:48
    But I want you to think
    about what it is doing already.
  • 5:48 - 5:54
    So right when we start, let’s
    say that number ends up being 3.
  • 5:54 - 5:56
    So product gets set to 1.
  • 5:56 - 5:58
    And we say for i in range.
  • 5:58 - 6:04
    And then, if we do range of 3,
    then i is first going to be 0.
  • 6:04 - 6:09
    And so we are going to take our old product –
    so 1 – times i + 1.
  • 6:09 - 6:12
    Well i is going to be 0 so 0 + 1 is 1.
  • 6:12 - 6:15
    So it is going to be 1 × 1,
    [which] is going to be 1.
  • 6:15 - 6:18
    Then we are going to assign i to be 1.
  • 6:18 - 6:19
    This is what the for loop does.
  • 6:19 - 6:20
    It iterates.
  • 6:20 - 6:22
    It keeps incrementing,
  • 6:22 - 6:23
    (Well it doesn't always have to increment.)
  • 6:23 - 6:27
    It keeps changing the value of i
    as it goes through this list.
  • 6:27 - 6:29
    And so the next time
    around our product is 1.
  • 6:29 - 6:35
    But now i is 1. So 1 plus 1 is 2, times 1
    is now going to be 2.
  • 6:35 - 6:37
    And then it will go – it will set i to be 2.
  • 6:37 - 6:43
    So it is going to be 2+1 times what product
    was from the last iteration – which was 2.
  • 6:43 - 6:47
    And so it is going to be 2 × 3 which is 6
  • 6:47 - 6:51
    And so when we are finally done
    with this for loop, inside of product –
  • 6:51 - 6:54
    or I guess we can say product
    will be referring to
  • 6:54 - 6:57
    the actual factorial of the number inputted.
  • 6:57 - 7:01
    So then we can say print product.
  • 7:01 - 7:06
    And in Python – in this case right here –
    the interpreter knows to break out
  • 7:06 - 7:10
    of this for loop once it runs
    out of numbers to assign i to.
  • 7:10 - 7:13
    So, that’s why it doesn't run forever.
  • 7:13 - 7:17
    Let’s see if what we
    did here actually works.
  • 7:17 - 7:19
    Let’s see if it actually works.
  • 7:19 - 7:25
    So let – actually – let me write it like this.
    So let’s see if this things actually works.
  • 7:25 - 7:30
    So I'll save it.
    And then let me run it.
  • 7:30 - 7:34
    And so it’s asking me, “Enter a
    non-negative integer to take the factorial of.”
  • 7:34 - 7:36
    So let’s try it with 3 – Enter.
  • 7:36 - 7:37
    It gave me the right answer.
  • 7:37 - 7:40
    Let’s try it again.
  • 7:40 - 7:44
    So, “Enter a non-negative
    integer to take the factorial of.”
  • 7:44 - 7:47
    I don't know, let’s
    try something large: 10.
  • 7:47 - 7:51
    It worked. [I'm] assuming that
    you can verify this for yourself.
  • 7:51 - 7:53
    So what we have done here
    is write a very simple program.
  • 7:53 - 7:56
    But, it already does something neat –
    it takes a factorial of an arbitrary number.
  • 7:56 - 7:59
    In the next video, we're going to
    step through it much more carefully,
  • 7:59 - 8:03
    in case this kind of confused you –
    what this for loop did.
  • 8:03 - 8:05
    So, just hold with me for
    the next video, and watch that.
  • 8:05 - 8:11
    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