2.1 If/Elif/Else - IP

Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
or

Already have an account? Sign In »

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