< Return to Video

Writing a Simple Factorial Program. (Python 2)

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