Python for Informatics – Chapter 3 – Conditional Execution

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


  1. Mohamed Qasem said:

    Hello Chunk, you are such a great Teacher!
    if you don't mind can you tell me if this is a right answer to the first Q?

    rate = 15 H_of_working = 50
    if H_of_working > 40 :
    over_time = H_of_working – 40
    rate = (rate * 1.5)
    pay = rate * over_time
    print pay

    Thank you for letting us learn from you!

    June 28, 2019
  2. Tree Spring said:


    June 28, 2019
  3. Khaled El khatib said:

    So why we did not use else here? if I did not use it finis does not print.
    x = 5
    if x < 10:
    print ('Smaller')

    if x >20:
    print ("bigger")

    print "finis"

    You are a great teacher

    June 28, 2019
  4. SQ K said:

    You are a great teacher. Thank you very much

    June 28, 2019
  5. qbest84 said:

    So I have a question about your try/except sample. you asked to enter a number. -1 is a number?or not? it seems that all the numbers that are negative were not numbers in this littler program.

    June 28, 2019
  6. Tian Zhang said:

    Thanks a lot Chuck! You are so funny, totally agree about ==should be ?= thing 🙂

    June 28, 2019
  7. The_Lucid _Gamer said:

    great job carry on helping

    June 28, 2019
  8. Debashis Ghosh said:

    Its so amazing. How can you explain these concepts so simply!! WOW!!

    June 28, 2019
  9. shabnam naaz said:

    thanks a ton

    June 28, 2019
  10. Francisco Javier Martínez Molina said:

    Thank you for your time and information.

    June 28, 2019
  11. Luku Zuku said:

    28:59 He said Derp :p

    June 28, 2019
  12. General Stone Star Gaming said:

    Hey Mr. Chuck why dont you get a better channel banner? I could make you one…

    June 28, 2019
  13. Johnny English, PhD said:

    This is sinking into my brain more thoroughly than Udacity's Python course was. I think the reasons are…

    1. Black background (easier on the eyes than white is).
    2. Computer font instead of handwritten text.

    I am aware that I may just be biased because this is the second time I have learned the basics of Python; the second time is always easier. The best way to test that, is to ask someone who has switched from this to Udacity, if they find Udacity easier than this. I'm pretty sure I'm not being biased though, but then I would say that – obviously.

    Full playlist…

    June 28, 2019
  14. BISDAK said:

    earn a sub heheh love it. the best video i ever seen so far

    June 28, 2019
  15. Theodore Martin said:

    Thanks for this informative video chuck!

    June 28, 2019
  16. Mehpass said:

    thank you.

    June 28, 2019

Leave a Reply

Your email address will not be published. Required fields are marked *