Python for Informatics - Chapter 3 - Conditional Execution
-
0:00 - 0:05Hello, and welcome to Chapter Three of
Python for Informatics. -
0:05 - 0:07Chapter One, Chapter Two, now we're
-
0:07 - 0:09going to get to something kind of
programmy. -
0:09 - 0:16I mean, assignment statements and reserved
words, that just kind of gurgling. -
0:16 - 0:18Now we're going to start seeing
composition. -
0:18 - 0:21We're going to start seeing the
conditional execution. -
0:21 - 0:23Gets us started, sort of, seeing the power
-
0:23 - 0:26of computers, where you're starting to
make decisions. -
0:26 - 0:31So, as always, this lecture and audio,
video, and slides are also available, -
0:31 - 0:35are copyright, Creative Commons
Attribution. -
0:35 - 0:40So, conditional steps are steps that may
or may not be executed. -
0:40 - 0:43So here's, here's a bit of code.
-
0:43 - 0:46So, and, and I draw these pictures. I won't
-
0:46 - 0:48draw too many of these pictures on the
left-hand side. -
0:48 - 0:51If you've taken a programming class, you
may have seen these. -
0:51 - 0:53They're sometimes called flow charts.
-
0:53 - 0:55Sometimes people really think these are
important. -
0:55 - 0:59I don't think they're all that
important for understanding. -
0:59 - 1:00The, the Python code is here on the
-
1:00 - 1:03right-hand side, and this picture's on the
left-hand side. -
1:03 - 1:08And, and the reality is is that this may,
initially, make more sense, cognitively, -
1:08 - 1:14to you, than this. But this part on the
right-hand side is what's important. -
1:14 - 1:16I like to call these like road maps, so
you can sort of -
1:16 - 1:20trace where the code is going by driving
down a little road. -
1:20 - 1:22That's kind of a, something that you do
once or -
1:22 - 1:25twice and then, pretty soon, you'll
start reading the code. -
1:25 - 1:26So I'm going to start on the right-hand
-
1:26 - 1:29side here, and just walk through the
code. -
1:29 - 1:31Remember, code operates in sequence.
-
1:31 - 1:37Well, there is a if, which is a special
reserved word. -
1:37 - 1:40It's one of those things that you can't,
you can't name a variable if. -
1:41 - 1:45And it is our indication that to Python,
that the next -
1:45 - 1:50statement that we're going to do may or
may not be executed, if. -
1:50 - 1:53And the thing that comes on the same line
as the if, -
1:53 - 1:57up to and including the, the little colon,
is a question. -
1:57 - 1:59This is a question.
-
1:59 - 2:01You're asking a question.
-
2:01 - 2:05So an assignment statement is moving a
value into a variable. -
2:05 - 2:06And a if statement
-
2:06 - 2:08is asking a question.
-
2:08 - 2:10In this case, we're asking a question
about a variable. -
2:10 - 2:16So always think, when you're, sort of,
here, that this is a question to be asked. -
2:16 - 2:18And, you'll notice when I'm doing the same
-
2:18 - 2:20thing over here, I put a question mark
there. -
2:20 - 2:23Is x less than 10?
Yes or no? -
2:23 - 2:25It's a question that has a yes or no.
-
2:25 - 2:28And so, the way this works is, this
statement -
2:28 - 2:32that's indented, after the if, is either
executed or -
2:32 - 2:34not executed based on the result of that
question. -
2:34 - 2:38So the way to sort of read this in
English is set x to 5. -
2:38 - 2:44If x is less than 10, which it is because
x is 5, then we're going to execute this. -
2:44 - 2:46So print Smaller comes out.
-
2:46 - 2:49And then we come back out and we continue
and say, oh, okay, now -
2:49 - 2:52I have another if statement, and then a
bit of, a block of indented code. -
2:52 - 2:56If x is less than 20, that's the question.
-
2:56 - 2:57The answer to that
-
2:57 - 3:01is no, and so it does not run that line,
and so it runs Finis. -
3:01 - 3:05So the printout of this program is
Smaller, followed by Finis. -
3:05 - 3:11What happens is, this line never executes
because the answer -
3:11 - 3:17to this question is false. Okay?
So, let's go through that a little faster. -
3:17 - 3:21Set x to five.
If x is less than 10, print Smaller. -
3:21 - 3:22Then, if x is greater
-
3:22 - 3:26than 20, which it's not, skip that, and
then print Finis. -
3:26 - 3:28That's the short version of it, okay?
-
3:28 - 3:30Conditional steps.
-
3:30 - 3:32This step is conditional, this step is
conditional. -
3:32 - 3:36They may or may not be executed based on
the result of the question. -
3:36 - 3:39Now, if we're thinking of this as like a
GPS -
3:39 - 3:42road map or something, we can look at this
right-hand side. -
3:42 - 3:47So, the CPU comes roaring down
here, x equals 5, okay, I'll run that. -
3:47 - 3:52Then it's faced with a choice.
Do, is x less than 10, yes or no? -
3:52 - 3:56If it is yes, and it is, I will go this
way. -
3:56 - 3:58If it was no, I would go that way.
-
3:58 - 4:01So if it's yes, I go here and I run this
little thing and I print Smaller, great. -
4:01 - 4:02And I follow the little road.
-
4:02 - 4:06And now the road takes me to here.
And it's asking another question. -
4:06 - 4:08Is x greater than 20?
-
4:08 - 4:12This time, the answer is no, so I'd come
down here, right? -
4:12 - 4:12And so,
-
4:12 - 4:15this bit of code is never executed.
-
4:15 - 4:20Now, this is a very simple example, but
you get the basic idea. -
4:20 - 4:22Okay? So that's conditional execution.
-
4:22 - 4:27Now there's a number of conditional
operators that we -
4:27 - 4:30want to use, just like we had
multiplication, division. -
4:30 - 4:34Some of them are pretty intuitive,
and the others, you just kind of have -
4:34 - 4:37to memorize, like less than and
greater than make a lot of sense. -
4:38 - 4:42The one that probably, that, easy, like
less than or equal to, -
4:42 - 4:45or greater than or equal to, those
kind of make sense, too. -
4:45 - 4:47They're less than or equal to.
-
4:47 - 4:50Just because we don't have a less than or
equal to sign on a -
4:50 - 4:54symbol or a greater than or equal sign,
which we would use in mathematics. -
4:54 - 4:57Equality, asking the question of
whether something is -
4:57 - 5:00equal to something else or not,
is double equal. -
5:00 - 5:04And that's because we're already using
single equals as assignment. -
5:04 - 5:10So when we say x equals 3, that is an
assignment and sticks a value into x. -
5:10 - 5:12This is the question.
-
5:12 - 5:14Is x equal to?
-
5:14 - 5:16If I was building a language, I would make
-
5:16 - 5:19it be equal question mark, or something
like that. -
5:19 - 5:22I'd be like, huh, is it equal?
Kind of a question mark. -
5:22 - 5:23But that's not what we do.
-
5:23 - 5:26I didn't invent this, so we're, double
equals -
5:26 - 5:30is the question, is something equal to
another. -
5:30 - 5:35Single equals changes something, x equals
five changes x. -
5:35 - 5:40Okay, and then, not equal, exclamation is
commonly used to mean not in -
5:40 - 5:45computer contexts, so if something
is not equal to something, -
5:45 - 5:50it is exclamation equal.
Here are some examples. -
5:50 - 5:52Just kind of running through them.
-
5:52 - 5:55They're all, they all turn out to be true,
because I set x to 5. -
5:55 - 5:58If x equals 5, print Equals 5.
-
5:58 - 6:02Come out here, if x is greater than 4,
which is true, print Greater than 4. -
6:02 - 6:05If x greater than or equal to 5, yep.
-
6:05 - 6:08If x less than 6, print Less than 6.
-
6:08 - 6:12Now here's a, there are two, sort of,
syntaxes to, to the if statement. -
6:12 - 6:16One is where the if statement is down here
on a separate line and it's indented, -
6:16 - 6:20and the other is where there's a single
line and it's right on the same line, -
6:20 - 6:23if x less than 6, print Less than 6.
-
6:23 - 6:26So this is true, so this whole thing
executes. -
6:26 - 6:29Then it continues down, if x less than or
equal to 5? -
6:29 - 6:30Yep, print Less than or Equal 5.
-
6:30 - 6:35If x is not equal to 6, which is true, cuz
it's 5, then Not equal to 6. -
6:35 - 6:40So, all those will turn out to be true,
and all those will execute. -
6:40 - 6:46And so, the, the tricky bit, here, is, you
know, just knowing, -
6:46 - 6:50seeing this syntax for an if statement,
where it's all one line, and this syntax, -
6:50 - 6:53where you end the first line with a colon
and then indent the second line. -
6:54 - 6:56This, you can only do one line.
-
6:56 - 6:59We will soon see that you can put more
than one line in the indented block. -
7:00 - 7:02Okay.
-
7:03 - 7:05Here we have more than one int line in
-
7:05 - 7:09the indented block, these are called
one-way decisions. -
7:09 - 7:11And so, we say x equals 5,
-
7:11 - 7:14we print out Before 5, so that
prints out. -
7:14 - 7:20If x equals 5, remember the double equals
is the question mark version of equality, -
7:20 - 7:22single equals assignment, it says yes.
-
7:22 - 7:25So we indent, and the convention is to
indent four spaces, -
7:25 - 7:28although it doesn't really matter
as long as you're consistent. -
7:28 - 7:29Then it's going to run all three of those.
-
7:29 - 7:33Is 5, Still 5, Third 5, these lines
all come out. -
7:33 - 7:34And then it comes out and prints,
-
7:36 - 7:39and the de-indenting, the fact that this
print has been moved to line up with -
7:39 - 7:42the if, that's what indicates that
this little -
7:42 - 7:47block of conditional executed code
is finished. -
7:47 - 7:53So then prints out Afterwards 5, comes
down some more, Before 6, then it asks -
7:53 - 7:55another question, if x is equal to 6,
-
7:55 - 7:57again, that's the question mark
version of it. -
7:58 - 8:01And if this is false, now, because x
-
8:01 - 8:02happens to be 5, so the answer
-
8:02 - 8:06to this expression, the logical
expression, is false. -
8:06 - 8:11Then it skips all of the indented bits, so
none of this executes. -
8:11 - 8:14So, since it's false, it skips all of the
indented bit, but then it, -
8:14 - 8:18this print lines up, and so then it picks
back up with Afterwards 6. -
8:18 - 8:21So we call this a one-way decision, where
you have the question, and then -
8:21 - 8:24you have a couple of things that you're
going to do on this true, true thing. -
8:24 - 8:26Or, if it turns out that you're false,
-
8:26 - 8:28you're going to skip all those things.
-
8:30 - 8:34So, Python is actually one of the
-
8:34 - 8:38few languages that uses indentation as
syntactically significant. -
8:40 - 8:43We like to indent code to, for ifs, and
-
8:43 - 8:45in a moment, we'll see you
learn about loops. -
8:45 - 8:46We like to indent code as a way to
-
8:47 - 8:51make sense of stuff, it makes it
easier to read. -
8:51 - 8:55You know, if this thing's inside, and so,
it, it's really quite nice. -
8:55 - 8:55And then,
-
8:55 - 8:57we, sort of, use it as a matching, to
-
8:57 - 9:01help us cognitively understand what's
inside of a program. -
9:03 - 9:06But in Python, it's really, really
important, and it's -
9:06 - 9:08almost, it's, it's, you have to
think of, like, -
9:08 - 9:11when you are moving in, you mean
something, -
9:11 - 9:13and when you move back out, you mean
something. -
9:13 - 9:16So you can increase the indent, which you
do after, like, -
9:16 - 9:18an if statement, or any other statement
that ends in a colon. -
9:18 - 9:21You increase the indent, and then
-
9:21 - 9:23when you're done, you decrease the indent.
-
9:23 - 9:25You maintain the indent, sort of, for
sequential code. -
9:26 - 9:29Now blank lines and comments are ignored.
-
9:29 - 9:31So you can have a blank line and it, it,
the -
9:31 - 9:34indentation just goes right past it and
the comments don't affect it. -
9:34 - 9:42And so, while we're here, we'll interrupt
us for a recommendation. -
9:44 - 9:50In your text editor, Notepad Plus or Text
Edit or TextWrangler, or whatever -
9:50 - 9:55you're using, it may be set, when you hit
the tab key, to move in four spaces. -
9:57 - 10:00Sometimes you also might move in four
spaces by hitting space bar four times. -
10:01 - 10:04Python will see that as different.
-
10:04 - 10:09And it is possible in all of these word
processors to say, hey, -
10:09 - 10:14don't actually put tabs in my document,
when I hit the tab, put in four spaces. -
10:14 - 10:18Then, whether you're hitting the space bar
or hitting the tab, at least you -
10:18 - 10:19are putting the same thing into your
-
10:19 - 10:21document and don't, not freaking
Python out. -
10:23 - 10:26If you don't, you may get indentation
errors. -
10:26 - 10:29Indentation errors are syntax errors to
Python. -
10:29 - 10:32And what's really frustrating is, if you,
it looks good -
10:32 - 10:34to you in your text editor,
you have an if, -
10:34 - 10:37and the block goes in, and comes back out,
but one of them is -
10:37 - 10:41four spaces and one of them is a tab, then
Python will yell at you. -
10:41 - 10:44And this is really frustrating, when
Python yells at you about that. -
10:45 - 10:49So what I'd like you to do is go into
your text editor, whatever it is, -
10:50 - 10:54into the properties or the
settings. -
10:54 - 10:59And here is, you know, your, yours may be
different, but here is where you set this. -
11:02 - 11:06Auto expand tabs, that is on the Mac in
TextWrangler, and then, -
11:06 - 11:09in Notepad Plus Plus, there is replace
-
11:09 - 11:11tabs as spaces, and that's underneath
preferences. -
11:11 - 11:13So you have to find it.
-
11:13 - 11:17Stop right now, and go set this so you're
not going to make yourself crazy. -
11:19 - 11:22Okay, so, this is kind of a busy slide,
but it gives -
11:22 - 11:28you the sense that you have to explicitly
think about indenting and de-indenting. -
11:28 - 11:30Okay? And so I'm just going to walk
through this. -
11:30 - 11:33So, when you have two lines lining up
-
11:33 - 11:36that means they're going to run
sequentially. -
11:36 - 11:40If you see an if, or later here, we'll
see a for. -
11:40 - 11:43We haven't talked about for yet, but
it's, it's like if. -
11:43 - 11:46So, the fact that we go from
this second line -
11:46 - 11:48to this third line and move the indent in,
we're actually -
11:48 - 11:51creating a block that has to do
with this if, -
11:51 - 11:53and it, you can also kind of tell these,
the if and -
11:53 - 11:56the for end in a colon character.
-
11:56 - 11:59Now, we could pull this print back out,
but we want -
11:59 - 12:02it to be part of the if, so we maintain
the indent. -
12:02 - 12:05And then we're done with the if
by pulling out. -
12:05 - 12:10So we line the p with the i, and that
means this is outside of the if. -
12:11 - 12:15This for, which we haven't learned about
for yet, for is another -
12:15 - 12:19statement that ends in a colon, and
afterwards you have to indent. -
12:19 - 12:22Then you maintain the indent.
Here's an if. -
12:22 - 12:25But now we have an if, and we're
already in, -
12:25 - 12:27but that ends in a colon, so we go in
farther. -
12:29 - 12:30And now this is the block.
-
12:30 - 12:36Now, we come back out, and we line up with
that if, right there, okay? -
12:36 - 12:39And now, at the end of this, this indent,
this x here -
12:39 - 12:43comes all the way back out, so it
lines up. -
12:43 - 12:45The rest of these are kind of weird in
that -
12:45 - 12:48comments don't matter, blank lines don't
matter. -
12:48 - 12:51And so, it just is, sort of, you have to
-
12:51 - 12:55get, mentally get used to the notion that
these don't count. -
12:55 - 12:57They can really cognitively mess you up.
-
12:57 - 13:00So these don't count.
-
13:00 - 13:01And now, if I look through it, without,
-
13:01 - 13:04with the comments hidden, it starts in
column one. -
13:05 - 13:10Ignore, ignore, goes in, stays in, ignore,
ignore, -
13:10 - 13:14ignore, comes out.
So that's, it all makes sense. -
13:14 - 13:18Those comments and blank lines are just,
kind of, confusion. -
13:19 - 13:24So, increasing and decreasing indent has
meaning in Python. -
13:24 - 13:26We'll learn more about this in a bit.
-
13:26 - 13:29Our programs won't get this complex right
away, but it's important to -
13:29 - 13:32think, these indents aren't just pretty;
-
13:32 - 13:35they actually are communicating something
to Python. -
13:35 - 13:38And what they're communicating is,
basically, what's in a block. -
13:39 - 13:41And it shouldn't take you very long, when
you -
13:41 - 13:45start looking at Python, to sort of
visualize these blocks. -
13:45 - 13:48So, here, here's a big block.
-
13:48 - 13:51This block here, that's got these three
things. -
13:51 - 13:52And then, this is a block as well, and
-
13:52 - 13:55you can kind of say, well, here's an if
statement. -
13:55 - 13:57And then these are the two statements that
are part of that if statement. -
13:57 - 14:01So mentally, you kind of make these block
pictures. -
14:01 - 14:04So here's another block.
This is that for loop. -
14:04 - 14:06This part's the indented part, but then
there's a block inside of the block. -
14:06 - 14:09So you gotta kind of start seeing that as
well. -
14:09 - 14:11So this is a block that has to do with
this green block -
14:11 - 14:15is the, the one that has to do
with the if. -
14:16 - 14:18And then there's a block here, and then
this is -
14:18 - 14:20a great big block because this is where it
finally de-indents. -
14:20 - 14:23So, don't worry about it yet, but at some
-
14:23 - 14:27point you're just going to start seeing
this indenting and de-indenting -
14:27 - 14:31as defining blocks of code
for various purposes. -
14:31 - 14:35Now we don't have all the purposes yet,
but we'll get there. -
14:35 - 14:40So, we saw in that previous thing one
block within a block. -
14:40 - 14:42And, and we're going to do that.
-
14:42 - 14:44We can have ifs, we can have loops that
-
14:44 - 14:46get indented, but then we can indent even
more. -
14:46 - 14:49We call that nested, where there's an
indented -
14:49 - 14:51area that's in an area that's already
indented. -
14:53 - 14:55So here's a nested decision.
-
14:55 - 14:56And it might be easier to start on
-
14:56 - 14:59this side, where I'm going to have
a first choice. -
14:59 - 15:01Is x greater than 1, yes or no, and if
-
15:01 - 15:04it's yes, I'll do some work, and
then I'm going to -
15:04 - 15:07ask another question, and if that's yes,
then I'm going to -
15:07 - 15:08do this, then I'm going to come all the
way back in. -
15:08 - 15:13And the way we encode this in Python is, x
equals 42, -
15:13 - 15:18if x is greater than 1, it's true, so we
continue working in the indent. -
15:18 - 15:22And now we say, oh, if x is less than 100,
which is still true, -
15:22 - 15:26so we go in farther, and we do this,
and now we come out. -
15:26 - 15:29We don't come out to here, we actually
keep going -
15:29 - 15:32all the way to here, so that
ends both blocks. -
15:32 - 15:35And so if you sort of think about this,
-
15:35 - 15:37again this is where I want you
to start seeing -
15:37 - 15:40what's in a block of code and
what's not in -
15:40 - 15:44a block of code, and how the
indents sort of, -
15:44 - 15:46like, put a boundary on the
blocks of code. -
15:46 - 15:50And so, the first thing you should see is,
sort of, like, that -
15:50 - 15:54purple part, the, the x less than 100,
print, that's kind of a box. -
15:54 - 15:58And you can see the box on the, on the
sort of flow diagram as well. -
15:58 - 16:00The boxes are there. The boxes
on the flow -
16:00 - 16:03diagram are places where there's one
entrance and one exit. -
16:09 - 16:11And then there's also, sort of, the
larger box, right? -
16:11 - 16:14There's this if box that includes the
smaller box. -
16:14 - 16:17So, the, there's this nesting, which is
boxes -
16:17 - 16:21within boxes, or indented areas within
indented areas. -
16:26 - 16:28Now that was a, what we call a one-way
decision, where -
16:28 - 16:31you're doing if, and this code either runs
or it doesn't run. -
16:31 - 16:35It is extremely common to want to
basically say, -
16:35 - 16:38look, I'm going to do one of two things.
I'm going to -
16:38 - 16:40ask a question, if the question is true,
I'm going to do -
16:40 - 16:43one thing. If the question's false, I'm
going to do another thing. -
16:43 - 16:46So that's what we have shown here.
-
16:46 - 16:49We say, is x equals 4, is x equal to
question mark? -
16:49 - 16:51If it's yes, we're going to go here.
-
16:51 - 16:53If it's no, we're going to go here.
-
16:53 - 16:55We,re going to execute one or the other, and
then we're going to continue. -
16:56 - 16:58So we're really at a fork in the road
here, right? -
16:58 - 17:01We're, we're at a fork in the road,
going to make -
17:01 - 17:05a choice, and one or the other, but never
both, right? -
17:05 - 17:08So, we're going to do one thing, or we're
going to do another thing. -
17:08 - 17:10We're going to do one of the two, and
-
17:10 - 17:12depending on what the question
that we ask, -
17:12 - 17:15the question that we ask is, which one
that we're going to do. -
17:18 - 17:19So here's a little bit of code.
-
17:22 - 17:26x equals 4, is x greater than 2, the
answer is yes. -
17:26 - 17:29Then we come out and hit this else and we
automatically -
17:29 - 17:33skip, right, because we're only going to
do one of the two. -
17:33 - 17:37And here's the picture, x equals 4.
Is x equal to yes? -
17:37 - 17:39Print, done.
-
17:39 - 17:43Which means we'll never do both this and
that, never do both, both sides. -
17:43 - 17:46We're going to do one or the other of the
sides. -
17:46 - 17:49And just sort of
going with the box, -
17:49 - 17:51that is our box,
oops, go back, go back. -
17:53 - 17:55This is our box, right?
-
17:55 - 17:58It's sort of the indent followed by the
final indent. -
17:58 - 18:00The else is really kind of part of it.
-
18:00 - 18:04And then we can draw the picture here.
It has one entry and one exit. -
18:06 - 18:11Okay.
So we have one-way ifs, and we have -
18:11 - 18:16two-way ifs, and now we have multi-way
ifs, okay? -
18:16 - 18:23So, here is a multi-way if,
-
18:23 - 18:31and it introduces a new reserved word, elif,
-
18:31 - 18:33which is a combination of else and if.
-
18:33 - 18:38And this one, probably, is just as easy to
talk about the picture here. -
18:38 - 18:40The first question is asked, there's still
-
18:40 - 18:42going to only be one, there's only
going to -
18:42 - 18:46be one, one and only one of these three
choices are going to run. -
18:46 - 18:50Once it's run one, then it's done, okay?
-
18:50 - 18:54So the, the way to think about this, if x
is less than 2, we're going to -
18:54 - 18:56run this one, and then we're going to
kind of -
18:56 - 18:59flush all the way out to the bottom.
-
18:59 - 19:01If x is not less than 2, and it's less
than 10, -
19:01 - 19:04we're going to run this one, then
flush out the bottom. -
19:04 - 19:06And if x is not less than 2, and x is not
-
19:06 - 19:12less than 10, we're going to run this one,
and flush out the bottom. -
19:12 - 19:18So, one of these three, one, two, three,
one of those three is going to run. -
19:18 - 19:23And it's going to run based on the
questions that are being asked. -
19:23 - 19:24The questions do get asked
-
19:24 - 19:26in an order, and the order
does matter, okay? -
19:26 - 19:29So that is a multi-way if.
-
19:29 - 19:35If, else if, else.
So this is kind of like an otherwise. -
19:35 - 19:39The else is like an otherwise, you know,
one way or another, we're going to run -
19:39 - 19:43something, and if none of these first two
have run, we will run this one. -
19:43 - 19:48We call it a multi-way if, okay?
-
19:48 - 19:52So, here's an example
of our multi-way if. -
19:52 - 19:55That, if we say x equals 0, x equals 0.
-
19:55 - 19:56Is it less than 2?
-
19:56 - 19:57Yes, it is.
-
19:57 - 20:01So we run Small, print Small, and then we
flush out the bottom. -
20:02 - 20:07If we switch, instead, x to 5, x is 5.
-
20:07 - 20:09Is it less than 2?
No, it is not less than 2. -
20:09 - 20:10Is it less than 10?
-
20:10 - 20:14Well, 5 is less than 10.
So the answer is yes, so we print Medium, -
20:14 - 20:19then we flush out the bottom.
One and only one are going to execute. -
20:20 - 20:23Now, in this case, we got x is 20.
-
20:23 - 20:25And so we come through here.
Is it less than 2? -
20:25 - 20:27No, it is not.
-
20:27 - 20:29Is it less than 10?
No, it is not. -
20:29 - 20:31So we're going to do this one, and then
flush out the bottom. -
20:31 - 20:39If we go here, it's false, false, go here,
all else being equal, we run that one. -
20:39 - 20:40So this one doesn't run
-
20:40 - 20:44and that one doesn't run, right?
Because these are like gateways. -
20:44 - 20:46If it were true, it would run it.
-
20:46 - 20:48But it's false, so we're going to skip it.
-
20:48 - 20:51This one, it's false, so we're going to
skip it. -
20:51 - 20:54But then we hit the else, that's like a
catch-all. -
20:54 - 20:58And then if none of these were true,
then it will run the else. -
20:58 - 21:02Any questions?
-
21:02 - 21:04Okay.
Well, -
21:04 - 21:09I'm going to ask you a question, in a
second. -
21:12 - 21:15Okay, so just a couple of things that
probably you're wondering about. -
21:16 - 21:17You don't actually need an else.
-
21:18 - 21:24You can have a multi-way, x equals 5, if x
is less than 2, there's no else here. -
21:24 - 21:26You'll notice that this print just comes
back. -
21:26 - 21:30And so this way, it could, if both of
these are false, it could -
21:30 - 21:34skip them both and just run right through
here, and there's no else clause, okay? -
21:34 - 21:37So, in this case, if this one's
-
21:37 - 21:40going to, the way this one's going to run
is, x equals 5 -
21:40 - 21:43if x is less than 2, it's, it's not.
-
21:43 - 21:44And it skips to here.
-
21:44 - 21:49Else if x is less than 10, which it
is, it will run that one and come here. -
21:49 - 21:55But, for a different value of x, like 95,
boop boop. -
21:57 - 22:02If x was 95, or 59, this would be false.
It would skip it. -
22:02 - 22:05This would, elif, would still be false,
and it would skip it, -
22:05 - 22:08and the only thing it would print out
would be, All done. Okay? -
22:08 - 22:13Okay, you can also have many elifs.
-
22:14 - 22:18So, better change to green.
-
22:18 - 22:20It checks this one, if it's true, it runs
the first one. -
22:20 - 22:21If it's false, it checks this one.
-
22:21 - 22:25If that's true, it run this one, and then
it skips, right? -
22:25 - 22:27And so, so the way to think about
-
22:27 - 22:30this is, is, it just goes through and
checks this one -
22:30 - 22:33false, this one false, false, false, oh, I
finally found one. -
22:33 - 22:35And now I'm done.
-
22:35 - 22:39It still is going to do one and only one
of these. -
22:39 - 22:43This one has an else, so that sooner or
later, it is going to do one. -
22:43 - 22:50And it only will do the else if all of
these are false. All have to be false. -
22:50 - 22:52Then it will, actually, come and hit the
else clause. -
22:54 - 22:55It's great, because there are lots of
situations where -
22:55 - 22:58you're like, oh, is it before eight in the
morning? -
22:58 - 23:00Or is it between eight and noon?
-
23:00 - 23:02Or is it between noon and five?
-
23:02 - 23:06Or after five? After midnight?
I don't know. -
23:06 - 23:11Okay, so, here, coming up, is a question.
-
23:12 - 23:15And, you, there's two puzzles
and I'm going to -
23:15 - 23:16stop so you can look at them for a while.
-
23:16 - 23:19And I want you to figure out,
-
23:19 - 23:22in both sides of this, which of the lines
-
23:22 - 23:27will not execute, regardless of the value
for x. -
23:27 - 23:29So in both sides, there is a line
-
23:29 - 23:32that won't execute, regardless of the
value for x. -
23:32 - 23:33Which will never print?
-
23:33 - 23:38There's two problems, problem A and
problem B. -
23:38 - 23:42Okay, I'll have some coffee while you
think. -
23:42 - 23:44[NOISE].
-
23:55 - 23:59Okay, hopefully you paused it so that you
could actually think for a bit. -
23:59 - 24:01So, so I'm going to guess you probably
-
24:01 - 24:03got the first one right, that's pretty
straightforward. -
24:03 - 24:06I mean, actually, you're in great shape
if you got both of them right. -
24:06 - 24:09If you got any of them right,
you're in great shape -
24:09 - 24:10because that means
you're starting to get it. -
24:10 - 24:15Starting to like, oh, I'm seeing, kind of,
this flow picture, there's a picture. -
24:15 - 24:17I look at these characters that seemingly
-
24:17 - 24:20look like gibberish, and a picture
arrives. -
24:20 - 24:24Or a pattern of access execution arises.
-
24:24 - 24:25That's what we want to see.
-
24:25 - 24:29So, the in the first one, which will
never print? -
24:29 - 24:33Well, we're looking for kind of a value
for x which will be defective. -
24:33 - 24:35So if x is less than 2, we're going
to do this. -
24:35 - 24:39Else, if x is greater than or equal to 2,
we'll do this, else we'll do that. -
24:39 - 24:41Well, here's the problem with this one.
-
24:41 - 24:44For all values of x, it is, is either going,
-
24:44 - 24:49x is less than 2 is either going to be
true or greater than equal to 2. -
24:49 - 24:52Greater than or equal to be, pah,
for X to be -
24:52 - 24:54greater than or equal to 2 is
going to be true. -
24:54 - 24:57So it's going to run this one, or it's
going to run that one. -
24:57 - 24:58So for big numbers, numbers above 2,
-
24:58 - 25:02it's going to run this one;
below 2, it's going to run that one. -
25:02 - 25:05So this one is never going to run, okay?
-
25:05 - 25:07Because the, one of the first two is
going to be -
25:07 - 25:11true, and so the third else situation
is not going to run. -
25:11 - 25:12Hope you got that right.
-
25:14 - 25:17Okay, so let's take a look at the next
one, okay? -
25:17 - 25:20So the question is, you know, if x is less
than 2, do this, -
25:20 - 25:23if x is less 20, do that, and if x is less
than 10, do this, -
25:23 - 25:29and otherwise do that.
Well, the one that will never execute -
25:29 - 25:34is this one. And, x equals 15,
-
25:34 - 25:39no, x equals 15 is a bad one,
x equals 5 is the one that will, -
25:39 - 25:44sort of, cause it to behave badly.
-
25:44 - 25:49And so, if x is 5, this is false.
-
25:49 - 25:53If x is less than 20, this is true, and
then it's done. -
25:53 - 25:59So the problem is, this is the one that
will never execute, because -
25:59 - 26:04if a value is less than 10, it's also
less than 20, so this will be true. -
26:04 - 26:07So for a value like 5, which happens to be
less than 10, -
26:07 - 26:10which you would think would cause that
line to execute, does not. -
26:12 - 26:14This one executes because it's checked
first. -
26:14 - 26:17Now, if we just moved this code, took this
code and -
26:17 - 26:21we moved it down here, then it would make
more sense, okay? -
26:21 - 26:22Oops.
-
26:22 - 26:24If we moved it down there, it would make
more sense. -
26:24 - 26:30But basically, the answer to these is,
change color, this one won't -
26:30 - 26:36execute, and this one will never execute
for any value, so there's the answer. -
26:38 - 26:40Okay, so we're almost done with
conditionals. -
26:40 - 26:44I want to show you one more kind of
conditional. -
26:44 - 26:45It's a little bit different.
-
26:46 - 26:51It's not a bit of code structure
that you make, it is, -
26:51 - 26:57it is dealing with the fact that some
things may blow up. -
26:57 - 27:00Like, if you read a number from a user and
you try to convert it -
27:00 - 27:03to a floating point number, as you may
-
27:03 - 27:06have already done in some of your
homework, -
27:06 - 27:07it can blow up.
-
27:07 - 27:12You know it's going to blow up, but you
don't exactly want it to kill your program. -
27:12 - 27:17So, the concept of try and except are,
hey, this is a dangerous thing. -
27:17 - 27:18I know it might blow up.
-
27:18 - 27:21I know exactly what it might blow up, but
I don't want to die -
27:21 - 27:23I don't want to stop my program
when it blows up. -
27:23 - 27:24I want to continue.
-
27:24 - 27:27And that's the purpose of the except
block. -
27:27 - 27:29So, here's a little bit of code.
-
27:29 - 27:31And, you know, it's, we've done
this code before. -
27:31 - 27:34This is code that's kind of
similar to, like -
27:34 - 27:37your rate and pay homework, where
you read a string -
27:37 - 27:41using raw input, you converted it using
float, but -
27:41 - 27:44then if you typed in Fred, the
thing blows up. -
27:44 - 27:46So we're kind of simulating that
right here. -
27:46 - 27:49So here we have a variable astr
called Hello Bob, -
27:49 - 27:52and then we try to turn it into an
integer. -
27:52 - 27:53And then we're going to print that out,
and then we have -
27:53 - 27:57another string called one, and that has
the letters 1, 2, 3. -
27:57 - 28:01We convert that to an integer, and then we
print that one out. -
28:01 - 28:04The problem is, is that when this runs,
-
28:06 - 28:07this is going to fail.
-
28:07 - 28:10It's going to fail with this traceback,
okay? -
28:10 - 28:17And the problem is, is when the traceback
happens, the program stops executing. -
28:17 - 28:22The traceback is Python's way of asking
you, hey, this -
28:22 - 28:25would be bad, I don't know what to do,
I'm stopping. -
28:25 - 28:30So that means that the rest of your
program is gone, right? -
28:30 - 28:31It,
-
28:31 - 28:34The fact that we had stuff down here
doesn't matter. -
28:34 - 28:37This line died with the traceback.
-
28:37 - 28:38It stopped.
-
28:38 - 28:41It doesn't, like, give you a traceback and
then keep going. -
28:41 - 28:44It gives you a traceback, and
that's the end. -
28:44 - 28:45Now this might be something, instead of
-
28:45 - 28:48just the string, Hello Bob, which is
insane. -
28:48 - 28:52Data might have come from a raw input,
where the user was typing, and you say, -
28:52 - 28:53give me a number, and they type something
-
28:53 - 28:56that's not a number, and this would
blow up. -
28:56 - 28:57It's like, hey, I know it's going to
blow up. -
28:58 - 29:04The problem with this is that you don't,
oops, erp, clear the thing. -
29:04 - 29:06Oh and now we have to start it on fire again.
-
29:06 - 29:08Okay, it's on fire.
-
29:08 - 29:12The problem is, is that in a sense, this
program is you. -
29:13 - 29:17If you recall, we have you as the, typing
these commands -
29:17 - 29:21into these scripts, feeding the central
processing unit, answering the question -
29:21 - 29:22what next?
-
29:22 - 29:28So you should take it a little personally
when your program gets a traceback. -
29:28 - 29:32because that means you, in the form of
your program, have been vaporized. -
29:32 - 29:36And you're not present to give any more
instructions. -
29:36 - 29:37It stops.
-
29:37 - 29:40It stops dead in its tracks.
You are gone. -
29:40 - 29:44So, we want to make sure we control this
behavior. -
29:44 - 29:47We know it might blow up,
-
29:47 - 29:53and we want to capture the situation where
it does, and execute alternate code. -
29:53 - 29:54Okay.
-
29:54 - 29:55So here it goes.
-
29:55 - 29:57It's a bit of syntax.
-
29:57 - 30:00I mentioned that it uses the try and
except keywords. -
30:00 - 30:05These are reserved words in Python.
And then it's a little indented block. -
30:05 - 30:08So, astr equals Hello Bob, great.
-
30:08 - 30:12Try means, we're about to do something
dangerous, let's take out some -
30:12 - 30:13insurance policy on it.
-
30:13 - 30:16And that is, we're going to convert this
to an integer. -
30:16 - 30:20Take astr, convert it to an integer, put
it in istr. -
30:21 - 30:24If that works, great, we'll just continue
on, and ignore this except. -
30:24 - 30:27If it blows up, we're going to jump into
the -
30:27 - 30:31except block, and then we'll have
alternate substitute code. -
30:31 - 30:35In this case, I'm going to set the
variable to negative 1 as an indicator. -
30:35 - 30:38Then I'll print it out, and I'll do it
again. -
30:38 - 30:41Try this code, and away we go.
-
30:41 - 30:46So, when this runs, we know exactly how
it's going to run. -
30:46 - 30:52It, whoa, woop, come back.
-
30:52 - 30:55We'll set this string, the try
takes out the insurance, -
30:55 - 31:02this blows up, so it runs down to here
and runs this part, and then it'll -
31:02 - 31:07print First minus 1.
And it sets the string to 1, 2, 3, not -
31:07 - 31:12123, but 1, 2, 3 as a string.
It takes out an insurance policy. -
31:12 - 31:18This time it works, and that puts
istr is going to be 123, -
31:18 - 31:22so we don't run the accept code, and
so out comes the -
31:22 - 31:28second 1, 2, 3, okay?
So the try is, take out insurance on this -
31:28 - 31:31little bit of code, and if it fails,
-
31:31 - 31:36run this alternate code. If not, skip the
alternate code. -
31:36 - 31:37So it's kind of conditional.
-
31:37 - 31:40If you put multiple lines in the block
between -
31:40 - 31:45the try and the except, it runs until one
dies. -
31:45 - 31:46So it doesn't come back, okay?
-
31:46 - 31:50It's not taking insurance out on,
separately, on all three statements. -
31:50 - 31:53It's like, here's a block of stuff, and if
anything blows up, -
31:53 - 31:57stop.
And the things that run do run. -
31:57 - 31:59So if, this is really kind of bad code,
-
31:59 - 32:02because you really don't want
the print in here. -
32:02 - 32:05It's actually a good idea on the try
except to have as little in the -
32:05 - 32:09try block as you possibly can, so you're
real clear on what's going to fail. -
32:12 - 32:16but, so here we come in, shh, it's Bob,
so it's going to fail. -
32:16 - 32:16We run this.
-
32:16 - 32:18That runs successfully.
-
32:18 - 32:24This blows up, so it quits and jumps into
the except blocks and continues. -
32:24 - 32:28The point is, is that this code never
executes, never executes. -
32:28 - 32:31The other point is, this code does
execute. -
32:31 - 32:34Just because this blew up, this is already
executed, -
32:34 - 32:37it might have done something other, more
complex than -
32:37 - 32:40print Hello, okay?
So, so there you go. -
32:40 - 32:42So, if we look at this kind of in a
picture, -
32:42 - 32:45we, we set this through the try block, it
runs, it runs. -
32:45 - 32:51And the, the try except kind of has this
escape hatch that says, if there is -
32:51 - 32:55a [SOUND] explosion somehow, then
it runs this -
32:55 - 33:00alternate code and then it comes out and
finishes, okay? -
33:00 - 33:02And, again, this, it doesn't go
-
33:02 - 33:07back and finish the block, and it doesn't
undo the work that is done by that. -
33:07 - 33:09So it doesn't un-execute it.
-
33:09 - 33:12If it executes and works, it just keeps on
going, then -
33:12 - 33:17it blows up, and then sort of flushes its
way out, okay? -
33:17 - 33:21So here's an example of how you might do
this in a running program, like the -
33:21 - 33:23programs that you're about to be assigned,
where -
33:23 - 33:26you're supposed to check for user input
having errors. -
33:27 - 33:33So, here is a little conversion of a
number, and -
33:33 - 33:38and so we're saying, you know, enter a number,
and we're putting a string into rawstr. -
33:38 - 33:42It's a string, and and so, we don't know.
-
33:42 - 33:44And here's where we're going to convert it
into an integer, -
33:44 - 33:46and we're just not sure if it's going to
work or not. -
33:46 - 33:52So, we know how int works. It either
converts it or it blows up. -
33:52 - 33:52So we know
-
33:52 - 33:53it's going to do that, we just don't
-
33:53 - 33:56know what the user's going to type, we
don't know. -
33:56 - 33:58So we have to take out insurance on it.
So this runs, -
33:58 - 34:00and then we do a try, and then we
try to convert it, -
34:00 - 34:04and if it works, it's great, and if
-
34:04 - 34:07it fails, it runs this and sets it to
negative 1. -
34:07 - 34:12And afterwards, we either have the number
or negative 1. -
34:12 - 34:16And so, if the person enters 42,
it says Nice work. -
34:17 - 34:19Well, let's show you where it runs.
-
34:19 - 34:25If the person says 42, it runs through
here, gets the string 42, converts that -
34:25 - 34:30to an integer, skips here, and then says,
Nice work, and that's how it runs. -
34:30 - 34:34If, on the other hand, they type
forty two, the words, -
34:34 - 34:37this gets to be the string forty two.
-
34:37 - 34:40It runs here, this blows up.
-
34:40 - 34:42So it comes and runs this part here.
-
34:42 - 34:47And then it says, if ival is greater than
0, which is not true, -
34:47 - 34:51so it runs this part and says
Not a number. -
34:51 - 34:54So this is our way of compensating for
user input -
34:54 - 34:59that might have errors in it, errors that
we're anticipating, right? -
34:59 - 35:02You'd, you'd rather at least
put up some kind of a -
35:02 - 35:05message, rather than just have a
traceback, -
35:05 - 35:07if you're writing code for somebody else.
-
35:07 - 35:08It just kind of is
-
35:08 - 35:13not very classy. So,
-
35:13 - 35:20the classic program to do this is a
time-and-a-half for overtime pay. -
35:23 - 35:26So you get some pay rate like
$10 an hour for your first 40 hours, -
35:26 - 35:30and then you get 15 hours, for
any hours above it. -
35:30 - 35:34So you have to sort of say, oh, okay, if
this ends up being, this ends up being -
35:34 - 35:40some kind of a thing, where, let me draw
that picture a little better. -
35:40 - 35:44Hours greater than 40, you're going to do
one thing, and -
35:44 - 35:48if hours are less than 40, you're going to
do another thing. -
35:48 - 35:50So you have two payout calculations.
-
35:50 - 35:54If the hours are greater than 40, then
you're going to do -
35:54 - 35:59a overtime calculation, which is kind of
like, 40 times the regular rate. -
35:59 - 36:02And then, the number of excess hours,
like 5 overtime hours -
36:02 - 36:06times the reg, rate times
one-and-a-half. -
36:06 - 36:11So this is kind of the calculation that
happens if the hours are greater than 40. -
36:11 - 36:16And then, if the hours are less than 40,
it's just pay -
36:16 - 36:19equals rate times hours.
-
36:19 - 36:24So it, you're going to do one of two
calculations, depending on how it works. -
36:24 - 36:28So that's one of the programming problems
for this chapter. -
36:28 - 36:29That's a classic.
-
36:29 - 36:31It's the classic if, then, else.
-
36:31 - 36:33You can actually do it with an if, then
if you're tricky. -
36:33 - 36:36There's a lot of ways to do this, so pick
a, pick one and do it. -
36:36 - 36:41Now the next thing I want you to do is, I
want you to take that -
36:41 - 36:48same program, do it again in another,
another assignment, or another problem in -
36:48 - 36:54the chapter, and that is have some kind of
a non-numeric input, and have it blow up. -
36:54 - 37:00So if they type, you know, something like
nine, put out an error. -
37:00 - 37:03Or, if they type something here, put out
an error. -
37:03 - 37:06Now, don't write a loop.
No loop. -
37:07 - 37:11This is one execution of the program, and
this is another execution of the program. -
37:11 - 37:12Later, we can write loops.
-
37:12 - 37:15We haven't even talked about loops.
So this is running it twice. -
37:15 - 37:18All I want you to do is exit.
-
37:18 - 37:20So take a look in the book as to how to
just get out. -
37:20 - 37:23So it, it's, it, I don't want you to try
to say, I'm going to -
37:23 - 37:27prompt for these numbers until I get a
good one, we'll do that later. -
37:27 - 37:29I just want you to deal with the
fact that -
37:29 - 37:32you read a thing, you use the try
-
37:32 - 37:35to convert it to a float and see if it
works. -
37:35 - 37:36And if you don't, you just quit.
-
37:36 - 37:40Don't, don't, don't try to be tricky and
repeatedly prompt. -
37:40 - 37:47So don't repeatedly prompt.
One prompt, and then -
37:47 - 37:52quit, okay?
So, this is contintous-, -
37:52 - 37:57conditional execution, if, if then else,
and then I added a little bit with -
37:57 - 38:00the try and except, as well.
-
38:00 - 38:04And the try and except is really a
limited kind of a problem. -
38:04 - 38:09It really is to compensate for errors that
you anticipate are going to happen, and -
38:09 - 38:11you can imagine what you want to do
-
38:11 - 38:15as a replacement for what those
errors are, okay? -
38:15 - 38:15See you next lecture.
- Title:
- Python for Informatics - Chapter 3 - Conditional Execution
- Description:
-
This explores various kinds of conditional execution statements like if, else, elif, and try / except code. We also look at indentation and how indentation is used in Python programs. This is provided by www.pythonlearn.com
All Lectures: http://www.youtube.com/playlist?list=PLlRFEj9H3Oj4JXIwMwN1_ss1Tk8wZShEJ - Video Language:
- English
- Team:
- Captions Requested
- Duration:
- 38:16
Claude Almansi edited English subtitles for Python for Informatics - Chapter 3 - Conditional Execution |