Hello, and welcome to Chapter Three of Python for Informatics.
Chapter One, Chapter Two, now we're
going to get to something kind of programmy.
I mean, assignment statements and reserved words, that just kind of gurgling.
Now we're going to start seeing composition.
We're going to start seeing the 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, here's a bit of code.
So, and, 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, the Python code is here on the
right-hand side, and this picture's on the left-hand side.
And, and the reality is 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 like road maps, so you can sort of
trace where the code is going by driving down a little road.
That's kind of a, something that you do once or
twice and then, pretty soon, you'll 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 that 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 and including the, 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 a 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 and say, oh, okay, now
I have another if statement, and then a bit of, a block of indented code.
If x is less than 20, that's the question.
The answer to that
is no, and so it does not run that line, and so it runs Finis.
So the printout of this program is Smaller, followed by Finis.
What happens is, this line never executes because the answer
to this question is false. Okay? So, let's go through that a little faster.
Set x to five. If x is less than 10, print Smaller.
Then, if x is greater
than 20, which it's not, skip that, and then print Finis.
That's the short version of it, okay?
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
road map or something, we can look at this right-hand side.
So, the CPU comes roaring down here, x equals 5, okay, I'll run that.
Then it's faced with a choice. Do, 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 I 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.
Okay? 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, that, easy, like less than or equal to,
or greater than or equal to, those kind of make sense, too.
They're less than or equal to.
Just because we don't have a less than or equal to sign on a
symbol or a greater than or equal sign, which we would use in mathematics.
Equality, asking the question of whether something is
equal to something else or not, is double equal.
And that's because we're already using single equals as assignment.
So when we say x equals 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.
I'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 we're, double equals
is the question, is something equal to another.
Single equals changes something, x equals five changes x.
Okay, and then, not equal, exclamation is commonly used to mean not in
computer contexts, so if something is not equal to something,
it is exclamation equal. Here are some examples.
Just kind of running through them.
They're all, they all turn out to be true, because I set x to 5.
If x equals 5, print Equals 5.
Come out here, if x is greater than 4, which is true, print Greater than 4.
If x greater than or equal to 5, yep.
If x less than 6, print Less than 6.
Now here's a, there are two, sort of, syntaxes to, 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 less than 6, print Less than 6.
So this is true, so this whole thing executes.
Then it continues down, if x less than or equal to 5?
Yep, print Less than or Equal 5.
If x is not equal to 6, which is true, cuz 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, the tricky bit, here, is, you know, just knowing,
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 in the indented block.
Okay.
Here we have more than one int line in
the indented block, these are called one-way decisions.
And so, we say x equals 5,
we print out Before 5, so that prints out.
If x equals 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 prints out Afterwards 5, comes down some more, Before 6, then it asks
another question, if x is equal to 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 it,
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, 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 see you 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, it, 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, and it's
almost, it's, it's, 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, like,
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.
Now blank lines and comments are ignored.
So you can have a blank line and it, it, 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 Plus or Text Edit 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 don't, 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, if you, it looks good
to you in your text editor, you have an if,
and the block goes in, and 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 here is, you know, your, 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 Plus Plus, there is replace
tabs as 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.
Okay, 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.
Okay? 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, it's like if.
So, 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 it, you can also kind of tell these, 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, for is another
statement that ends in a 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 that 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, okay?
And now, at the end of this, 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, without,
with the comments hidden, it starts in column one.
Ignore, ignore, goes in, stays in, ignore, ignore,
ignore, comes out. So that's, 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, 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 gotta kind of start seeing that as well.
So this is a block that has to do with this green block
is the, 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 just 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.