< Return to Video

While Loops in Python | Python for Beginners

  • 0:00 - 0:01
    INSTRUCTOR: Hello, everybody.
  • 0:01 - 0:04
    Today we're going to be taking
    a look at While loops in Python.
  • 0:04 - 0:07
    The While loop in Python is used
    to iterate over a block of code
  • 0:07 - 0:09
    as long as the test
    condition is true.
  • 0:09 - 0:12
    Now, the difference between
    a For loop and a While loop
  • 0:12 - 0:13
    is that a For loop
    is going to iterate
  • 0:13 - 0:16
    over the entire sequence,
    regardless of a condition,
  • 0:16 - 0:19
    but the While loop is only going
    to iterate over that sequence as
  • 0:19 - 0:21
    long as a specific
    condition is met.
  • 0:21 - 0:24
    Once that condition is not
    met, the code is going to stop.
  • 0:24 - 0:25
    And it's not going to
    iterate through the rest
  • 0:25 - 0:26
    of the sequence.
  • 0:26 - 0:28
    So if we take a look at
    this flowchart right here,
  • 0:28 - 0:30
    we're going to enter
    this While loop.
  • 0:30 - 0:32
    And we have a test
    condition right here.
  • 0:32 - 0:34
    The first time that this test
    condition comes back false,
  • 0:34 - 0:35
    it's going to exit
    the While loop.
  • 0:35 - 0:37
    So let's start actually
    writing out the code
  • 0:37 - 0:39
    and see how this
    While loop works.
  • 0:39 - 0:40
    So let's create a variable.
  • 0:40 - 0:43
    We're just going to say,
    number is equal to 1.
  • 0:43 - 0:44
    And then we'll say While.
  • 0:44 - 0:46
    And now we need to
    write our condition that
  • 0:46 - 0:48
    needs to be met in order
    for our block of code
  • 0:48 - 0:49
    beneath this to run.
  • 0:49 - 0:53
    So we're going to say,
    While number is less than 5,
  • 0:53 - 0:55
    and then we'll do colon, Enter.
  • 0:55 - 0:57
    And now this is
    our block of code.
  • 0:57 - 0:58
    We're going to say Print.
  • 0:58 - 0:59
    And then we'll say, number.
  • 0:59 - 1:02
    Now what we need to do is
    basically create a counter.
  • 1:02 - 1:06
    We're going to say, number
    equals number plus 1.
  • 1:06 - 1:08
    If you've never done something
    like this, it's like a counter.
  • 1:08 - 1:10
    Most people start it at 0.
  • 1:10 - 1:11
    In fact, let's start it at 0.
  • 1:11 - 1:14
    And then each time it runs
    through this While loop,
  • 1:14 - 1:16
    it's going to add 1 to
    this number up here.
  • 1:16 - 1:19
    And then it's going to
    become a 1, a 2, a 3 each
  • 1:19 - 1:21
    time it iterates
    through this While loop.
  • 1:21 - 1:25
    Now, once this number is
    no longer less than 5,
  • 1:25 - 1:26
    it'll break out
    of the While loop.
  • 1:26 - 1:28
    And it will no longer run.
  • 1:28 - 1:31
    So let's run this really
    quick by hitting Shift-Enter.
  • 1:31 - 1:32
    So it starts at 0.
  • 1:32 - 1:35
    And it's going to say, While
    the number is less than 5,
  • 1:35 - 1:36
    Print number.
  • 1:36 - 1:38
    So the first time that
    it runs through, it is 0.
  • 1:38 - 1:40
    And so it prints 0.
  • 1:40 - 1:42
    And then it adds 1 to Number.
  • 1:42 - 1:45
    And then it continues that
    While loop right here.
  • 1:45 - 1:47
    And it keeps looping
    through this portion.
  • 1:47 - 1:49
    It never goes back up
    here to this line of code.
  • 1:49 - 1:51
    This is just our variable
    that we start with.
  • 1:51 - 1:55
    And then once this condition is
    no longer met, once it is false,
  • 1:55 - 1:57
    then it's going to
    break out of that code.
  • 1:57 - 1:59
    Now that we basically know
    how a While loop works,
  • 1:59 - 2:02
    let's look at something
    called a Break statement.
  • 2:02 - 2:03
    So let's copy this
    right down here.
  • 2:03 - 2:09
    And what we're going to say
    is, if number is equal to 3,
  • 2:09 - 2:10
    we're going to Break.
  • 2:10 - 2:11
    Now, with the
    Break statement, we
  • 2:11 - 2:15
    can basically stop the loop even
    if the While condition is true.
  • 2:15 - 2:17
    So while this number
    is less than 5,
  • 2:17 - 2:19
    it's going to continue
    to loop through.
  • 2:19 - 2:21
    But now we have this
    Break statement.
  • 2:21 - 2:23
    So it's going to say,
    if the Number equals 3,
  • 2:23 - 2:25
    we're going to break
    out of this While loop.
  • 2:25 - 2:27
    But if this is
    false, we're going
  • 2:27 - 2:30
    to continue adding to that
    number just like normal.
  • 2:30 - 2:31
    So let's execute this.
  • 2:31 - 2:34
    So as you can see, it only
    went to 3 instead of 4
  • 2:34 - 2:36
    like before because
    each time it was
  • 2:36 - 2:38
    running through this While
    loop, it was checking
  • 2:38 - 2:40
    if the number was equal to 3.
  • 2:40 - 2:42
    And once it got to
    3, this became true.
  • 2:42 - 2:44
    And then we broke out
    of this While loop.
  • 2:44 - 2:46
    The next thing that I want to
    look at-- and we'll copy this
  • 2:46 - 2:47
    right down here--
  • 2:47 - 2:50
    is an Else statement,
    much like an If statement.
  • 2:50 - 2:53
    But we can use the Else
    statement with a While loop,
  • 2:53 - 2:54
    which runs the block of code.
  • 2:54 - 2:56
    And when that condition
    is no longer true,
  • 2:56 - 2:58
    then it activates
    the Else statement.
  • 2:58 - 2:59
    So we'll go right down here.
  • 2:59 - 3:01
    And we'll say, Else.
  • 3:01 - 3:03
    And we'll do a colon and Enter.
  • 3:03 - 3:05
    And then, we'll say Print.
  • 3:05 - 3:10
    And we'll say, no
    longer less than 5.
  • 3:10 - 3:13
    Now because this If statement is
    still in there, it will break.
  • 3:13 - 3:15
    So let's say, 6.
  • 3:15 - 3:17
    And then we'll run this.
  • 3:17 - 3:19
    And so it's going to iterate
    through this block of code.
  • 3:19 - 3:21
    And once this statement
    is no longer true,
  • 3:21 - 3:23
    once we break out
    of it, we're going
  • 3:23 - 3:25
    to go to our Else statement.
  • 3:25 - 3:26
    Now, as long as this
    statement is true,
  • 3:26 - 3:28
    it's going to continue
    to iterate through.
  • 3:28 - 3:30
    But once this
    condition is not met,
  • 3:30 - 3:32
    then it will go to
    our Else statement.
  • 3:32 - 3:33
    And we'll run that line of code.
  • 3:33 - 3:36
    Now the Else statement is
    only going to trigger if the
  • 3:36 - 3:38
    While loop no longer is true.
  • 3:38 - 3:39
    If we have something
    like this If statement
  • 3:39 - 3:42
    that causes it to break
    out of the While loop,
  • 3:42 - 3:44
    the Else statement
    will no longer work.
  • 3:44 - 3:47
    So let's say, if the number
    is 3, and we run this,
  • 3:47 - 3:49
    the Else statement is no
    longer going to trigger.
  • 3:49 - 3:51
    So this body of code
    will not be run.
  • 3:51 - 3:52
    Now, the next thing
    that I want to look at
  • 3:52 - 3:54
    is the Continue statement.
  • 3:54 - 3:55
    If the Continue
    statement is triggered,
  • 3:55 - 3:58
    it basically rejects
    all remaining statements
  • 3:58 - 3:59
    in the current
    iteration of the loop.
  • 3:59 - 4:01
    And then we'll go to
    the next iteration.
  • 4:01 - 4:03
    Now to demonstrate
    this, I'm going
  • 4:03 - 4:05
    to change this Break
    into a Continue.
  • 4:05 - 4:08
    So before when we had the Break,
    if the number was equal to 3,
  • 4:08 - 4:11
    it would stop all
    the code completely.
  • 4:11 - 4:14
    But when we change this
    to Continue, which we'll
  • 4:14 - 4:16
    do right now, what
    it's going to do
  • 4:16 - 4:18
    is, it's no longer
    going to run through any
  • 4:18 - 4:20
    of the subsequent code
    in this block of code,
  • 4:20 - 4:22
    it's just going to go
    straight up to the beginning
  • 4:22 - 4:24
    and restart our While loop.
  • 4:24 - 4:25
    So what's going
    to happen when we
  • 4:25 - 4:29
    run this is, it's going to come
    to 3, it's going to become 3.
  • 4:29 - 4:31
    And it's going to continue
    back into the While loop.
  • 4:31 - 4:33
    But it's never going to
    have that number changed
  • 4:33 - 4:36
    to be add it to 1 to
    continue with the While loop.
  • 4:36 - 4:39
    This will basically
    create an infinite loop.
  • 4:39 - 4:40
    Let's try this really quickly.
  • 4:40 - 4:43
    And as you can see, it's
    going to stay 3 forever.
  • 4:43 - 4:45
    Eventually, this would time
    out, but I'm just going
  • 4:45 - 4:46
    to stop the code really quick.
  • 4:46 - 4:49
    So if we just change up
    the order of which we're
  • 4:49 - 4:53
    doing things, we're
    going to say, there,
  • 4:53 - 4:54
    and we're going to
    put this down here.
  • 4:54 - 4:56
    So what it's going
    to do now, instead
  • 4:56 - 4:58
    of printing the number
    immediately and then
  • 4:58 - 5:00
    adding the number
    later, we're going
  • 5:00 - 5:02
    to add the number right away.
  • 5:02 - 5:04
    And then we're going
    to say, if it is 3,
  • 5:04 - 5:05
    we're going to continue.
  • 5:05 - 5:06
    And it's going to
    print the number.
  • 5:06 - 5:08
    So let's try executing
    this and see what happens.
  • 5:08 - 5:12
    So as you can see, we no longer
    have the 3 in our output.
  • 5:12 - 5:14
    What it did was, when
    we got to the number 3,
  • 5:14 - 5:17
    it continued and didn't
    execute this right here,
  • 5:17 - 5:18
    which prints off that number.
  • 5:18 - 5:21
    So that really is the
    basics of the While loop.
  • 5:21 - 5:22
    I hope that this was helpful.
  • 5:22 - 5:24
    I hope that you learned
    something in this video.
  • 5:24 - 5:26
    If you did, be sure to
    and subscribe below.
  • 5:26 - 5:28
    And I'll see you
    in the next video.
  • 5:28 - 5:40
Title:
While Loops in Python | Python for Beginners
Description:

more » « less
Video Language:
English
Duration:
05:40

English subtitles

Revisions