>> Hello everyone and welcome back to
Intro to Python here on Cybrary on-demand.
This is going to be lesson one of
our second module and in this lesson,
we're going to be focusing on if, elif,
and else as they apply to
the discipline of Python programming.
In this lesson, we've got two primary objectives.
We're going to learn how to apply Boolean logic,
how to apply truth and falsehood to Python,
and then we're going to learn how to use
a properly formatted if statement in Python.
Now for those who've watched the other videos,
you may notice a difference here,
which is that my face is no longer in
the bottom right of your screen.
There are two excellent reasons for that.
One, it actually takes
a shockingly long time to do
my hair in the morning and two,
a little bit more seriously,
because we're doing
some actual on-screen programming right now,
we want to make sure we have all the screen real estate
we possibly can so I'm not going to
be onscreen very much and we're just going to be
focusing on the actual code on the screen.
Slightly different format. Hopefully,
it doesn't trip you up.
If you really need to see my face on this,
you can Photoshop it all and feel better.
But if you really need to see
my face on the videos I'm making, I guess.
Hi, mom. I'm glad you're proud of me.
That's probably about the only person.
Like I mentioned in this video,
we're going to be focusing on if,
elif, and else.
If, elif, else is a paradigm that we actually learned
in the last module where
we were talking about the concept of flow control,
which you may remember is
an execution decision based upon some internal logic.
We're going to crack into our Vim here.
Again, we got an Ubuntu 64-bit Vim.
I'm working in Terminator right now,
and we're just going to actually
start out working in the interpreter.
We're not even going to start with a script.
When we're using an if statement,
what we're trying to do is we're trying to
evaluate that internal logic.
We're trying to set some important value,
we're trying to make some decision based upon it.
We're going to set a couple of initial values
so that we have something to work with.
[NOISE]
We have three initial values set,
x equals TestString, y equals 10,
and z equals negative 15.
Now the way we're going to structure this if statement,
there's no parentheses, wrong language,
is by writing the word if all lowercase.
In Python that's a keyword.
It's not a function or anything like that.
It's just a keyword. Excuse me, hiccup there.
We've got our if statement and then we're going to
apply conditional statement,
and these here in these brackets is
just a standard value, a fake variable.
We're actually going to replace it with if
y is greater than two.
Now, remember, because
this statement is subordinate to our if,
this is a subordinate clause to our if statement,
we're going to tab in or four spaces,
whatever your style is,
so long as it's consistent, focus on that.
If y is greater than two,
print y minus 2.
In our interpreter, you may have noticed
it still hasn't printed anything out.
The REPL hasn't actually
performed a print and that's because of the fact that
if statements and other statements that have
subordinate code actually give
the interpreter context and say
it's not actually time to execute yet.
Get the rest of the code, get the rest of
the context and then perform the operation.
Here you can see that it prints the number 8,
which is y minus 2.
Fantastic. Now we can do, for example,
if y is greater than z, print y.
But if we want to handle the case where
perhaps y is less than z,
that's where we apply the elif that we learned about.
Now, I'll remind you that elif is else if.
It is an if statement that only
potentially evaluates if the
preceding if statement turns out to be false.
If z is greater than y, print z.
What's great here is because we have
a bimodal situation where it is probably,
actually that's not entirely great.
But in general, when you have bimodal situations,
you don't necessarily have to have an else.
We have to have one here because of the fact that
it is possible for y to be greater than z,
z to be greater than y, or z and y to be equal.
[NOISE] We run that
and we see of course that 10 is greater than negative 15.
But that code gives a great demonstration of
the three components of an if statement.
You have if with your conditional evaluation.
You have the subordinate code that's tabbed in.
You have else if or elif
with the second possible condition.
Then you have that subordinate code tabbed in.
Then you have your else statement.
Again, your else statement is,
if none of the preceding if statements evaluated to true,
if all of the preceding
conditionals turned out to be false,
then just execute this code.
Now, this actually right here
is a simple implementation of what's
called a max function or
a function to find the larger number,
and one of your exercises in the lab
that's going to be coming up about half-time
of this module is actually going to
be creating this code.
It's worth looking at and
understanding exactly how that works.
Now we're going to step out of our interpreter
here for just a minute.
We're actually going to write ourselves
a little bit of a script.
Vim and we're going to do if_then.py.
[NOISE] We're going to put our shebang line in here.
There's also something else that I want to
mention that actually I did not
bring up in the syntax video
because we didn't have a good cause for it.
But Python does have the option of
commenting or writing things
into your script that don't get executed or interpreted.
The way that's done is by using
this pound sign hashtag, whatever you want to call it,
and with that you can just write,
this is a Python comment.
You can see that if you were to write it by
itself in Vim because I have context on it.
It's going to give it different colors.
But that's how you can put comments in your code.
Remember when we talked about
the design philosophy of Python,
it's very important to write readable code.
If you're going to create what
may have to be a complex function,
it's important to have your comments in there so that
people understand what that function is supposed to do.
This first function, we're going to evaluate two strings.
The first string is going to be
s1 and the second string is going to be s2.
What we're going to do here is if s1 equals s2.
You can see that I have auto-indent and I have
some contexts built into my Vim so it tabbed for me.
If s1 equals s2 or if s1 is the same as s2,
print s1 else print s1 plus s2.
We mostly separate out of the interpreter
there just so that you could see
writing it into a script
and so you can get a little familiar with that.
Again, we're going to give
these permissions to allow it to execute.
[NOISE] There you go.
You can see that it printed those two lines out together
because again, the conditional evaluated.
Now over to the side you will see
a document that says if assignment.
That's just going to be a whole bunch of
example if statements and you can feel free
to peruse that and see if you can
figure those out and write some code yourself.
In the lab that is going to be at
the end or at the midpoint of Module 2.
We're going to examine if statements as well
as the other lessons that are coming down the road.
If you want to get more practice with those,
feel free to crack that open and start in on those
first and then come back and watch
the next videos before you get to that lab.
That's going to be the end of this lesson.
In this lesson again,
we talked about how Boolean logic applies to Python,
the idea of evaluating truth with an if statement,
and then we learned how to
properly format an if statement.
As always, I'm your instructor
Joe Perry and I'm really glad to have you all here.
I look forward to having you in our next lesson,
which is going to be discussing for-loops in Python.
If you have any questions of course,
please feel free to reach out to me or one
of your course mentors if you have them.
Thank you as always for watching this,
Intro to Python on Cybrary on-demand.