< Return to Video

Python For Loops - Python Tutorial for Absolute Beginners

  • 0:00 - 0:03
    [MUSIC PLAYING]
  • 0:03 - 0:04
  • 0:04 - 0:05
    MOSH HAMEDANI: There
    are times that we
  • 0:05 - 0:08
    may want to repeat a
    task a number of times.
  • 0:08 - 0:11
    For example, let's say we
    send a message to a user.
  • 0:11 - 0:14
    If that message
    cannot be delivered,
  • 0:14 - 0:16
    perhaps we want to
    retry three times.
  • 0:16 - 0:19
    Now, for simplicity,
    let's imagine
  • 0:19 - 0:24
    this print statement is
    equivalent to sending a message.
  • 0:24 - 0:26
    In a real-world program, to
    send a message to a user,
  • 0:26 - 0:29
    we have to write 5
    to 10 lines of code.
  • 0:29 - 0:31
    Now, if you want to
    retry three times,
  • 0:31 - 0:33
    we don't want to
    repeat all that code.
  • 0:33 - 0:34
    That is ugly.
  • 0:34 - 0:36
    That's when we use a loop.
  • 0:36 - 0:38
    We use loops to
    create repetition.
  • 0:38 - 0:41
    So here is how it works.
  • 0:41 - 0:44
    We start with for, number, in.
  • 0:44 - 0:47
    We have a built-in
    function called range.
  • 0:47 - 0:50
    Now how many times do we
    want to repeat this task?
  • 0:50 - 0:51
    Let's say three times.
  • 0:51 - 0:55
    So we call range and
    pass 3 as an argument.
  • 0:55 - 0:57
    Now, similar to
    our if statements,
  • 0:57 - 1:01
    we need to terminate
    this line with a colon.
  • 1:01 - 1:03
    Enter-- we get indentation.
  • 1:03 - 1:07
    So in this block, we can
    write all the statements that
  • 1:07 - 1:08
    should be repeated three times.
  • 1:08 - 1:12
    Let's do a print, a
    message like attempt.
  • 1:12 - 1:14
    Save the changes.
  • 1:14 - 1:15
    Run the program.
  • 1:15 - 1:18
    So we have attempt
    printed three times.
  • 1:18 - 1:19
    Beautiful.
  • 1:19 - 1:21
    Now, what is this number?
  • 1:21 - 1:23
    Let's take a look.
  • 1:23 - 1:25
    It's a variable of type integer.
  • 1:25 - 1:29
    So let's pass it as the second
    argument to the print function.
  • 1:29 - 1:31
    Number.
  • 1:31 - 1:33
    Run the program.
  • 1:33 - 1:34
    This is what we get--
  • 1:34 - 1:36
    0, 1, 2.
  • 1:36 - 1:38
    So here we have a for loop.
  • 1:38 - 1:41
    This for loop is
    executed three times.
  • 1:41 - 1:45
    In each iteration, number
    will have a different value.
  • 1:45 - 1:46
    Initially it will be 0.
  • 1:46 - 1:48
    In the second
    iteration, it will be 1.
  • 1:48 - 1:51
    And finally, in the last
    iteration, it will be 2.
  • 1:51 - 1:53
    Now here we can
    do something fun.
  • 1:53 - 1:57
    We can add 1 to this.
  • 1:57 - 1:58
    Run the program.
  • 1:58 - 2:00
    And now the messages
    that we print
  • 2:00 - 2:03
    are more meaningful
    or more user-friendly,
  • 2:03 - 2:07
    like attempting number 1,
    attempting number 2, and so on.
  • 2:07 - 2:09
    We can take this
    to the next level.
  • 2:09 - 2:11
    So we can pass another argument.
  • 2:11 - 2:13
    Here, I'm going to
    add an expression.
  • 2:13 - 2:14
    One more time.
  • 2:14 - 2:15
    Number plus 1.
  • 2:15 - 2:17
    So we'll get 1, 2, 3.
  • 2:17 - 2:20
    Now I want to put this
    expression in parentheses.
  • 2:20 - 2:23
    So let's select this.
  • 2:23 - 2:29
    Put it in parentheses, and
    then multiply it by a dot.
  • 2:29 - 2:32
    So here we have a string that
    is multiplied by a number.
  • 2:32 - 2:36
    The result will be that string
    repeated that number of times.
  • 2:36 - 2:37
    Let's take a look.
  • 2:37 - 2:39
    So run the program.
  • 2:39 - 2:40
    See.
  • 2:40 - 2:42
    That's pretty cool, isn't it?
  • 2:42 - 2:44
    Now, let me show
    you one more thing
  • 2:44 - 2:45
    before we finish this lecture.
  • 2:45 - 2:48
    As you saw, this range
    function generates
  • 2:48 - 2:52
    numbers starting from 0 all
    the way up to this number here,
  • 2:52 - 2:53
    but it doesn't
    include this number.
  • 2:53 - 2:58
    Here we can pass another
    argument, say, start from 1
  • 2:58 - 3:00
    and finish before 4.
  • 3:00 - 3:03
    With this change, we don't
    need to add 1 to number
  • 3:03 - 3:06
    every time, because in
    the first iteration,
  • 3:06 - 3:09
    this number variable
    will be set to 1.
  • 3:09 - 3:13
    So we can simplify our
    code and make it cleaner.
  • 3:13 - 3:14
    Let's run it one more time.
  • 3:14 - 3:19
    We get the exact same result. We
    can also pass a third argument
  • 3:19 - 3:20
    as a step.
  • 3:20 - 3:24
    So I'm going to change
    the second argument to 10
  • 3:24 - 3:26
    and pass 2 as a step.
  • 3:26 - 3:30
    Look at the result. These
    are the numbers we get--
  • 3:30 - 3:32
    1, 3, 5, and so on.
  • 3:32 - 3:33
    So pretty useful.
  • 3:33 - 3:34
    You're going to use
    this function a lot
  • 3:34 - 3:36
    in real-world applications.
  • 3:36 - 3:39
  • 3:39 - 3:42
    [MUSIC PLAYING]
  • 3:42 - 3:43
  • 3:43 - 3:46
    Continuing with the example
    from the last lecture,
  • 3:46 - 3:49
    let's imagine the scenario
    where after the first attempt
  • 3:49 - 3:51
    we can successfully
    send the message.
  • 3:51 - 3:54
    In that case, we want to
    jump out of this loop.
  • 3:54 - 3:58
    We don't want to repeat this
    task of sending a message three
  • 3:58 - 3:59
    times.
  • 3:59 - 4:01
    Let me show you how
    to implement this.
  • 4:01 - 4:04
    So in this demo, I'm going to
    simulate the scenario where
  • 4:04 - 4:07
    we can successfully
    send a message.
  • 4:07 - 4:13
    So we define a variable,
    successful, and set it to true.
  • 4:13 - 4:16
    Now here after this
    print statement,
  • 4:16 - 4:18
    we'll have an if statement.
  • 4:18 - 4:26
    If successful, colon, then
    perhaps we can print successful.
  • 4:26 - 4:29
    Now here we want to
    jump out of this loop.
  • 4:29 - 4:31
    For that, we use
    the break statement.
  • 4:31 - 4:34
    Let's run this program
    and see what happens.
  • 4:34 - 4:36
    So there you go.
  • 4:36 - 4:38
    After the first attempt,
    we are successful,
  • 4:38 - 4:42
    and there are no more attempts.
  • 4:42 - 4:44
    So once again, I want you
    to pay great attention
  • 4:44 - 4:47
    to the indentation here because
    that's one of the common issues
  • 4:47 - 4:49
    amongst beginners.
  • 4:49 - 4:51
    So here's for loop.
  • 4:51 - 4:54
    These two lines are
    indented with four spaces,
  • 4:54 - 4:56
    and they belong to a for loop.
  • 4:56 - 5:00
    In every iteration, these
    two lines will be executed.
  • 5:00 - 5:04
    Now when we get to line 4,
    if this condition is true,
  • 5:04 - 5:07
    then these two lines
    will be executed
  • 5:07 - 5:12
    because both these lines
    are indented below this if
  • 5:12 - 5:13
    statement.
  • 5:13 - 5:15
    Now let's take this
    program to the next level.
  • 5:15 - 5:19
    What if we attempt three
    times and we still cannot send
  • 5:19 - 5:20
    an email?
  • 5:20 - 5:22
    Perhaps we want to display a
    different message to the user.
  • 5:22 - 5:26
    We say, hey, we tried three
    times, but it didn't work.
  • 5:26 - 5:31
    So I'm going to change
    successful to false.
  • 5:31 - 5:35
    Now at the end, here we
    can add an else statement.
  • 5:35 - 5:38
    This is what we call a
    false else statement.
  • 5:38 - 5:40
    What we put under
    this else statement
  • 5:40 - 5:43
    will only be executed
    if this loop completes
  • 5:43 - 5:45
    without an early termination.
  • 5:45 - 5:48
    So if we never break
    out of this loop,
  • 5:48 - 5:51
    then the else statement
    will be executed.
  • 5:51 - 5:56
    So here we can print a message
    like attempted three times
  • 5:56 - 5:57
    and failed.
  • 5:57 - 6:02
    So run the program.
  • 6:02 - 6:03
    See what we get.
  • 6:03 - 6:07
    Three attempts followed by this
    message-- attempted three times
  • 6:07 - 6:08
    and failed.
  • 6:08 - 6:12
    In contrast, if we change
    successful to true,
  • 6:12 - 6:15
    because we terminate this loop
    using this break statement,
  • 6:15 - 6:19
    what we have in the else
    block will not be executed.
  • 6:19 - 6:20
    Take a look.
  • 6:20 - 6:21
    Run the program.
  • 6:21 - 6:23
    We have one attempt.
  • 6:23 - 6:23
    Successful.
  • 6:23 - 6:24
    Done.
  • 6:24 - 6:27
  • 6:27 - 6:30
    [MUSIC PLAYING]
  • 6:30 - 6:32
  • 6:32 - 6:35
    In programming, we have this
    concept called nested loops.
  • 6:35 - 6:38
    So we can put one loop
    inside of another loop.
  • 6:38 - 6:41
    And with this, we can get
    some interesting results.
  • 6:41 - 6:42
    Let me show you.
  • 6:42 - 6:49
    So I'm going to start with this
    loop-- for x in range 5 colon.
  • 6:49 - 6:53
    Now inside of this loop, I'm
    going to add another loop.
  • 6:53 - 6:59
    So for y in range 3 colon.
  • 6:59 - 7:03
    And then in our second loop, I'm
    going to add a print statement.
  • 7:03 - 7:08
    Here we can use formatted
    strings to display coordinates.
  • 7:08 - 7:12
    Remember formatted string, so
    we have f followed by quotes.
  • 7:12 - 7:16
    Now here we add parentheses
    for our coordinate.
  • 7:16 - 7:22
    First, we want to display x
    and then comma followed by y.
  • 7:22 - 7:25
    Let's run this program
    and see what happens.
  • 7:25 - 7:26
    There you go.
  • 7:26 - 7:28
    Pretty cool, isn't it.
  • 7:28 - 7:31
    So we get 0 and 0,
    0 and 1, 0 and 2.
  • 7:31 - 7:36
    Then we get 1 and 0, 1
    and 1, 1 and 2, and so on.
  • 7:36 - 7:40
    Now let me explain how exactly
    Python interpreter executes
  • 7:40 - 7:41
    this code.
  • 7:41 - 7:43
    So here we have two loops.
  • 7:43 - 7:45
    This is what we
    call the outer loop,
  • 7:45 - 7:47
    and this is the inner loop.
  • 7:47 - 7:50
    So the execution of our
    program starts here.
  • 7:50 - 7:54
    In the first iteration
    of this loop, x is 0.
  • 7:54 - 7:56
    Now we get to this
    statement, which
  • 7:56 - 8:00
    is a child of this for
    statement because it's
  • 8:00 - 8:02
    indented four times.
  • 8:02 - 8:05
    This statement itself is a loop.
  • 8:05 - 8:07
    So what we have
    inside of this loop
  • 8:07 - 8:10
    will be executed three times.
  • 8:10 - 8:14
    In the first iteration, x
    is 0 because we're still
  • 8:14 - 8:17
    in the first iteration
    of the outer loop.
  • 8:17 - 8:21
    And y is also 0 because we
    are in the first iteration
  • 8:21 - 8:22
    of the inner loop.
  • 8:22 - 8:25
    That is why we get 0 and 0.
  • 8:25 - 8:29
    Now we go to the second
    iteration of this inner loop.
  • 8:29 - 8:33
    In this iteration, y will
    be 1, whereas x is still 0.
  • 8:33 - 8:36
    That is why we get 0 and 1.
  • 8:36 - 8:39
    And similarly, in the third
    iteration of our inner loop,
  • 8:39 - 8:42
    we'll get 0 and 2
    in the terminal.
  • 8:42 - 8:45
    Now we're done with the
    execution of the inner loop.
  • 8:45 - 8:48
    So the control moves
    back to our outer loop.
  • 8:48 - 8:50
    Here will be in the
    second iteration.
  • 8:50 - 8:52
    So x will be 1.
  • 8:52 - 8:54
    And then we start here again.
  • 8:54 - 8:58
    So we have to execute this
    inner loop three times.
  • 8:58 - 9:02
    In the first iteration,
    y will be 0, and x is 1.
  • 9:02 - 9:04
    So here we have 1 and 0.
  • 9:04 - 9:06
    Then we'll get 1
    and 1, and 1 and 2.
  • 9:06 - 9:08
    You got the point.
  • 9:08 - 9:10
    So this is all
    about nested loops.
  • 9:10 - 9:14
  • 9:14 - 9:17
    [MUSIC PLAYING]
  • 9:17 - 9:18
  • 9:18 - 9:20
    So you have learned
    how to use for loops
  • 9:20 - 9:23
    to repeat one or more
    statements in your programs.
  • 9:23 - 9:25
    Now let's dive
    deeper and see what
  • 9:25 - 9:27
    this range function returns.
  • 9:27 - 9:33
    So earlier, you learned about
    the built-in type function.
  • 9:33 - 9:36
    With this function, we can
    get the type of an object.
  • 9:36 - 9:42
    So if I pass 5 here and run this
    program, this is what we get.
  • 9:42 - 9:46
    So the type of this number or
    this object is int or integer.
  • 9:46 - 9:50
    Now let's look at
    the type of the value
  • 9:50 - 9:52
    that we get from
    the range function.
  • 9:52 - 9:57
    So as an argument, we
    pass range of a number.
  • 9:57 - 9:59
    Let's run this program.
  • 9:59 - 10:04
    So this range function returns
    an object of type range.
  • 10:04 - 10:09
    So in Python, we have primitive
    types like numbers, strings,
  • 10:09 - 10:10
    and Booleans.
  • 10:10 - 10:12
    But we also have complex types.
  • 10:12 - 10:15
    Range is an example of one
    of those complex types.
  • 10:15 - 10:17
    Throughout this
    course, you're going
  • 10:17 - 10:20
    to learn about a lot
    of other complex types.
  • 10:20 - 10:23
    Now what is interesting
    about this range object
  • 10:23 - 10:27
    is that it's iterable, which
    means we can iterate over
  • 10:27 - 10:30
    it or use it in a for loop.
  • 10:30 - 10:35
    That is why we can
    write code like this.
  • 10:35 - 10:38
    So this range function
    returns a range object
  • 10:38 - 10:42
    which is iterable, which
    means we can iterate over it.
  • 10:42 - 10:46
    In each iteration, x will
    have a different value.
  • 10:46 - 10:51
    Now, range objects are not the
    only iterable objects in Python.
  • 10:51 - 10:53
    Strings are also iterable.
  • 10:53 - 10:59
    So here we can add a
    string like Python.
  • 10:59 - 11:01
    Now in each
    iteration, x will hold
  • 11:01 - 11:04
    one character in this string.
  • 11:04 - 11:04
    Let me show you.
  • 11:04 - 11:10
    So print, x, and I'm going to
    delete these two lines here.
  • 11:10 - 11:13
    Let's run this program.
  • 11:13 - 11:17
    So in each iteration, we'll
    get one character and print it.
  • 11:17 - 11:21
    We have another complex
    type called list which we
  • 11:21 - 11:23
    use to store a list of objects.
  • 11:23 - 11:26
    So we add square brackets.
  • 11:26 - 11:27
    This indicates a list.
  • 11:27 - 11:30
    Now we can add a list of
    numbers or a list of strings,
  • 11:30 - 11:32
    like a list of names.
  • 11:32 - 11:35
    You will learn about
    lists later in the course.
  • 11:35 - 11:38
    So let's run this one more time.
  • 11:38 - 11:40
    As you can see, we can
    iterate over lists.
  • 11:40 - 11:45
    In each iteration, we'll
    get one object in this list.
  • 11:45 - 11:47
    Now, later in the
    course, I will show you
  • 11:47 - 11:51
    how to create your own custom
    objects that are iterable.
  • 11:51 - 11:54
    For example, you will learn
    how to write code like this--
  • 11:54 - 12:01
    for item in shopping
    cart, print item.
  • 12:01 - 12:03
    So shopping cart is
    going to be a custom
  • 12:03 - 12:05
    object that you will create.
  • 12:05 - 12:08
    It's not going to be an
    integer or a string or Boolean.
  • 12:08 - 12:09
    It's a custom object.
  • 12:09 - 12:11
    It has a different
    structure, and we
  • 12:11 - 12:15
    will make it iterable so we
    can use it in a for loop.
  • 12:15 - 12:16
    And in each
    iteration, we can get
  • 12:16 - 12:20
    one item in the shopping cart
    and print it on the terminal.
  • 12:20 - 12:23
  • 12:23 - 12:26
    [MUSIC PLAYING]
  • 12:26 - 12:28
  • 12:28 - 12:30
    All right, time for an exercise.
  • 12:30 - 12:34
    I want you to write a program
    to display the even numbers
  • 12:34 - 12:35
    between 1 to 10.
  • 12:35 - 12:39
    So when you run this program,
    you should see 2, 4, 6, and 8.
  • 12:39 - 12:42
    And after these, I want
    you to print this message--
  • 12:42 - 12:45
    We have four even numbers.
  • 12:45 - 12:48
    Now here is a quick hint
    before you get started.
  • 12:48 - 12:51
    You should call the range
    function with 1 and 10.
  • 12:51 - 12:54
    Do not use the third argument,
    which is called step.
  • 12:54 - 12:57
    So basically, I want you to
    iterate over all the numbers
  • 12:57 - 12:59
    between 1 to 10.
  • 12:59 - 13:01
    Check if each number
    is an even number
  • 13:01 - 13:04
    and then print it
    on the terminal.
  • 13:04 - 13:05
    So pause the video.
  • 13:05 - 13:07
    Spend two minutes
    on this exercise.
  • 13:07 - 13:08
    When you're done, come back.
  • 13:08 - 13:09
    Continue watching.
  • 13:09 - 13:13
  • 13:13 - 13:15
    So we start with a for loop--
  • 13:15 - 13:20
    for number in range
    1 to 10 colon.
  • 13:20 - 13:25
    We check to see if the remainder
    of division of this number by 2
  • 13:25 - 13:26
    equals 0.
  • 13:26 - 13:35
    So if number modulus 2 equals
    0, then we print this number.
  • 13:35 - 13:38
    Now let's run this program.
  • 13:38 - 13:40
    So we get 2, 4, 6, 8.
  • 13:40 - 13:40
    Beautiful!
  • 13:40 - 13:45
    Now to count the even numbers,
    we need a separate variable.
  • 13:45 - 13:47
    So let's call that count.
  • 13:47 - 13:49
    Initially, we set it to 0.
  • 13:49 - 13:52
    Now in this if block, every
    time we find an even number,
  • 13:52 - 13:54
    we need to increment count.
  • 13:54 - 13:58
    So we set count plus equals 1.
  • 13:58 - 14:04
    And finally, after for loop, we
    can print a formatted string.
  • 14:04 - 14:09
    We have count even numbers.
  • 14:09 - 14:11
    Let's run the program.
  • 14:11 - 14:16
    And here's the result.
  • 14:16 - 14:17
    Hi, guys.
  • 14:17 - 14:19
    Thank you for watching
    this tutorial.
  • 14:19 - 14:21
    My name is Mosh
    Hamedani, and I have
  • 14:21 - 14:23
    tons of tutorials like
    this for you on my channel.
  • 14:23 - 14:25
    So be sure to subscribe!
  • 14:25 - 14:27
    And also please like
    and share this video.
  • 14:27 - 14:29
    If you want to learn
    Python properly
  • 14:29 - 14:33
    from scratch with depth, I have
    a comprehensive Python tutorial
  • 14:33 - 14:34
    for you.
  • 14:34 - 14:35
    The link is below this video.
  • 14:35 - 14:37
    So click the link
    to get started.
  • 14:37 - 14:40
    Thank you, and have
    a fantastic day!
  • 14:40 - 14:41
Title:
Python For Loops - Python Tutorial for Absolute Beginners
Description:

more » « less
Video Language:
English
Duration:
14:42

English subtitles

Revisions