Return to Video

Python for Informatics: Chapter 5 - Iterations

  • 0:04 - 0:08
    As always, this lecture is copyright
    Creative Commons Attribution, including
  • 0:08 - 0:13
    the audio and video and, and the
    slides, and the book even.
  • 0:13 - 0:19
    So, now we're getting to our
    fourth basic pattern.
  • 0:19 - 0:22
    We've talked about sequential, where
    steps happen one after another.
  • 0:22 - 0:25
    We've talked about conditional, where
    steps may or may not happen.
  • 0:25 - 0:28
    In Chapter Four we talked about
    the store and retrieve pattern.
  • 0:28 - 0:30
    And now we're going to talk
    about the looping pattern.
  • 0:30 - 0:34
    And the looping pattern is the last
    of our really foundational ones, and
  • 0:34 - 0:39
    it potentially is the most important one,
    because it's the thing that allows us
  • 0:39 - 0:42
    to get computers to do lots of
    things that, say,
  • 0:42 - 0:46
    humans might get tired of,
    but computers don't tire of.
  • 0:46 - 0:50
    And so after this we'll start
    sort of becoming a little more
  • 0:50 - 0:53
    skilled in the basic language
    capabilities.
  • 0:53 - 0:56
    We'll talk about strings and, and
    then start to talk about files.
  • 0:56 - 1:01
    Add start doing some real work after we
    get done with this, so bear with us.
  • 1:01 - 1:03
    It's going to be a while,
    but we'll get there.
  • 1:03 - 1:07
    We'll get there.
    So, welcome to repeated steps.
  • 1:07 - 1:13
    This is the example that I had in the
    first, first lecture, Chapter 1.
  • 1:13 - 1:15
    And the basic idea, just to review,
  • 1:15 - 1:18
    is that you have this while keyword.
  • 1:18 - 1:21
    The while keyword sort of
    functions like an if in that
  • 1:21 - 1:24
    it implicitly has a decision that
    it's going to make.
  • 1:24 - 1:27
    And it's either going to do
    the code in the
  • 1:27 - 1:30
    indented block or not do it,
    or skip it basically.
  • 1:30 - 1:33
    Alright?
    So you either do it or you skip it.
  • 1:33 - 1:36
    The difference between the while
    and the if is that it's going
  • 1:36 - 1:41
    to do it many times, as long as this
    question that we have remains true.
  • 1:41 - 1:42
    Okay?
  • 1:42 - 1:48
    And so, in this case, n is 5, while n
    greater than 0 functions like an if.
  • 1:48 - 1:53
    So yes, it's going to run it.
    Prints out 5, subtracts 1, so it's 4.
  • 1:53 - 1:54
    Goes back up.
  • 1:54 - 1:58
    Goes back up and asks the question again,
    is n still greater than 0?
  • 1:58 - 2:01
    Well, since it's 4, yes,
    we'll continue on.
  • 2:01 - 2:06
    Out comes 4 then n gets subtracted.
    3, 2, 3, 2, and then
  • 2:06 - 2:10
    we come through, we print 1.
    Print 1.
  • 2:10 - 2:13
    We subtract n to 0. We go up,
    we go back up.
  • 2:13 - 2:17
    n is now not greater than 0, so we come
  • 2:17 - 2:21
    up and we execute outside the loop,
    we leave the loop.
  • 2:21 - 2:23
    And that really means in the
    Python code we
  • 2:23 - 2:26
    skip to whatever is lined with
    the while statement.
  • 2:26 - 2:29
    The same indent level as
    the while statement.
  • 2:29 - 2:31
    And so that's how it works, and
  • 2:31 - 2:36
    I just print n at the end here to remind
    ourselves that n ended up at 0, not at 1,
  • 2:36 - 2:39
    the last thing we printed out
    in the loop. The last thing we
  • 2:39 - 2:43
    printed out in the loop was the 1,
    but n ended up at 0 because
  • 2:43 - 2:46
    it was, this loop was going to run
    as long as n was greater than 0,
  • 2:46 - 2:50
    so n had to sort of be not greater
    than 0 to get out of the loop.
  • 2:50 - 2:50
    Okay?
  • 2:50 - 2:53
    So that's basically a
    review of what we've done.
  • 2:54 - 2:56
    Now, oh, wait, wait, wait, wait,
  • 2:56 - 2:59
    wait, something else.
    Iteration variables.
  • 3:01 - 3:04
    Okay, so the key to this is
    these loops can't run forever.
  • 3:04 - 3:06
    We don't want them to run forever.
  • 3:06 - 3:08
    We want them to run until,
    as long as we want them to run.
  • 3:08 - 3:12
    They may run a very long time,
    but not forever.
  • 3:12 - 3:13
    There's got to be a way to get
    out of them, otherwise we
  • 3:13 - 3:16
    call them infinite loops, which
    we'll talk about in the next slide.
  • 3:16 - 3:20
    And so the iteration variable
    is generally some variable
  • 3:20 - 3:21
    that is changing each
  • 3:21 - 3:26
    time through the loop, and we're changing
    it by subtracting 1 to it, from it.
  • 3:26 - 3:28
    And so this thing is going to keep
    running, and we
  • 3:28 - 3:31
    can pretty much see that Oh, this is
    going to exit, right?
  • 3:31 - 3:35
    Whatever n is, it could be a large number,
    but eventually it's going to get to 0.
  • 3:35 - 3:35
    Right?
  • 3:35 - 3:41
    So the iteration variable controls how
    many times the loop runs.
  • 3:41 - 3:44
    And it also allows us to do something
    different inside the loop.
  • 3:44 - 3:45
    And, of course, this is like a trivial
  • 3:45 - 3:47
    loop, where we're just printing the
    iteration variable.
  • 3:47 - 3:50
    But it just means that this
    loop is going to run
  • 3:50 - 3:54
    five times and it's going to do something
    potentially different each time.
  • 3:54 - 3:56
    If you just ran the loop that
    did the same thing over
  • 3:56 - 3:58
    and over and over again, with no data
    changing, that's kind of dull and pointless.
  • 3:58 - 4:01
    So just because you have an
    iteration variable
  • 4:01 - 4:06
    doesn't mean that you've properly
    constructed your loop.
  • 4:06 - 4:09
    It's a, it's a common problem or
    something we want to avoid,
  • 4:09 - 4:13
    is an infinite loop. And here is a, a
    carefully constructed loop.
  • 4:13 - 4:16
    We start n at 5 at the beginning.
  • 4:16 - 4:19
    We have a good question at the end,
    while n greater than 0.
  • 4:19 - 4:20
    It's going to run this
  • 4:20 - 4:22
    as long as n is greater than 0.
  • 4:23 - 4:26
    But the problem is, is we don't
    change in the little block.
  • 4:26 - 4:27
    We don't change the n.
  • 4:27 - 4:30
    Which means it's going to come back
    and n is going to be 5.
  • 4:30 - 4:32
    And it's going to run this and
    n is going to be 5.
  • 4:32 - 4:34
    And it's going to run this and
    n is going to be 5.
  • 4:34 - 4:37
    And so this is an infinite loop, which
    means this loop will never exit.
  • 4:37 - 4:39
    It will never get out,
  • 4:40 - 4:43
    it's just going to run forever in here,
    because n is not changing.
  • 4:43 - 4:45
    Neither of these statements change n.
  • 4:45 - 4:50
    So part of the iteration variable is there
    needs to be something that changes
  • 4:50 - 4:52
    so that the loop will ultimately
    make progress to
  • 4:52 - 4:54
    accomplish what it is and
    know when to stop.
  • 4:54 - 4:56
    So this is an infinite loop
    and, of course,
  • 4:56 - 5:01
    Lather, Rinse, Repeat is commonly put on
    shampoo and conditioner.
  • 5:01 - 5:04
    And so you know, you can,
    next time you are
  • 5:04 - 5:05
    in the shower, take a look at
    your shampoo and
  • 5:05 - 5:08
    conditioner and find the
    infinite loop that's,
  • 5:08 - 5:12
    that's on most bottles of
    shampoo and conditioner.
  • 5:14 - 5:15
    Now here is another loop,
  • 5:18 - 5:20
    just to emphasize that it's
    possible to structure
  • 5:20 - 5:23
    these loops in a way that they never run.
  • 5:23 - 5:25
    So this function is, is an if.
  • 5:25 - 5:28
    The while functions as an if.
  • 5:28 - 5:32
    And so when n is set to 0 and
    we ask the question,
  • 5:32 - 5:35
    it is literally going to make the
    decision based on n greater than 0.
  • 5:35 - 5:39
    Well it is not greater than 0, so it's
    going to go right by it, over
  • 5:39 - 5:43
    here it's going to come in here and go
    right to there and never run these
  • 5:43 - 5:47
    lines of code. And that's, we
    call this a zero trip loop.
  • 5:49 - 5:52
    And that's okay.
    I mean, this is a silly one, of course.
  • 5:54 - 5:58
    It just shows that the test, the
    question that's being asked is
  • 5:58 - 6:01
    above the lines that make up
    the body of the loop.
  • 6:01 - 6:03
    And if it's false, the loop never runs.
  • 6:03 - 6:07
    So it's possible that these loops
    have zero trips.
  • 6:07 - 6:08
    Okay?
  • 6:08 - 6:09
    So, that's a loop.
  • 6:09 - 6:14
    Now, there are more than one way
    to sort of control the flow
  • 6:14 - 6:16
    of a loop.
  • 6:16 - 6:18
    The basic flow of the loop is
    when it gets to the
  • 6:18 - 6:21
    bottom, it goes back up to the while,
    and does the check.
  • 6:21 - 6:22
    This is a different way of getting out of
  • 6:22 - 6:25
    a loop or controlling the execution
    of a loop.
  • 6:25 - 6:28
    There is a keyword or a part of
    the Python language called...
  • 6:30 - 6:31
    What color do I got?
  • 6:31 - 6:34
    No, green's over here.
    Called break.
  • 6:34 - 6:39
    If you look back at the reserved words,
    break was one of the reserved words.
  • 6:39 - 6:39
    Break says,
  • 6:39 - 6:43
    hey, if I'm in a loop, stop the loop.
    All right?
  • 6:43 - 6:44
    Get out of this loop.
  • 6:44 - 6:46
    I'm done with this loop.
  • 6:46 - 6:48
    And so here's this loop.
  • 6:48 - 6:49
    Now the interesting thing we've
    done is I've
  • 6:49 - 6:51
    just got done talking to you about
    infinite loops.
  • 6:51 - 6:55
    We have just constructed an
    "infinite loop", because the
  • 6:55 - 6:58
    question is right there, and
    the answer is yes.
  • 6:58 - 6:59
    True.
  • 6:59 - 6:59
    True.
  • 6:59 - 7:02
    And that's a way to construct
    an infinite loop.
  • 7:02 - 7:03
    We've done this because we have a
    different
  • 7:03 - 7:04
    way of getting out of the loop, so we've
  • 7:04 - 7:07
    constructed a loop that,
    just on the face of it,
  • 7:07 - 7:10
    just looking at that line, looks
    like an infinite loop.
  • 7:10 - 7:13
    So what this loop does is it
    reads a line of input,
  • 7:13 - 7:17
    checks to see if the string that
    we've entered is done.
  • 7:17 - 7:21
    And if it is, we're going to
    skip out with this break
  • 7:21 - 7:23
    and get out of the loop, otherwise
    we're going to print it.
  • 7:23 - 7:25
    So at a high level, what this loop is
  • 7:25 - 7:29
    going to do is prompt for
    strings of characters
  • 7:29 - 7:30
    until we enter done.
  • 7:30 - 7:31
    And that's exactly what it does.
  • 7:31 - 7:33
    It prompts, we say hello there,
    it prints that out.
  • 7:33 - 7:35
    We say, we say finished, it
    prints that out.
  • 7:35 - 7:37
    We say done, and it's done.
  • 7:37 - 7:40
    So it, when we say done, it comes out and
  • 7:40 - 7:43
    finishes the loop, and, and that's
    the end of the program.
  • 7:43 - 7:44
    Okay?
  • 7:44 - 7:46
    So, to look at this in some more detail.
  • 7:48 - 7:52
    The first time, it comes in,
    does the raw input.
  • 7:52 - 7:54
    Because True is true, so it's
    going to run it.
  • 7:54 - 7:58
    And then we enter hello there.
  • 7:58 - 8:00
    It checks to see if what we entered is
    equal to the string done.
  • 8:00 - 8:02
    It is not, so it skips and it
    does the print.
  • 8:04 - 8:07
    And we do this one more time,
    we type finished.
  • 8:07 - 8:10
    And then the line is not done,
    that variable
  • 8:10 - 8:12
    line does not have the value done in it.
  • 8:12 - 8:13
    So we print that. We come in
  • 8:13 - 8:17
    one more time, but this time
    this is true, so it
  • 8:17 - 8:21
    goes in and executes the break
    and then it escapes the loop.
  • 8:21 - 8:22
    And so you can think of...
  • 8:24 - 8:24
    Right?
  • 8:24 - 8:26
    Here is the body of this loop,
  • 8:26 - 8:30
    because that's where the indentation
    starts and ends.
  • 8:30 - 8:35
    The break says, break me out of the
    current loop that I am in
  • 8:35 - 8:38
    and get to that next line that has the
    same indent as the while.
  • 8:38 - 8:42
    So, whatever it is, break says, we are
    done with this loop.
  • 8:42 - 8:45
    When that statement executes, we are
    done with the loop.
  • 8:45 - 8:47
    We're finished with the loop.
  • 8:47 - 8:51
    It'll run until that executes
    because we got this set to be while True.
  • 8:53 - 8:57
    Okay, so there's a simpler, I mean this is
    sort of a simple way to draw this.
  • 8:57 - 9:01
    Break is sort of a jump to the statement
    immediately following the loop.
  • 9:03 - 9:04
    If you really want to picture this, I think
  • 9:04 - 9:07
    of this as a kind of like a Star Trek
    transporter.
  • 9:07 - 9:12
    Where you kind of come into break and
    then [SOUND] your molecules are sent
  • 9:12 - 9:16
    to the four corners of the universe, and
    you reassemble outside of the loop.
  • 9:16 - 9:19
    And so if we look at this in sort of
    my little road map
  • 9:19 - 9:21
    version of these things, right?
  • 9:21 - 9:24
    The while loop is going to run for
    a while, yada, yada.
  • 9:24 - 9:26
    There can actually be more
    than one break
  • 9:26 - 9:30
    as long as they only get this. But the
    moment that somehow, some if or
  • 9:30 - 9:36
    whatever hits the break, then it gets out
    completely and so it escapes the loop.
  • 9:36 - 9:41
    And so it sort of like you, you,
    you're zoom, zoom,
  • 9:41 - 9:44
    zoom, zoom, zoom, zoom, you
    come in here and then you are,
  • 9:44 - 9:48
    you are re-materialized outside the loop.
    That's what the break does, okay?
  • 9:48 - 9:53
    So, break is one way to control the
    execution of loops.
  • 9:55 - 9:57
    Now, another way to control
    the execution of
  • 9:57 - 10:00
    loops, that doesn't actually exit the
    loop, is called continue.
  • 10:01 - 10:07
    Continue basically says, hey, I'm done
    with this iteration of the loop.
  • 10:07 - 10:10
    Now each time through the loop, we
    call that an iteration.
  • 10:10 - 10:13
    Continue says, I don't want to
    stop the loop, but I
  • 10:13 - 10:18
    want to stop this iteration and
    advance to the next iteration.
  • 10:18 - 10:20
    And so what we have here is
    we have the same basic loop,
  • 10:20 - 10:23
    a while True which kind of
    makes us an infinite loop.
  • 10:25 - 10:27
    We're going to read a line prompting
    with a less than sign.
  • 10:29 - 10:31
    And if it's done, we are going to
    break, that code is
  • 10:31 - 10:33
    down here, and we are going to
    print it if we fall through.
  • 10:33 - 10:35
    So normally we'll be reading and
  • 10:35 - 10:38
    printing, and if the line is done,
    we are going to break out.
  • 10:38 - 10:41
    That's we just got done doing.
    But the new part is right here.
  • 10:42 - 10:44
    And this is, we'll learn this
    in the next chapter.
  • 10:44 - 10:47
    If line sub 0, if the first character of
  • 10:47 - 10:51
    the line, is a pound sign, we're
    going to continue.
  • 10:51 - 10:54
    And what continue says is it doesn't
    actually get us out of the loop.
  • 10:54 - 10:56
    It jumps back up to the top of the loop.
  • 10:56 - 11:00
    Which means that it ignores,
    for that iteration,
  • 11:00 - 11:02
    the rest of the loop. Right?
  • 11:02 - 11:04
    So if execution comes in here.
  • 11:04 - 11:06
    I'm going to clear that.
  • 11:07 - 11:13
    If execution comes in here and hits this
    line, it goes back up to the while.
  • 11:13 - 11:14
    Okay?
  • 11:14 - 11:17
    Which means it, whatever this is, it's not
    coming out of this if.
  • 11:17 - 11:19
    It's going back up to the while.
  • 11:19 - 11:20
    Okay?
  • 11:20 - 11:23
    So continue ends the current
    iteration and jumps to
  • 11:23 - 11:25
    the top of the loop, and starts
    the next iteration.
  • 11:26 - 11:28
    And so if we look at how the code runs,
  • 11:28 - 11:32
    hello there prints, pound sign with the
    first character
  • 11:32 - 11:35
    doesn't print, so there is
    no printout right here.
  • 11:35 - 11:39
    Print this is not done. And we enter done,
    and then the loop ends.
  • 11:39 - 11:41
    Now another way to sort of draw this is
  • 11:41 - 11:44
    the continued jumps to the top
    of the loop.
  • 11:44 - 11:50
    It, it does run the question, right?
    It does check the question.
  • 11:50 - 11:51
    So here is another way to, to draw
  • 11:51 - 11:56
    that picture. And so here again we have a
    loop and it's happily running.
  • 11:56 - 11:58
    And there can be breaks in there
    and there could
  • 11:58 - 12:00
    be continues in there, and as
    long as we don't
  • 12:00 - 12:02
    hit a break or continue, the
    loop just sort of
  • 12:02 - 12:05
    runs and goes up to the top.
    And at some point, some if,
  • 12:05 - 12:09
    we hit the continue. And
    like a transporter, instead of
  • 12:09 - 12:11
    going out of the loop, we go to
    the top of the loop.
  • 12:11 - 12:16
    But it's important that we go and
    we check the question, right?
  • 12:16 - 12:17
    So the continue is
  • 12:17 - 12:20
    not likely to exit the loop
    unless the question has become false.
  • 12:20 - 12:23
    So the continue is likely to
    come up here,
  • 12:23 - 12:26
    run some more, then hit the continue.
    It comes up here.
  • 12:26 - 12:27
    Oops!
    Oops, it did that backwards.
  • 12:27 - 12:28
    Run some more.
  • 12:28 - 12:29
    Clear this out.
  • 12:29 - 12:32
    So, the continue could run
    many times, right?
  • 12:32 - 12:33
    So we have the loop.
  • 12:33 - 12:37
    Loop runs a bunch of times, then
    finally we hit the continue.
  • 12:37 - 12:38
    Continue goes up to the top.
  • 12:38 - 12:41
    If it's still true, we'll run the
    loop some more.
  • 12:41 - 12:42
    Then you might hit the continue.
  • 12:42 - 12:44
    Then you might go up to the top, come
  • 12:44 - 12:47
    down, round and round and round and round,
    hit the continue again.
  • 12:47 - 12:49
    Go up to the top, yada, yada.
  • 12:49 - 12:53
    Now in this, in this particular loop,
    this break eventually
  • 12:53 - 12:55
    is down here and that's how we
    get out, okay?
  • 12:55 - 12:58
    So the continue goes back up
    to the top of the loop.
  • 13:00 - 13:02
    So these loops that we construct with the
  • 13:02 - 13:05
    while keyword are what we call
    indefinite loops.
  • 13:05 - 13:07
    I mean, looking at the ones that we've
  • 13:07 - 13:10
    written, which are two lines or six lines,
  • 13:10 - 13:13
    we can kind of inspect them and
    understand when they are going
  • 13:13 - 13:17
    to stop, and we are going to know that
    they're possible to stop them.
  • 13:17 - 13:20
    A loop that won't stop is an infinite loop.
  • 13:22 - 13:25
    Sometimes these loops can be rather [COUGH]
    complex and you may not actually be able to
  • 13:25 - 13:30
    look at them, because there are many lines
    and, and so we don't know.
  • 13:30 - 13:32
    And so, so if you, you have to be
    real careful when you
  • 13:32 - 13:37
    construct these to make sure that they
    stop as, as things get more complicated.
  • 13:37 - 13:42
    Now the cousin to indefinite loops are
    definite loops.
  • 13:42 - 13:47
    And definite loops is something where we
    have a list of things or a set
  • 13:47 - 13:50
    of things that are kind of a known set of
    things, a finite set of things.
  • 13:51 - 13:53
    And we are going to write a loop that's
    going to go through that set of things
  • 13:53 - 13:57
    and do something to each thing
    in that set of things.
  • 13:57 - 14:00
    And the keyword that we use
    for this is the for.
  • 14:00 - 14:05
    So we use the Python for keyword that
    says we are going to write a loop, but
  • 14:05 - 14:08
    instead of it just running until some
    condition
  • 14:08 - 14:09
    becomes true or false or we hit a break,
  • 14:10 - 14:14
    we're actually going to know how many
    times this is going to run.
  • 14:14 - 14:17
    Now you can actually use break and
    continue in for loops.
  • 14:17 - 14:20
    We call these definite loops because
    the, how long
  • 14:20 - 14:23
    they're going to run is kind of
    well known, basically.
  • 14:23 - 14:27
    So here's a simple definite loop, and it's
    kind of like that while loop
  • 14:27 - 14:28
    that we just got done looking at
  • 14:28 - 14:31
    where it's counting down and then
    saying blastoff.
  • 14:31 - 14:35
    And so the way we construct this loop is
    we have the for keyword,
  • 14:35 - 14:38
    it's part of Python language,
  • 14:38 - 14:41
    the in keyword, and then we
    have an iteration variable.
  • 14:41 - 14:44
    I've chosen i as my iteration variable.
  • 14:44 - 14:47
    And basically what we're saying is,
    dear Python,
  • 14:49 - 14:53
    run this indented block, and there's only
    one line in the indented block.
  • 14:53 - 14:57
    Run it once for each of the values
    in this little list.
  • 14:57 - 15:01
    This is a Python list, square brackets
    make Python lists.
  • 15:01 - 15:02
    Comma-separated values.
  • 15:02 - 15:05
    So it says, I would like i to be 5,
    then run this code.
  • 15:05 - 15:07
    Then I would like i to be 4,
    then run this code.
  • 15:07 - 15:09
    Then I would like i to be 3,
    then run this code.
  • 15:09 - 15:14
    i should be 2, then run this code.
    And i should be 1, then run this code.
  • 15:14 - 15:16
    And so this is pretty clear,
    and I like this word in.
  • 15:18 - 15:23
    It says you know, doop, doop, doop,
    doop, doop, and then run this each time.
  • 15:23 - 15:28
    And so out of that comes 5, 4, 3, 2, 1,
    and then the loop is done.
  • 15:28 - 15:30
    Python is doing all the tricky bits
  • 15:30 - 15:33
    here, Python's figuring all these
    things out for us
  • 15:35 - 15:39
    and handling all this, and then we're done.
    And so it's, it's, if you look at it,
  • 15:39 - 15:41
    we have an iteration variable, but we
    didn't have to increment it,
  • 15:41 - 15:45
    we didn't have to do anything. Python took
    care of a lot of things for us.
  • 15:45 - 15:48
    And so when we're looping through
    a known list of things,
  • 15:48 - 15:50
    or later when we read a file,
  • 15:50 - 15:51
    we're going to loop through
    the lines in the file.
  • 15:51 - 15:54
    And so the for loop is a really nice
    powerful.
  • 15:54 - 15:58
    And it's syntactically cleaner, it's
    really quite nice.
  • 15:58 - 16:00
    Now it's important to realize that you
  • 16:00 - 16:04
    don't have to just loop through numbers.
    I did that one with the set of
  • 16:04 - 16:05
    descending numbers, so that
    it was equivalent to the
  • 16:05 - 16:08
    while loop that I started at the
    beginning.
  • 16:08 - 16:12
    But this is a loop where what it's
    going to loop through is a list.
  • 16:12 - 16:15
    Closed square brackets are a list in Python.
  • 16:15 - 16:19
    This is a list of three strings, Joseph,
    Glenn, and Sally.
  • 16:19 - 16:23
    They're string constants, and then commas
    are how we make lists.
  • 16:24 - 16:26
    And so friends
  • 16:26 - 16:27
    is a mnemonic variable.
  • 16:27 - 16:30
    Python doesn't know anything about friends
    in particular,
  • 16:30 - 16:32
    but I've chosen this variable name
    to be friends.
  • 16:32 - 16:35
    And it's a list of three people, Joseph,
    Glenn, and Sally.
  • 16:35 - 16:38
    And so I have an iteration variable
    called friend.
  • 16:38 - 16:40
    And I'm going to loop through the
    set of friends.
  • 16:40 - 16:42
    Now Python doesn't know anything about
    singular.
  • 16:42 - 16:44
    Python doesn't know anything about plural.
  • 16:44 - 16:47
    I'm just choosing these variable names
    because it makes a lot of sense.
  • 16:48 - 16:51
    This is a set of friends.
    Because it has three of them in it.
  • 16:51 - 16:54
    And this is a single friend.
  • 16:54 - 16:58
    What it's really going to do is friend is
    going to take on the successive
  • 16:58 - 17:01
    of values Joseph, Glenn, and Sally,
    and this little block of code
  • 17:01 - 17:05
    is going to once for each of
    those three items in the set, and
  • 17:05 - 17:10
    the variable friend is going to take on
    the successive values of that set.
  • 17:10 - 17:13
    So out of this comes
    three lines of printout.
  • 17:13 - 17:16
    Happy New Year Joseph, Happy New Year
    Glenn, Happy New Year Sally.
  • 17:16 - 17:20
    Of course this is the i bit
    right down here, but we just
  • 17:20 - 17:23
    made it so, hey Python, look, how ever
    many friends they are, run this
  • 17:23 - 17:27
    code one time for each one, change this
    variable friend to be each
  • 17:27 - 17:30
    of the successive ones in order,
    and then we print that we're done.
  • 17:30 - 17:31
    Okay?
  • 17:31 - 17:33
    So the for loop.
  • 17:33 - 17:35
    Sort of if we go and try to make a
    picture of the for loop.
  • 17:35 - 17:39
    The for loop is kind of a powerful thing.
    It's does, it does two things.
  • 17:39 - 17:41
    It decides if we're done or not.
  • 17:42 - 17:44
    Do we keep going in the loop?
  • 17:44 - 17:46
    Or, well I mean as long as we keep
  • 17:46 - 17:49
    going, we're going to advance
    the i value, the iteration variable.
  • 17:49 - 17:53
    It takes care of the responsibility of
    changing the iteration variable.
  • 17:53 - 17:57
    We do not have to add lines of code in
    that change the iteration variable, okay?
  • 17:57 - 18:01
    And so if we take a look, you know,
    we come in.
  • 18:01 - 18:02
    Are we done? We're not done.
  • 18:02 - 18:04
    Set i to the right thing, then print it.
  • 18:04 - 18:07
    Out comes 5. Advance i,
  • 18:07 - 18:09
    advance i, print it. Advance it.
  • 18:09 - 18:10
    Print it. Advance it. Print it.
  • 18:10 - 18:12
    Oh, now we're done.
  • 18:12 - 18:13
    Right?
  • 18:13 - 18:17
    i was not the thing that decided
    when we were done.
  • 18:17 - 18:20
    The for loop just keeps track
    internally as i moves
  • 18:20 - 18:23
    through these things and it
    goes like oh, I'm all done.
  • 18:23 - 18:25
    I'll take care of that, you finished.
  • 18:25 - 18:29
    So it doesn't, there's no if in here,
    so it's not like like if i equals 1, stop.
  • 18:29 - 18:32
    No, no, no, it just says you told me
    to do five things.
  • 18:32 - 18:32
    I'm going to do five things
  • 18:32 - 18:34
    and then we're going to stop.
  • 18:34 - 18:40
    And so again the for loop, the for loop
    here has got sort of two functions.
  • 18:40 - 18:44
    It decides how long the loop is going to
    run, and changes the
  • 18:44 - 18:48
    iteration variable based on what you've
    told it to, in this in clause.
  • 18:49 - 18:49
    Okay?
  • 18:50 - 18:54
    So I think in is a real elegant
    construct.
  • 18:54 - 18:58
    It's just a keyword, but it's
    sort of, if you think about math,
  • 18:58 - 19:00
    if you're familiar with sets,
  • 19:00 - 19:02
    it's like something inside of
    a set of something.
  • 19:02 - 19:04
    I think it's a real pretty way
    to think about it.
  • 19:05 - 19:07
    And you can kind of think of it
    a little more abstractly.
  • 19:07 - 19:11
    That you say, well here's a little
    indented block of code.
  • 19:11 - 19:11
    Right?
  • 19:11 - 19:15
    And I want it to run some number
    of times for each
  • 19:15 - 19:19
    of the i values in the set 5, 4, 3, 2, 1.
  • 19:19 - 19:20
    That's how I kind of think of it.
  • 19:20 - 19:23
    So I think that this is a
    real pretty syntax.
  • 19:23 - 19:25
    Different languages have different
    looping syntax.
  • 19:25 - 19:29
    I think this is really a very
    expressive, very pretty one.
  • 19:33 - 19:34
    Yeah.
  • 19:34 - 19:37
    So another way to, so, so, so one way to
  • 19:37 - 19:40
    think about this picture is that, you
    know, the for loop
  • 19:40 - 19:43
    causes sort of repeated execution
    in that we're
  • 19:43 - 19:45
    driving in the circle and then we
    stop, right?
  • 19:45 - 19:48
    The other way to think about is to, to
  • 19:48 - 19:51
    not, to think about a little more
    abstractly, right?
  • 19:51 - 19:53
    To say, hmm, you know, at the
    end of the day, all I'm
  • 19:53 - 19:58
    really telling Python is, I want to
    execute this block of code five times,
  • 19:58 - 20:02
    then I want the variable i to
    change from, to these three values.
  • 20:02 - 20:04
    So in a way, you could think of
    this as expanded
  • 20:04 - 20:07
    is the for loop sets it to 5,
    then runs your code.
  • 20:07 - 20:09
    The for loop then sets it to 4,
    runs your code.
  • 20:09 - 20:12
    The for loop sets it to 3, runs your code.
  • 20:12 - 20:16
    For loop sets it to 2, runs your code.
    Sets it to 1, runs your code.
  • 20:16 - 20:19
    These two ways of looking at it
    are the same
  • 20:20 - 20:23
    from your perspective, because
    you're just asking Python
  • 20:23 - 20:25
    to do something.
  • 20:25 - 20:28
    Whether it does it this way, or
    whether it does it this way,
  • 20:28 - 20:32
    you hardly can tell the difference.
    It's probably going to do it this way.
  • 20:32 - 20:37
    But logically it's not that different.
    It's not different from doing it this way.
  • 20:37 - 20:41
    You're saying, run this block of code,
    change i in the following way.
  • 20:45 - 20:49
    Cool.
    It's like, we don't have to worry.
  • 20:49 - 20:52
    I mean, we can use, mentally,
    either model of what's
  • 20:52 - 20:55
    going on inside, because it doesn't
    matter, because they're the same.
  • 20:57 - 21:00
    Okay, so these definite loops
    are really cool.
  • 21:00 - 21:03
    Starting in a couple of chapters we'll
    mostly use definite
  • 21:03 - 21:07
    loops to go through lists or
    dictionaries or tuples or files.
  • 21:08 - 21:10
    And so that's a finite set of things.
  • 21:10 - 21:12
    It can be a large set of things, but
    it's a finite set of things.
  • 21:14 - 21:17
    Okay. So now I want to talk about
    loop idioms.
  • 21:19 - 21:24
    Loop idioms are how we construct loops.
  • 21:24 - 21:28
    And we're going to, the loops kind of
    have some kind of a goal in mind.
  • 21:28 - 21:30
    Finding the largest, we played with that.
  • 21:30 - 21:33
    Finding the smallest, counting the
    number of things,
  • 21:33 - 21:36
    looking for lines that start with pound
    signs, something like that.
  • 21:36 - 21:40
    They, they have a kind of a high-level
    view of what they're supposed to do,
  • 21:40 - 21:44
    and then we have to kind of build a
    loop to accomplish that.
  • 21:44 - 21:48
    And and this goes back to how we have to
    think like a computer, right?
  • 21:48 - 21:50
    We have to say, hey computer,
  • 21:50 - 21:52
    do this over and over and over again,
    and then I'll
  • 21:52 - 21:54
    get what I want once you've done that
    over and over again.
  • 21:54 - 21:56
    You have to do something a million times.
  • 21:56 - 21:57
    I'm not going to sit here and wait.
  • 21:57 - 21:59
    At the end, I'll get what I want.
  • 21:59 - 22:01
    So I call these kind of "smart loops",
  • 22:01 - 22:03
    or how to kind of build
    intelligence into loops.
  • 22:06 - 22:09
    So, for example, we want the largest
    number.
  • 22:09 - 22:09
    Right?
  • 22:09 - 22:12
    But we have to construct a loop that will
  • 22:12 - 22:15
    get us the largest number thinking
    like a computer.
  • 22:15 - 22:17
    Okay? Thinking computationally.
  • 22:17 - 22:19
    Thinking like a computer.
  • 22:19 - 22:22
    So the idea is that we have some kind
  • 22:22 - 22:24
    of a loop and we're going to
    go through this list,
  • 22:24 - 22:28
    some list of things, and this is
    going to run a bunch of times.
  • 22:28 - 22:30
    And, but the way we're going to do it is
  • 22:30 - 22:32
    we're going to set something up
    before the loop starts.
  • 22:32 - 22:36
    We're going to do something to each of the
    things that's being looked at,
  • 22:36 - 22:40
    and at the end, we're going to get the
    result we're looking for.
  • 22:40 - 22:41
    Okay?
  • 22:41 - 22:43
    And so in the middle it's kind of like
    working.
  • 22:43 - 22:46
    It's in the middle working,
    da, da, da, da, da.
  • 22:46 - 22:47
    And then here is the payoff.
  • 22:47 - 22:52
    The payoff is at the end when we get the
    information that we're interested in.
  • 22:52 - 22:56
    So I will sort of use in the
    next few examples
  • 22:56 - 22:58
    this simple loop,
  • 22:58 - 23:00
    and right now it doesn't do much.
  • 23:00 - 23:02
    It does a print Before and it
    has this variable
  • 23:02 - 23:06
    thing that goes through the successive
    values of these numbers.
  • 23:06 - 23:09
    And it prints it out, right?
    So that middle part says
  • 23:09 - 23:12
    run this six times, once for
    each of those values.
  • 23:12 - 23:13
    And then After. Okay?
  • 23:13 - 23:17
    And so we will add some intelligence
    at the beginning, we'll add
  • 23:17 - 23:20
    some intelligence in the middle, and we'll
    add some intelligence at the end.
  • 23:20 - 23:23
    And then the whole thing will
    accomplish what we want.
  • 23:23 - 23:26
    Right now this is kind of not that
    intelligent.
  • 23:26 - 23:31
    So now what I want to do, is I want to
    review the thing we did, and I want you
  • 23:31 - 23:33
    to remember what the largest number is,
    and I'm going to
  • 23:33 - 23:36
    show you a sequence of numbers in order.
  • 23:36 - 23:37
    Ready?
  • 23:37 - 23:39
    One, I'll do it kind of quickly, because
    you've seen this before.
  • 23:39 - 23:42
    So, I'm only showing you one number
    at a time.
  • 23:42 - 23:44
    So you want to tell me what the
    largest number is.
  • 23:44 - 23:48
    So here we go.
    The first number is 9.
  • 23:48 - 23:52
    The second number is 41.
  • 23:52 - 23:57
    The third number is 12.
    The fourth number is 3.
  • 23:57 - 24:04
    The fifth number is 74.
    And the sixth number is 15.
  • 24:04 - 24:06
    So what was the largest number?
  • 24:09 - 24:13
    Did you have to go back?
    Or did you remember how to do it?
  • 24:13 - 24:18
    Okay, well, I will give you a clue.
    It was 74.
  • 24:18 - 24:20
    Okay? That's because I know.
  • 24:20 - 24:24
    Okay, now if you did that and you had to
    do that for 20 or 30 numbers,
  • 24:24 - 24:27
    you'd have to create a mental algorithm in
  • 24:27 - 24:31
    your head to approach it and stay
    concentrated, focused.
  • 24:31 - 24:34
    So, you would've created a
    variable in your head
  • 24:34 - 24:35
    called largest_so_far.
  • 24:37 - 24:40
    I would show you the first number, which
    would be 9, and you would go hmm.
  • 24:40 - 24:43
    Well, 9 is larger than 1, negative 1.
  • 24:43 - 24:46
    So I will keep that. That's the new
    largest I've seen so far.
  • 24:46 - 24:50
    That's pretty awesome, because it's way
    better than negative 1.
  • 24:50 - 24:55
    41? I thought 9 was good.
    But 41, that is a lot better.
  • 24:55 - 24:56
    So I'm going to keep that one.
  • 24:56 - 24:59
    That's the, that's the best.
    It's the largest we've seen so far.
  • 24:59 - 25:00
    We've only seen two numbers.
  • 25:01 - 25:03
    But the best we've so far is 41.
  • 25:03 - 25:09
    So 12 is not larger. Who, who
    cares about that?
  • 25:09 - 25:12
    It's not as big as 41 so we'll just go
    right on to the next, on to the next.
  • 25:12 - 25:16
    3, that's lame when we are
    looking for large numbers.
  • 25:16 - 25:20
    So we skip, oh, 74. 74, that's a
    rockingly large number.
  • 25:20 - 25:21
    So we're going to, that's a lot.
  • 25:21 - 25:23
    That's actually the largest we've
    seen so far
  • 25:23 - 25:26
    because it's bigger than 41,
    and 41 was the former champion
  • 25:26 - 25:31
    largest we've seen so far.
    And there's 74, so we keep that one.
  • 25:31 - 25:34
    I don't know how many letters, of these
    things you're going to see, right?
  • 25:34 - 25:38
    We could see lots of them.
    But the next one we see 15.
  • 25:38 - 25:40
    Well, that's no good.
  • 25:40 - 25:43
    We've got 74 already.
    74's like totally awesome.
  • 25:43 - 25:44
    Right?
  • 25:44 - 25:45
    So now, oh, we're done.
  • 25:45 - 25:50
    So, hey, we're done and so 74
    is the champion.
  • 25:51 - 25:52
    That is the largest.
  • 25:52 - 25:57
    It's not even the largest so far
    any more, it's actually the largest.
  • 25:57 - 25:59
    It's the largest.
  • 25:59 - 26:02
    So again, we had this thing
    at the top, we had this loop in
  • 26:02 - 26:04
    the middle, and at the bottom is
    where you kind of get the payoff.
  • 26:04 - 26:05
    And the payoff is
  • 26:05 - 26:07
    not in the middle.
  • 26:07 - 26:09
    while we're largest so far, largest so
    far, largest so far,
  • 26:09 - 26:11
    but at the end it turned out
  • 26:11 - 26:12
    once you've looked at all the variables,
  • 26:12 - 26:16
    all the values, the largest so far
    is indeed the largest.
  • 26:16 - 26:17
    Okay.
  • 26:17 - 26:19
    So here's the algorithm for this.
  • 26:19 - 26:21
    I have some variables, and remember
  • 26:21 - 26:25
    that underscores are valid characters in
    variables.
  • 26:25 - 26:29
    Now [COUGH] I'm being a little
    over-explicit in this.
  • 26:29 - 26:32
    So I have a variable called
    largest_so_far.
  • 26:32 - 26:37
    Then what I do is I set it to 1,
    negative 1.
  • 26:37 - 26:42
    Then I print Before so we can see that
    largest_so_far is negative 1.
  • 26:42 - 26:46
    Then we have a for loop and my variable
    iteration variable is the_num.
  • 26:46 - 26:51
    So that's going to take on the successive
    values: 9, 41, 12, 3, 74, 15,
  • 26:51 - 26:54
    and run this indented loop of code, okay?
  • 26:54 - 26:59
    And so the_num will be 9,
    first time through.
  • 26:59 - 27:02
    If the_num, 9, is greater than
  • 27:02 - 27:07
    largest_so_far, then grab the_num
    and assign
  • 27:07 - 27:13
    it into largest_so_far.
    Then print at the end of each loop,
  • 27:13 - 27:19
    largest_so_far and the_num.
    So, so, in effect, the_num is 9.
  • 27:19 - 27:23
    We compare it to negative 1, and
    negative 1, 9 is higher.
  • 27:23 - 27:25
    So we make largest_so_far be 9.
  • 27:27 - 27:33
    Next time through the loop, next time
    through the loop, num is 41.
  • 27:33 - 27:38
    So we compare largest_so_far with 41, and
    we like it, so we store it.
  • 27:38 - 27:42
    So we like it, we run it, and we print
    our 41 is the largest we've seen so far.
  • 27:43 - 27:45
    And we run again.
  • 27:45 - 27:50
    We come in, the_num now points to 12.
    the_num, 12, is
  • 27:50 - 27:55
    not greater than 41, and so we skip.
    So, the largest
  • 27:55 - 28:03
    so far stays 41, and we see 12.
    Similarly, the_num advances to 3.
  • 28:03 - 28:05
    We skip.
  • 28:05 - 28:08
    So we saw 3, but the largest
    so far is still 41.
  • 28:08 - 28:14
    Continuing, the_num is now 74.
    It runs, 74 is
  • 28:14 - 28:20
    greater than 41, and so we run
    this code. And so we say 74
  • 28:20 - 28:25
    is stuck in largest_so_far,
    and indeed, then we print
  • 28:25 - 28:27
    it out, and largest so far is now 74.
  • 28:27 - 28:29
    We continue on.
  • 28:29 - 28:31
    We go up one more time.
  • 28:31 - 28:36
    The_num points to 15, but 15
    is not larger than 74.
  • 28:36 - 28:37
    And so we skip.
  • 28:37 - 28:41
    We print out 15 and 74, and then
    we come out and
  • 28:41 - 28:45
    at the end, at the end, we get
    the largest so far.
  • 28:45 - 28:46
    It, the name no matter, no longer,
  • 28:46 - 28:48
    I mean it's kind of the largest
  • 28:48 - 28:52
    so far at the end is the largest,
    but the variable name.
  • 28:52 - 28:53
    Okay? Got it?
  • 28:56 - 28:59
    That's one idiom.
    So let's just switch to another idiom.
  • 29:01 - 29:03
    Now counting, how many things are we
  • 29:03 - 29:04
    going to, how many times is loop
    going to execute?
  • 29:04 - 29:06
    How things are we going to find
    in the loop?
  • 29:06 - 29:08
    It's all kind of the same notion.
  • 29:08 - 29:12
    And the pattern is really simple.
    We start some variable zork.
  • 29:12 - 29:13
    A better name for this would be count.
  • 29:13 - 29:16
    But I want to call it zork.
  • 29:16 - 29:20
    And then we have a loop, and then in the
    loop we just add 1 to zork,
  • 29:20 - 29:22
    and at the end, zork.
  • 29:22 - 29:27
    That should be light blue right there.
    zork should be the total count.
  • 29:27 - 29:30
    Now of course we can look at it and say
    it's going to be 6, but
  • 29:30 - 29:32
    assume this loop is looping through a
    million
  • 29:32 - 29:33
    lines in the file or something like that.
  • 29:33 - 29:39
    So it's [SOUND], so it's cheating to
    kind of look at it and say, ooh, it's 6,
  • 29:39 - 29:42
    because we want to actually compute it.
    So it's really simple.
  • 29:42 - 29:46
    You know, zork starts at 0.
    It's going to run zork is 1 now, and
  • 29:46 - 29:52
    2, 3, 4, 5, 6, and then we've run out of
    stuff and then we print out 6.
  • 29:52 - 29:53
    Okay?
  • 29:53 - 29:54
    So that's kind of the idiom, right?
  • 29:54 - 29:59
    Before, during, and after, right?
    We do something before, we do
  • 29:59 - 30:05
    something during, and, and in a sense this
    zork here is the number we've seen so far.
  • 30:05 - 30:07
    And at the end it becomes kind of the
    total number.
  • 30:10 - 30:12
    Summing in a loop, very similar.
  • 30:12 - 30:16
    Again, you have to think of this is, there
    is a whole bunch of variables here.
  • 30:16 - 30:19
    We start a variable at 0.
  • 30:19 - 30:22
    Each time through the loop, we add
    whatever it is that we're seeing.
  • 30:22 - 30:28
    Instead of adding 1 we're
    adding 9, 41, 12, 3, 74, 15.
  • 30:28 - 30:34
    And zork would be best thought of as
    running total.
  • 30:34 - 30:36
    So, zork is the running total.
  • 30:36 - 30:39
    And so if we look at the numbers 9,
  • 30:39 - 30:44
    running total's 9, running total's 50,
    running total's 62, 65, 139, 154.
  • 30:44 - 30:48
    And then we skip out and at the end, the
    running total becomes the total.
  • 30:49 - 30:50
    Okay?
  • 30:50 - 30:53
    So, that's another of these patterns
    that, sort of,
  • 30:53 - 30:55
    we do something at the beginning, we
  • 30:55 - 30:58
    do something in the middle, and we have
  • 30:58 - 31:01
    something very nice for ourselves
    at the end.
  • 31:05 - 31:07
    Finding the average of a sequence of
    values
  • 31:07 - 31:11
    is the combination of the
    two previous patterns.
  • 31:11 - 31:14
    This time I am going to use
    more mnemonic variables.
  • 31:14 - 31:16
    A variable called count.
  • 31:16 - 31:18
    Everyone calls this count.
  • 31:18 - 31:21
    Sum, now total would maybe be
    a better word for this.
  • 31:21 - 31:25
    The running total. And then, so the count
    and the sum start out at 0, and
  • 31:25 - 31:30
    then each time through the loop, count equals
    count plus 1, so we're adding 1 to count.
  • 31:30 - 31:34
    Sum equal sum plus value, so we're
    adding 1 to to sum.
  • 31:34 - 31:35
    I mean adding the value.
  • 31:35 - 31:39
    Value of course being
    9, 41, 12, 3, 74, 15.
  • 31:39 - 31:42
    And then at the very end we can
    print out the number.
  • 31:42 - 31:47
    We have six things with a total of 154,
    and then we calculate the average.
  • 31:47 - 31:52
    Of course these are integer numbers, and
    so this is a truncating division.
  • 31:52 - 31:56
    So 154 over 6 equals 25 and
  • 31:56 - 31:59
    not 25 point something.
  • 31:59 - 32:02
    In Python 3000, Python 3, it'd be better.
  • 32:02 - 32:08
    But so the average, the integer average is
    of the numbers we just looked at, is 25.
  • 32:08 - 32:12
    So sometimes we're searching, like
    for a needle in a haystack.
  • 32:12 - 32:15
    Looking for something and again
    you have to think of like
  • 32:15 - 32:19
    you're handed some amount of data and you
    gotta hunt for something.
  • 32:19 - 32:21
    And there might be a million things and
    you might only want five of them.
  • 32:21 - 32:22
    And you can either look by hand
    or you can write
  • 32:22 - 32:25
    a loop that's got an if statement that
    says found it.
  • 32:25 - 32:28
    Maybe I found it at line seven
    or found it wherever.
  • 32:28 - 32:32
    So this is filtering or finding or
    searching, looking
  • 32:32 - 32:34
    for a needle in a haystack, in a loop.
  • 32:35 - 32:38
    And so the, the idea basically is
    that we have this loop,
  • 32:38 - 32:43
    it's going to go through all the
    values, 9, 41, 12, 3, 74.
  • 32:43 - 32:46
    But we put in the loop, we embed
    an if statement.
  • 32:46 - 32:49
    If the value we're looking at is greater
    than 20, print I found it.
  • 32:50 - 32:58
    So when value is 9, this is going to do
    nothing and just go and make value be 41.
  • 32:58 - 33:02
    And then value 41, oop, yep, there we go,
    print Large number, so off this comes.
  • 33:04 - 33:08
    Then value becomes 12, nothing happens,
    value becomes 3, nothing happens,
  • 33:08 - 33:12
    value becomes 74, oops, this
    time it's going to happen.
  • 33:12 - 33:14
    So out comes Large number 74.
  • 33:14 - 33:19
    Then value becomes 15, nothing happens,
    and then value is all done,
  • 33:19 - 33:21
    and so it comes and finishes.
  • 33:21 - 33:26
    So this is the searching or
    filtering or catching or, or whatever.
  • 33:26 - 33:28
    Okay?
  • 33:29 - 33:32
    We can also sort of, if we don't just want
  • 33:32 - 33:34
    to print everything out, we want to
    say is something in there.
  • 33:34 - 33:36
    Go look through this million things and
  • 33:36 - 33:39
    tell me if blah exists.
  • 33:39 - 33:42
    And in this we're going to introduce the
    notion of Boolean variable.
  • 33:42 - 33:44
    A Boolean is a true-false.
  • 33:44 - 33:47
    It only has two values and we've
    already used it in the while True.
  • 33:47 - 33:57
    So that capital True, that is a constant,
    just like 7 or 42 or 99, or "Sam".
  • 33:57 - 33:58
    And so we're going to make this
    variable called found.
  • 33:58 - 34:03
    Now found is a mnemonic value, variable.
    It's just a name I picked.
  • 34:03 - 34:04
    So found equals False.
  • 34:04 - 34:06
    This is going to be false,
    until we find what
  • 34:06 - 34:09
    we're looking for and then it's
    going to switch to true.
  • 34:09 - 34:11
    So it starts out and it's false.
  • 34:11 - 34:14
    Then we're going to run this
    bit of code three times.
  • 34:16 - 34:19
    And if the value that we are looking at
    is 3, then we're going
  • 34:19 - 34:23
    to set found to be true, and we'll print
    found value each time through.
  • 34:23 - 34:28
    So value's going to take on 9, 41, 12, 3,
    74, so we get a line
  • 34:28 - 34:31
    of output for each one. And the
    first time through
  • 34:31 - 34:34
    it's not yet found, because we're
    looking at a 9.
  • 34:34 - 34:35
    Second time, it's not yet found.
  • 34:35 - 34:39
    We looked at 41, still false. So it could
    stay false for long time.
  • 34:40 - 34:41
    Oh, we found a true.
  • 34:41 - 34:43
    And then that means that this code is
    going to run once.
  • 34:43 - 34:46
    And so you can kind of think of this
    found variable as sticky.
  • 34:46 - 34:49
    It's going to stay false, and then
    the rest of the loop
  • 34:49 - 34:53
    it's going to stay true, and at
    the end it is true.
  • 34:53 - 34:54
    Now the way we usually do these
    kinds of things
  • 34:54 - 34:55
    is we don't bother with this
    print statement,
  • 34:55 - 34:58
    so we wouldn't see all this stuff.
  • 34:58 - 35:00
    All we'd see is Before False, After True.
  • 35:00 - 35:03
    And After would just tell us
    that yeah, we found it.
  • 35:03 - 35:06
    There was a 3 somewhere in
    that long list of numbers.
  • 35:06 - 35:08
    Okay? I am just adding this print
    statement so we can
  • 35:08 - 35:14
    kind of trace it. But basically this
    loop, sort of from here to here, is asking
  • 35:14 - 35:18
    the question, is there the number 3 in the
    list that we're about to go through?
  • 35:19 - 35:20
    Okay?
    Now...
  • 35:22 - 35:26
    How could, I'll just give you a second and
    ask you a quick question.
  • 35:26 - 35:27
    You can pause if you want.
  • 35:29 - 35:32
    How could you improve this loop
    using the break?
  • 35:32 - 35:35
    Where might you put a break
    to make this loop smarter?
  • 35:35 - 35:41
    [SOUND].
  • 35:41 - 35:44
    It's okay if you didn't, if it
    doesn't jump out at you.
  • 35:44 - 35:47
    So, if you think about it.
  • 35:47 - 35:51
    Once you hit true, there's really little
    point in looking at the rest of them.
  • 35:51 - 35:55
    There just is no point.
    So we could put a break right here.
  • 35:57 - 36:01
    Inside this block.
    You'd say, look, I'm looking for a 3.
  • 36:01 - 36:03
    All I care is whether I found it or not.
  • 36:03 - 36:08
    If I find it, I mark it to true that I
    found it, and I get out of the loop.
  • 36:08 - 36:10
    Why bother?
  • 36:10 - 36:11
    Why do all these things?
  • 36:11 - 36:13
    Right, just get out.
    Okay?
  • 36:13 - 36:15
    So don't worry about it.
  • 36:15 - 36:18
    I'm just pointing that out, that's one of
    the places where break could be used.
  • 36:18 - 36:22
    The loop functions either way it just, it
    just loops through all the rest
  • 36:22 - 36:22
    of them as well.
  • 36:25 - 36:27
    Okay. So.
  • 36:28 - 36:34
    Here's this largest value one I've used
    before and you know, away we go.
  • 36:34 - 36:37
    We, you know, we have
    largest_so_far, we check to see
  • 36:37 - 36:38
    if the one we're looking at is better
  • 36:38 - 36:45
    and if it is we keep it and then away we
    go and we find that the largest is 17.
  • 36:45 - 36:46
    What if...
  • 36:47 - 36:50
    What would you have to change in this
  • 36:50 - 36:54
    code to make this search for the
    smallest of all the values?
  • 36:56 - 36:58
    Like point, point where in the screen.
  • 37:00 - 37:03
    Where, what would you have to change to
  • 37:03 - 37:05
    make this look for the smallest
    in the list of values?
  • 37:07 - 37:12
    What is the nature of what's making this
    about being largest?
  • 37:12 - 37:14
    What would you change?
    Okay.
  • 37:16 - 37:17
    Pause if you like.
  • 37:19 - 37:24
    So here is some things that you might do
    to make it work about the smallest.
  • 37:24 - 37:26
    So, hey, one thing we would do.
  • 37:26 - 37:30
    Let's change the name of the variable.
    We had a variable named largest_so_far.
  • 37:30 - 37:32
    And now we'll change it to be called
    smallest_so_far.
  • 37:34 - 37:36
    Changing the variable name doesn't
    change the program at all.
  • 37:36 - 37:38
    But it makes the program easier to read.
  • 37:38 - 37:40
    If the program works.
  • 37:40 - 37:42
    So, it's like smallest so far.
  • 37:42 - 37:44
    Okay, but that didn't make it
    about being small.
  • 37:44 - 37:47
    The thing that made it about being small
  • 37:47 - 37:50
    is change this greater than to a less than.
  • 37:51 - 37:55
    Because we're kind of thinking when
    we're doing largest so far, if the
  • 37:55 - 37:59
    number we're looking at is bigger than
    the largest so far, we keep it.
  • 37:59 - 38:01
    If the number we're looking at in
    the smallest is
  • 38:01 - 38:03
    smaller than the smallest so far,
    then we want to keep it.
  • 38:03 - 38:04
    So this is like,
  • 38:06 - 38:10
    this line here is the keeping line
    and this is the when line.
  • 38:10 - 38:14
    When to keep.
  • 38:14 - 38:15
    We'll keep it if it's smaller.
  • 38:16 - 38:18
    Okay?
  • 38:18 - 38:21
    So that's the key, and so yeah,
    so I name it smallest_so_far,
  • 38:21 - 38:25
    whoop de do. That's good.
    But the real thing that
  • 38:25 - 38:28
    had this being about largeness and
    smallness was whether this, this less
  • 38:28 - 38:31
    than and greater than and this was the
    repeated code that got re-checked
  • 38:31 - 38:36
    over and over again.
    So, but this still has a bug in it.
  • 38:37 - 38:39
    So let's run this visually.
  • 38:42 - 38:42
    Okay.
  • 38:42 - 38:45
    So now we've got a variable called
    smallest so far.
  • 38:46 - 38:48
    We are going to check to see if
    a series of numbers that
  • 38:48 - 38:51
    I'm about to show you are smaller
    than the smallest so far.
  • 38:53 - 38:57
    So the first number is 9.
    Is that smaller than negative 1?
  • 38:59 - 39:02
    No, it's not.
    Negative 1 is smaller.
  • 39:02 - 39:06
    The second number is 41.
    Is that smaller than negative 1?
  • 39:06 - 39:08
    No, it is not.
  • 39:08 - 39:10
    The next number is 12.
    Is that smaller than negative 1?
  • 39:10 - 39:11
    No.
  • 39:11 - 39:14
    Negative 1 is smaller than 12.
  • 39:14 - 39:17
    3? No, not smaller.
  • 39:17 - 39:21
    74? No, not smaller.
  • 39:21 - 39:23
    15? Not smaller.
  • 39:23 - 39:26
    So, we're all done.
  • 39:26 - 39:30
    Yay, and the smallest number we saw
    in the list is...
  • 39:32 - 39:36
    Negative 1?
    Negative 1 wasn't even in the list.
  • 39:36 - 39:38
    So that's not a very good program.
  • 39:42 - 39:45
    So let's take a look at what went wrong
    with this program.
  • 39:45 - 39:47
    So we fixed it, we fixed it as
    best we could. Right?
  • 39:48 - 39:53
    We made it, we changed the words
    largest to smallest and yay, that'll fix.
  • 39:53 - 39:55
    Just makes it more readable, doesn't
    actually change the program.
  • 39:55 - 39:59
    And we made this less than, so now what
    happens is it comes in,
  • 39:59 - 40:03
    if 3 is less than negative 1, smallest so
    far of course is negative 1.
  • 40:03 - 40:06
    It, this just never runs.
  • 40:06 - 40:07
    This never runs. And so
  • 40:07 - 40:12
    as we print, smallest_so_far stays
    negative 1 and oops, that
  • 40:12 - 40:18
    should be negative 1, right there.
    [LAUGH] I'm sorry, I forgot to fix that.
  • 40:18 - 40:21
    Here, let me magically fix that.
  • 40:22 - 40:24
    Boom. [SNAP]
  • 40:24 - 40:27
    So let's take a look at what
    went wrong with this.
  • 40:27 - 40:30
    So here we have the code.
    smallest_so_far is negative 1.
  • 40:30 - 40:32
    We have it fixed so we're checking,
    looking for smaller
  • 40:32 - 40:36
    numbers rather than larger numbers, by
    turning this to less than.
  • 40:36 - 40:43
    But the first time through,
    smallest_so_far is negative 1 and the_num is 3.
  • 40:43 - 40:48
    3 is not less than negative 1, so we skip
    through and the printout of the
  • 40:48 - 40:50
    first line is negative 1 3.
  • 40:50 - 40:54
    And it doesn't take long to realize that
    it's just going to keep doing this.
  • 40:54 - 40:57
    smallest_so_far is going to stay
    negative 1,
  • 40:57 - 41:00
    no matter what we look at on this side.
  • 41:00 - 41:01
    And then we're going to come out
    at the end.
  • 41:02 - 41:07
    And we end up with negative 1 as the
    answer. Not very good.
  • 41:07 - 41:10
    [SIGH].
  • 41:10 - 41:11
    So the question is...
  • 41:14 - 41:18
    What should we make this value be?
    Negative 1?
  • 41:18 - 41:21
    It barely worked in the largest because we
    were working with positive
  • 41:21 - 41:24
    numbers and so starting with negative 1
    as the largest so far
  • 41:24 - 41:27
    was a reasonable assumption as
    long as the numbers were all positive.
  • 41:27 - 41:30
    [SOUND].
  • 41:30 - 41:33
    But what would be the number
    to choose here?
  • 41:33 - 41:34
    Think about that for a second.
  • 41:34 - 41:35
    Pause if you have to.
  • 41:35 - 41:39
    Let me clear it.
    Let me make it real clear.
  • 41:39 - 41:42
    [SOUND].
    What's the right thing
  • 41:42 - 41:46
    to put here? Okay.
  • 41:46 - 41:53
    So, what? A million?
  • 41:53 - 41:59
    That might work, a million might work.
    But what if this number, you know,
  • 41:59 - 42:04
    was, you know, what if, what if all
    these numbers were larger
  • 42:04 - 42:07
    than a million, okay?
    Then, then that wouldn't work.
  • 42:07 - 42:12
    So the problem is there's no real good
    value, unless you could make this be
  • 42:12 - 42:17
    somehow infinity, okay?
    You could make this be infinity.
  • 42:18 - 42:24
    But there is a way to do this in Python.
    And it's a really kind of cool technique.
  • 42:24 - 42:27
    It's sort of a way we signal ourselves,
  • 42:27 - 42:30
    and that is we're going to use
    a special value.
  • 42:30 - 42:30
    Not negative 1.
  • 42:30 - 42:34
    It's not a number. And the special value
    we're going to use is None.
  • 42:36 - 42:41
    It's a different type. It's not a number,
    it's itself its own type.
  • 42:41 - 42:44
    So what we're going to do is mark
    smallest as "None".
  • 42:44 - 42:46
    And, and, and sort of at a high level what
  • 42:46 - 42:50
    we're really saying is we haven't seen
    anything so far.
  • 42:50 - 42:53
    The smallest we've seen so far is none.
  • 42:53 - 42:55
    We've not seen anything so far.
  • 42:55 - 42:58
    Now we have to change our loop,
    our little if inside the loop.
  • 42:58 - 43:00
    This is this intelligence in the middle.
  • 43:00 - 43:03
    First we say if smallest is None.
  • 43:03 - 43:06
    is is an operator, part of the
    Python language.
  • 43:06 - 43:10
    If smallest is None, exactly the
    same as None,
  • 43:10 - 43:12
    then the smallest we've seen so far
    is the value.
  • 43:12 - 43:17
    Now this is going to happen the first time
    [SOUND].
  • 43:17 - 43:20
    Because smallest starts out None and then
    as soon as we set smallest
  • 43:20 - 43:24
    to the value, it's going to be that first
    number, so it's going to be 9.
  • 43:24 - 43:27
    Okay? So the smallest is quickly
    going to become 9.
  • 43:27 - 43:33
    Then we print out the new, the smallest
    is 9 after we've seen the 9.
  • 43:33 - 43:37
    Then we go up to the top and we say, is
    smallest None?
  • 43:37 - 43:41
    And the answer is no it is not,
    because smallest is now 9.
  • 43:41 - 43:46
    Then this else-if is going to ask, is the
    value we're looking at, which is 41,
  • 43:46 - 43:51
    is the value less than smallest?
    Well, no, it is not.
  • 43:51 - 43:52
    9 is smaller than 41.
  • 43:52 - 43:56
    And, so in a sense, after the
    first time it's executed,
  • 43:56 - 43:58
    after the first time the statement
    is executed,
  • 43:58 - 44:00
    this is going to always be false, right?
  • 44:00 - 44:02
    Because smallest is no longer None
    and this is going to be
  • 44:02 - 44:05
    the thing that really is operating.
  • 44:05 - 44:06
    And then it's going to work.
  • 44:06 - 44:09
    And when we, you know, smallest will
    become 9.
  • 44:09 - 44:11
    The smallest so far is 9, but then we see
  • 44:11 - 44:15
    the 3 finally, and the value of
    the 3 is less than 9.
  • 44:15 - 44:18
    And so then we take 3 and we stick it into
  • 44:18 - 44:22
    smallest and we end up with this,
    and then the loop
  • 44:22 - 44:24
    runs some more times, and when
    we're all done we have 3.
  • 44:26 - 44:30
    So the trick here is we put this
    None in, and we have a
  • 44:30 - 44:34
    little more if code to check to see
    if we haven't seen anything so far.
  • 44:34 - 44:36
    This is what, you can think of this
  • 44:36 - 44:41
    as a way to trigger on the first,
    first iteration.
  • 44:41 - 44:44
    Special code that's really going to, it
    could, it looks at it
  • 44:44 - 44:46
    on each iteration, but it's never true
    after the first iteration.
  • 44:48 - 44:49
    Okay? So that's just a technique.
  • 44:52 - 44:55
    So this is and the is not operator,
    I think it's a real elegant thing.
  • 44:55 - 45:02
    Don't start overusing it. It's,
    at a low level
  • 45:02 - 45:06
    its real meaning is exactly the
    same as in type and value.
  • 45:08 - 45:11
    There's an is and there's an
    is not, but don't, like,
  • 45:11 - 45:16
    say, like, if, don't do things like
    saying if i equals.
  • 45:16 - 45:20
    Oops.
    I won't even let myself type the bad code.
  • 45:20 - 45:23
    if i is 4.
  • 45:23 - 45:26
    Don't say that, okay? Don't say that.
  • 45:26 - 45:28
    Don't do if i is 4.
  • 45:30 - 45:34
    It may work in certain situations.
    It's really best used in very limited
  • 45:34 - 45:37
    situations where you're checking
    for some of these
  • 45:37 - 45:41
    special values like None and False.
  • 45:41 - 45:42
    Okay.
  • 45:42 - 45:46
    The problem is if you use equality here,
    it tries to kind of
  • 45:46 - 45:51
    convert values and it may end up
    giving you a false yes.
  • 45:51 - 45:54
    And so is is a stronger equality than
    simple equals.
  • 45:56 - 46:03
    Equals is same value, same numeric value,
    whereas is is exactly the same thing.
  • 46:03 - 46:06
    But don't, don't overuse is. Use
    double equals
  • 46:06 - 46:09
    95% of the time and use is when you're
  • 46:09 - 46:13
    checking if it's one of these special
    constants like True or False.
  • 46:14 - 46:14
    Okay?
  • 46:16 - 46:19
    Okay.
    So this is a iterations.
  • 46:19 - 46:21
    I mean our loops are going to get more
    sophisticated and
  • 46:21 - 46:24
    we have more interesting things
    to do, but we,
  • 46:24 - 46:25
    you know, we talked about some
  • 46:25 - 46:28
    indefinite loops, definite loops,
    iteration variables.
  • 46:28 - 46:33
    Some patterns like maximum, minimum,
    summing, averaging, you know.
  • 46:33 - 46:37
    We introduced the concept of None, you
    know, and, and so this is.
  • 46:37 - 46:40
    We're getting there, we've got a couple
    more chapters before we really
  • 46:40 - 46:43
    start hitting the data analysis, so see
    you in the next lecture.
Title:
Python for Informatics: Chapter 5 - Iterations
Description:

This covers chapter 5 for my book Python for Informatics - Exploring data from www.pythonlearn.com.
All Lectures: http://www.youtube.com/playlist?list=PLlRFEj9H3Oj4JXIwMwN1_ss1Tk8wZShEJ

more » « less
Video Language:
English
Team:
Captions Requested
Duration:
46:44

English subtitles

Revisions