Here's this largest value one I've used before and you know, away we go.
We, you know, we have largest_so_far, we check to see
if the one we're looking at is better
and if it is we keep it and then away we go and we find that the largest is 17.
What if...
What would you have to change in this
code to make this search for the smallest of all the values?
Like point, point where in the screen.
Where, what would you have to change to
make this look for the smallest in the list of values?
What is the nature of what's making this about being largest?
What would you change? Okay.
Pause if you like.
So here is some things that you might do to make it work about the smallest.
So, hey, one thing we would do.
Let's change the name of the variable. We had a variable named largest_so_far.
And now we'll change it to be called smallest_so_far.
Changing the variable name doesn't change the program at all.
But it makes the program easier to read.
If the program works.
So, it's like smallest so far.
Okay, but that didn't make it about being small.
The thing that made it about being small
is change this greater than to a less than.
Because we're kind of thinking when we're doing largest so far, if the
number we're looking at is bigger than the largest so far, we keep it.
If the number we're looking at in the smallest is
smaller than the smallest so far, then we want to keep it.
So this is like,
this line here is the keeping line and this is the when line.
When to keep.
We'll keep it if it's smaller.
Okay?
So that's the key, and so yeah, so I name it smallest_so_far,
whoop de do. That's good. But the real thing that
had this being about largeness and smallness was whether this, this less
than and greater than and this was the repeated code that got re-checked
over and over again. So, but this still has a bug in it.
So let's run this visually.
Okay.
So now we've got a variable called smallest so far.
We are going to check to see if a series of numbers that
I'm about to show you are smaller than the smallest so far.
So the first number is 9. Is that smaller than negative 1?
No, it's not. Negative 1 is smaller.
The second number is 41. Is that smaller than negative 1?
No, it is not.
The next number is 12. Is that smaller than negative 1?
No.
Negative 1 is smaller than 12.
3? No, not smaller.
74? No, not smaller.
15? Not smaller.
So, we're all done.
Yay, and the smallest number we saw in the list is...
Negative 1? Negative 1 wasn't even in the list.
So that's not a very good program.
So let's take a look at what went wrong with this program.
So we fixed it, we fixed it as best we could. Right?
We made it, we changed the words largest to smallest and yay, that'll fix.
Just makes it more readable, doesn't actually change the program.
And we made this less than, so now what happens is it comes in,
if 3 is less than negative 1, smallest so far of course is negative 1.
It, this just never runs.
This never runs. And so
as we print, smallest_so_far stays negative 1 and oops, that
should be negative 1, right there. [LAUGH] I'm sorry, I forgot to fix that.
Here, let me magically fix that.
Boom. [SNAP]
So let's take a look at what went wrong with this.
So here we have the code. smallest_so_far is negative 1.
We have it fixed so we're checking, looking for smaller
numbers rather than larger numbers, by turning this to less than.
But the first time through, smallest_so_far is negative 1 and the_num is 3.
3 is not less than negative 1, so we skip through and the printout of the
first line is negative 1 3.
And it doesn't take long to realize that it's just going to keep doing this.
smallest_so_far is going to stay negative 1,
no matter what we look at on this side.
And then we're going to come out at the end.
And we end up with negative 1 as the answer. Not very good.
[SIGH].
So the question is...
What should we make this value be? Negative 1?
It barely worked in the largest because we were working with positive
numbers and so starting with negative 1 as the largest so far
was a reasonable assumption as long as the numbers were all positive.
[SOUND].
But what would be the number to choose here?
Think about that for a second.
Pause if you have to.
Let me clear it. Let me make it real clear.
[SOUND]. What's the right thing
to put here? Okay.
So, what? A million?
That might work, a million might work. But what if this number, you know,
was, you know, what if, what if all these numbers were larger
than a million, okay? Then, then that wouldn't work.
So the problem is there's no real good value, unless you could make this be
somehow infinity, okay? You could make this be infinity.
But there is a way to do this in Python. And it's a really kind of cool technique.
It's sort of a way we signal ourselves,
and that is we're going to use a special value.
Not negative 1.
It's not a number. And the special value we're going to use is None.
It's a different type. It's not a number, it's itself its own type.
So what we're going to do is mark smallest as "None".
And, and, and sort of at a high level what
we're really saying is we haven't seen anything so far.
The smallest we've seen so far is none.
We've not seen anything so far.
Now we have to change our loop, our little if inside the loop.
This is this intelligence in the middle.
First we say if smallest is None.
is is an operator, part of the Python language.
If smallest is None, exactly the same as None,
then the smallest we've seen so far is the value.
Now this is going to happen the first time [SOUND].
Because smallest starts out None and then as soon as we set smallest
to the value, it's going to be that first number, so it's going to be 9.
Okay? So the smallest is quickly going to become 9.
Then we print out the new, the smallest is 9 after we've seen the 9.
Then we go up to the top and we say, is smallest None?
And the answer is no it is not, because smallest is now 9.
Then this else-if is going to ask, is the value we're looking at, which is 41,
is the value less than smallest? Well, no, it is not.
9 is smaller than 41.
And, so in a sense, after the first time it's executed,
after the first time the statement is executed,
this is going to always be false, right?
Because smallest is no longer 1 and this is going to be
the thing that really is operating.
And then it's going to work.
And when we, you know, smallest will become 9.
The smallest so far is 9, but then we see
the 3 finally, and the value of the 3 is less than 9.
And so then we take 3 and we stick it into
smallest and we end up with this, and then the loop
runs some more times, and when we're all done we have 3.
So the trick here is we put this None in, and we have a
little more if code to check to see if we haven't seen anything so far.
This is what, you can think of this
as a way to trigger on the first, first iteration.
Special code that's really going to, it could, it looks at it
on each iteration, but it's never true after the first iteration.
Okay? So that's just a technique.
So this is and the is not operator, I think it's a real elegant thing.
Don't start overusing it. It's, at a low level
its real meaning is exactly the same as in type and value.
There's an is and there's an is not, but don't, like,
say, like, if, don't do things like saying if i equals.
Oops. I won't even let myself type the bad code.
if i is 4.
Don't say that, okay? Don't say that.
Don't do if i is 4.
It may work in certain situations. It's really best used in very limited
situations where you're checking for some of these
special values like None and False.
Okay.
The problem is if you use equality here, it tries to kind of
convert values and it may end up giving you a false yes.
And so is is a stronger equality than simple equals.
Equals is same value, same numeric value, whereas is is exactly the same thing.
But don't, don't overuse is. Use double equals
95% of the time and use is when you're
checking if it's one of these special constants like True or False.
Okay?
Okay. So this is a iterations.
I mean our loops are going to get more sophisticated and
we have more interesting things to do, but we,
you know, we talked about some
indefinite loops, definite loops, iteration variables.
Some patterns like maximum, minimum, summing, averaging, you know.
We introduced the concept of None, you know, and, and so this is.
We're getting there, we've got a couple more chapters before we really
start hitting the data analysis, so see you in the next lecture.