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