< Return to Video

Writing a Simple Factorial Program. (Python 2)

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