Loop idioms are how we construct loops.
And we're going to, the loops kind of have some kind of a goal in mind.
Finding the largest, we played with that.
Finding the smallest, counting the number of things,
looking for lines that start with pound signs, something like that.
They, they have a kind of a high-level view of what they're supposed to do,
and then we have to kind of build a loop to accomplish that.
And and this goes back to how we have to think like a computer, right?
We have to say, hey computer,
do this over and over and over again, and then I'll
get what I want once you've done that over and over again.
You have to do something a million times.
I'm not going to sit here and wait.
At the end, I'll get what I want.
So I call these kind of "smart loops",
or how to kind of build intelligence into loops.
So, for example, we want the largest number.
Right?
But we have to construct a loop that will
get us the largest number thinking like a computer.
Okay? Thinking computationally.
Thinking like a computer.
So the idea is that we have some kind
of a loop and we're going to go through this list,
some list of things, and this is going to run a bunch of times.
And, but the way we're going to do it is
we're going to set something up before the loop starts.
We're going to do something to each of the things that's being looked at,
and at the end, we're going to get the result we're looking for.
Okay?
And so in the middle it's kind of like working.
It's in the middle working, da, da, da, da, da.
And then here is the payoff.
The payoff is at the end when we get the information that we're interested in.
So I will sort of use in the next few examples
this simple loop,
and right now it doesn't do much.
It does a print Before and it has this variable
thing that goes through the successive values of these numbers.
And it prints it out, right? So that middle part says
run this six times, once for each of those values.
And then After. Okay?
And so we will add some intelligence at the beginning, we'll add
some intelligence in the middle, and we'll add some intelligence at the end.
And then the whole thing will accomplish what we want.
Right now this is kind of not that intelligent.
So now what I want to do, is I want to review the thing we did, and I want you
to remember what the largest number is, and I'm going to
show you a sequence of numbers in order.
Ready?
One, I'll do it kind of quickly, because you've seen this before.
So, I'm only showing you one number at a time.
So you want to tell me what the largest number is.
So here we go. The first number is 9.
The second number is 41.
The third number is 12. The fourth number is 3.
The fifth number is 74. And the sixth number is 15.
So what was the largest number?
Did you have to go back? Or did you remember how to do it?
Okay, well, I will give you a clue. It was 74.
Okay? That's because I know.
Okay, now if you did that and you had to do that for 20 or 30 numbers,
you'd have to create a mental algorithm in
your head to approach it and stay concentrated, focused.
So, you would've created a variable in your head
called largest_so_far.
I would show you the first number, which would be 9, and you would go hmm.
Well, 9 is larger than 1, negative 1.
So I will keep that. That's the new largest I've seen so far.
That's pretty awesome, because it's way better than negative 1.
41? I thought 9 was good. But 41, that is a lot better.
So I'm going to keep that one.
That's the, that's the best. It's the largest we've seen so far.
We've only seen two numbers.
But the best we've so far is 41.
So 12 is not larger. Who, who cares about that?
It's not as big as 41 so we'll just go right on to the next, on to the next.
3, that's lame when we are looking for large numbers.
So we skip, oh, 74. 74, that's a rockingly large number.
So we're going to, that's a lot.
That's actually the largest we've seen so far
because it's bigger than 41, and 41 was the former champion
largest we've seen so far. And there's 74, so we keep that one.
I don't know how many letters, of these things you're going to see, right?
We could see lots of them. But the next one we see 15.
Well, that's no good.
We've got 74 already. 74's like totally awesome.
Right?
So now, oh, we're done.
So, hey, we're done and so 74 is the champion.
That is the largest.
It's not even the largest so far any more, it's actually the largest.
It's the largest.
So again, we had this thing at the top, we had this loop in
the middle, and at the bottom is where you kind of get the payoff.
And the payoff is
not in the middle.
while we're largest so far, largest so far, largest so far,
but at the end it turned out
once you've looked at all the variables,
all the values, the largest so far is indeed the largest.
Okay.
So here's the algorithm for this.
I have some variables, and remember
that underscores are valid characters in variables.
Now [COUGH] I'm being a little over-explicit in this.
So I have a variable called largest_so_far.
Then what I do is I set it to 1, negative 1.
Then I print Before so we can see that largest_so_far is negative 1.
Then we have a for loop and my variable iteration variable is the_num.
So that's going to take on the successive values: 9, 41, 12, 3, 74, 15,
and run this indented loop of code, okay?
And so the_num will be 9, first time through.
If the_num, 9, is greater than
largest_so_far, then grab the_num and assign
it into largest_so_far. Then print at the end of each loop,
largest_so_far and the_num. So, so, in effect, the_num is 9.
We compare it to negative 1, and negative 1, 9 is higher.
So we make largest_so_far be 9.
Next time through the loop, next time through the loop, num is 41.
So we compare largest_so_far with 41, and we like it, so we store it.
So we like it, we run it, and we print our 41 is the largest we've seen so far.
And we run again.
We come in, the_num now points to 12. the_num, 12, is
not greater than 41, and so we skip. So, the largest
so far stays 41, and we see 12. Similarly, the_num advances to 3.
We skip.
So we saw 3, but the largest so far is still 41.
Continuing, the_num is now 74. It runs, 74 is
greater than 41, and so we run this code. And so we say 74
is stuck in largest_so_far, and indeed, then we print
it out, and largest so far is now 74.
We continue on.
We go up one more time.
The_num points to 15, but 15 is not larger than 74.
And so we skip.
We print out 15 and 74, and then we come out and
at the end, at the end, we get the largest so far.
It, the name no matter, no longer,
I mean it's kind of the largest
so far at the end is the largest, but the variable name.
Okay? Got it?
That's one idiom. So let's just switch to another idiom.
Now counting, how many things are we
going to, how many times is loop going to execute?
How things are we going to find in the loop?
It's all kind of the same notion.
And the pattern is really simple. We start some variable zork.
A better name for this would be count.
But I want to call it zork.
And then we have a loop, and then in the loop we just add 1 to zork,
and at the end, zork.
That should be light blue right there. zork should be the total count.
Now of course we can look at it and say it's going to be 6, but
assume this loop is looping through a million
lines in the file or something like that.
So it's [SOUND], so it's cheating to kind of look at it and say, ooh, it's 6,
because we want to actually compute it. So it's really simple.
You know, zork starts at 0. It's going to run zork is 1 now, and
2, 3, 4, 5, 6, and then we've run out of stuff and then we print out 6.
Okay?
So that's kind of the idiom, right?
Before, during, and after, right? We do something before, we do
something during, and, and in a sense this zork here is the number we've seen so far.
And at the end it becomes kind of the total number.
Summing in a loop, very similar.
Again, you have to think of this is, there is a whole bunch of variables here.
We start a variable at 0.
Each time through the loop, we add whatever it is that we're seeing.
Instead of adding 1 we're adding 9, 41, 12, 3, 74, 15.
And zork would be best thought of as running total.
So, zork is the running total.
And so if we look at the numbers 9,
running total's 9, running total's 50, running total's 62, 65, 139, 154.
And then we skip out and at the end, the running total becomes the total.
Okay?
So, that's another of these patterns that, sort of,
we do something at the beginning, we
do something in the middle, and we have
something very nice for ourselves at the end.
Finding the average of a sequence of values
is the combination of the two previous patterns.
This time I am going to use more mnemonic variables.
A variable called count.
Everyone calls this count.
Sum, now total would maybe be a better word for this.
The running total. And then, so the count and the sum start out at 0, and
then each time through the loop, count equals count plus 1, so we're adding 1 to count.
Sum equal sum plus value, so we're adding 1 to to sum.
I mean adding the value.
Value of course being 9, 41, 12, 3, 74, 15.
And then at the very end we can print out the number.
We have six things with a total of 154, and then we calculate the average.
Of course these are integer numbers, and so this is a truncating division.
So 154 over 6 equals 25 and
not 25 point something.
In Python 3000, Python 3, it'd be better.
But so the average, the integer average is of the numbers we just looked at, is 25.
So sometimes we're searching, like for a needle in a haystack.
Looking for something and again you have to think of like
you're handed some amount of data and you gotta hunt for something.
And there might be a million things and you might only want five of them.
And you can either look by hand or you can write
a loop that's got an if statement that says found it.
Maybe I found it at line seven or found it wherever.
So this is filtering or finding or searching, looking
for a needle in a haystack, in a loop.
And so the, the idea basically is that we have this loop,
it's going to go through all the values, 9, 41, 12, 3, 74.
But we put in the loop, we embed an if statement.
If the value we're looking at is greater than 20, print I found it.
So when value is 9, this is going to do nothing and just go and make value be 41.
And then value 41, oop, yep, there we go, print Large number, so off this comes.
Then value becomes 12, nothing happens, value becomes 3, nothing happens,
value becomes 74, oops, this time it's going to happen.
So out comes Large number 74.
Then value becomes 15, nothing happens, and then value is all done,
and so it comes and finishes.
So this is the searching or filtering or catching or, or whatever.
Okay?
We can also sort of, if we don't just want
to print everything out, we want to say is something in there.
Go look through this million things and
tell me if blah exists.
And in this we're going to introduce the notion of Boolean variable.
A Boolean is a true-false.
It only has two values and we've already used it in the while True.
So that capital True, that is a constant, just like 7 or 42 or 99, or "Sam".
And so we're going to make this variable called found.
Now found is a mnemonic value, variable. It's just a name I picked.
So found equals False.
This is going to be false, until we find what
we're looking for and then it's going to switch to true.
So it starts out and it's false.
Then we're going to run this bit of code three times.
And if the value that we are looking at is 3, then we're going
to set found to be true, and we'll print found value each time through.
So value's going to take on 9, 41, 12, 3, 74, so we get a line
of output for each one. And the first time through
it's not yet found, because we're looking at a 9.
Second time, it's not yet found.
We looked at 41, still false. So it could stay false for long time.
Oh, we found a true.
And then that means that this code is going to run once.
And so you can kind of think of this found variable as sticky.
It's going to stay false, and then the rest of the loop
it's going to stay true, and at the end it is true.
Now the way we usually do these kinds of things
is we don't bother with this print statement,
so we wouldn't see all this stuff.
All we'd see is Before False, After True.
And After would just tell us that yeah, we found it.
There was a 3 somewhere in that long list of numbers.
Okay? I am just adding this print statement so we can
kind of trace it. But basically this loop, sort of from here to here, is asking
the question, is there the number 3 in the list that we're about to go through?
Okay? Now...
How could, I'll just give you a second and ask you a quick question.
You can pause if you want.
How could you improve this loop using the break?
Where might you put a break to make this loop smarter?
[SOUND].
It's okay if you didn't, if it doesn't jump out at you.
So, if you think about it.
Once you hit true, there's really little point in looking at the rest of them.
There just is no point. So we could put a break right here.
Inside this block. You'd say, look, I'm looking for a 3.
All I care is whether I found it or not.
If I find it, I mark it to true that I found it, and I get out of the loop.
Why bother?
Why do all these things?
Right, just get out. Okay?
So don't worry about it.
I'm just pointing that out, that's one of the places where break could be used.
The loop functions either way it just, it just loops through all the rest
of them as well.
Okay. So.