1.4 If Statements - 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 intro to
00:00
Python here on Cybrary on-demand.
00:00
I as always, I'm your instructor
00:00
Joe Perry and in this lesson today,
00:00
we're going to be learning about if statements.
00:00
In Lesson 3, programming basics.
00:00
Lesson 3, our objectives are going to be;
00:00
Understand and define the term flow control,
00:00
understand and apply if statements,
00:00
and learn the components of if statements.
00:00
Note this has a slightly Python bent.
00:00
The inclusion of ellipse,
00:00
which we'll talk about in a minute,
00:00
is something that Python uses that
00:00
not all other programming languages use.
00:00
But this isn't going to be actual Python code yet.
00:00
It's just going to be bent toward Python.
00:00
All flow control actually is,
00:00
is execution decisions made
00:00
based upon some internal logic.
00:00
I want to say that again, and I'm going to say it
00:00
again throughout this course.
00:00
It is an execution decision
00:00
made based upon some internal logic.
00:00
A program, for example, might perform
00:00
different operations based on the keys you press.
00:00
Almost all programs do.
00:00
A Word processor is going to print the letter
00:00
of the key you press or the symbol.
00:00
If you use Shift, it'll print
00:00
a capital. Things like that.
00:00
That's a great example of
00:00
flow control because it's not something
00:00
people generally think of as flow control.
00:00
Flow control, it's
00:00
a weird phrase to keep saying over and over again,
00:00
but it's something that people often
00:00
get wrapped around the axle with,
00:00
get a little bit confused,
00:00
don't really understand, and really,
00:00
it's because it's being over-complicated.
00:00
The simple fact of the matter is that any decision
00:00
your program makes based on some internal logic,
00:00
some evaluation of truth or falsehood,
00:00
that's all flow control.
00:00
That's all it is. Don't get too far into the weeds on it.
00:00
Just understand that it is
00:00
a decision your program is making.
00:00
If statement specifically in this lesson,
00:00
going back to the Boolean logic we
00:00
talked about in our earlier lesson,
00:00
if is implied in all of those statements.
00:00
All of those statements can be framed and
00:00
generally are framed as if then.
00:00
If A equals one and B equals one,
00:00
then A and B equals one or if A equals one,
00:00
or B equals one,
00:00
then A or B equals one.
00:00
It's really easy to understand,
00:00
or it's not really easy to understand,
00:00
that's not a fair thing to say.
00:00
It's very straightforward.
00:00
All you're doing with if,
00:00
is you're just using it to evaluate a condition.
00:00
You'll often hear this referred to
00:00
as a conditional and in
00:00
the help documentation that we provide,
00:00
it is referred to as a conditional statement.
00:00
Some examples of if from everyday life,
00:00
if it is raining, wear a raincoat.
00:00
If the subway is late,
00:00
yell at the sign.
00:00
That doesn't help but if you've
00:00
never lived in Washington DC,
00:00
I don't know how to tell you how much better it makes you
00:00
feel to shout at a subway sign,
00:00
this has done nothing wrong.
00:00
But it gets out a little bit of
00:00
your aggression and in DC, that's very important.
00:00
If the number X is divisible by 3,
00:00
print yes to the screen.
00:00
That's a more programmatic example.
00:00
That's something that's going to often show up
00:00
in different programs,
00:00
different tools, having menus that are
00:00
based on a control number that is actually a number,
00:00
an integer of some kind.
00:00
That's a very common paradigm used to control a program.
00:00
You'll see the graphic above me is a flowchart.
00:00
Flowcharts are a great way of
00:00
learning to understand if statements,
00:00
I'm aware that's probably pretty hard to read.
00:00
That's okay. It's actually just an xkcd comic.
00:00
I mentioned before and I'll mention again,
00:00
you're going to see a lot of those in this series,
00:00
and then basically everything I do
00:00
because xkcd is glorious.
00:00
Elif, the next statement is part of the if paradigm,
00:00
where we have, if, Elif, and else.
00:00
Elif is a little bit less
00:00
common like I mentioned it's used in Python,
00:00
it's used in some other languages,
00:00
but it's not used in all of them.
00:00
The way Elif works is it is
00:00
actually its own if statement.
00:00
It's a hybrid of else and if.
00:00
If the first condition,
00:00
so whatever your if statement was,
00:00
evaluates to false,
00:00
then consider this condition and evaluate it.
00:00
Which is to say that, for example,
00:00
for the menu example I gave a moment ago,
00:00
if the number is one,
00:00
add, say it's a calculator.
00:00
If the number is one, perform the add operation.
00:00
If Elif the number is two,
00:00
perform the subtract operation.
00:00
Elif the number is three, so on and so forth.
00:00
The idea here is just that
00:00
Elif is an if statement that only
00:00
gets checked in the case that the
00:00
preceding if statement was,
00:00
or the preceding Elif statement was false.
00:00
Now you can have many Elif statements in a row.
00:00
There's not really a serious upper limit on it,
00:00
though generally speaking, you're going to want to avoid
00:00
too many of them just for code readability.
00:00
The final part of this paradigm is the else statement.
00:00
Else only ever exists in the context of an if,
00:00
Elif, else statement.
00:00
It exists also for a type of loop,
00:00
but we'll talk about that later.
00:00
The point is it only exists in
00:00
the context of evaluation of
00:00
a truth or falsehood determination of flow control.
00:00
If the previous evaluation was false,
00:00
then perform this operation.
00:00
Now unlike Elif,
00:00
there is no new conditional being evaluated with else.
00:00
It's simply, if A perform this behavior,
00:00
else in all other cases perform a different behavior.
00:00
That's really all there is to if, Elif, and else.
00:00
But we're going to take a couple of examples here and
00:00
make sure that we understand and can break this down.
00:00
Here are the facts of the situation.
00:00
X is a number.
00:00
If that number is less than 10,
00:00
you will print small.
00:00
Elif that number is greater than 100,
00:00
you will print large,
00:00
else you will print medium.
00:00
We're going to try it out with a few examples.
00:00
X in this first case,
00:00
we're going to set equal to 999.
00:00
Now, if that number is less than 10 if
00:00
is going to be the first statement we evaluate always.
00:00
In most programs within certain contexts,
00:00
your statements are going to be
00:00
evaluated from top to bottom.
00:00
Functions will mess with that a little bit.
00:00
We'll talk about why and how later.
00:00
But generally speaking, you're going to
00:00
evaluate from top to bottom.
00:00
But even in cases where that might
00:00
be messed up for some reason,
00:00
you've got complex go-tos or whatever, if, Elif,
00:00
and else will always be
00:00
evaluated in the order of if, Elif,
00:00
and else and if you don't have an if,
00:00
you can't have an Elif or an else.
00:00
If that number is less than 10, print small.
00:00
Well, 999, of course, is larger than 10,
00:00
so we're not going to print small
00:00
that is evaluated to false,
00:00
therefore we are going to
00:00
>> consider the second statement.
00:00
>> Elif that number is greater than 100, print large.
00:00
Well, the last time I checked,999 is larger than 100,
00:00
therefore, we print large.
00:00
Now, what happens if X equals 15?
00:00
Well, if that number is less than 10,
00:00
we print small, 15 is still larger than 10.
00:00
Elif that number is greater than 100, print large.
00:00
It's smaller than 100,
00:00
so we can't perform that operation either.
00:00
Else print medium.
00:00
Now again, there's no new evaluation when I'm saying
00:00
if it's some set of numbers,
00:00
if it's within some range, simply if neither of
00:00
the previous two conditions are met, just print medium.
00:00
We have X equals one.
00:00
If the number is less than 10, print small.
00:00
Of course, that's going to be our first evaluation
00:00
because that's the first line we check,
00:00
and sure enough, it is small.
00:00
That's all there is for Lesson 3.
00:00
In this lesson, again we talked
00:00
about the concept of flow control,
00:00
which is an execution decision
00:00
based upon some internal logic.
00:00
Then we went ahead and we talked about
00:00
if statements, if, Elif,
00:00
and else and we understood the way that
00:00
those three statements fit together and how they work.
00:00
Join me in the next lesson,
00:00
which is going to be discussing for loops.
00:00
That will be Lesson 4 worked
00:00
out. Did not mean to do that.
00:00
I actually did not realize that that worked
00:00
out that way until just now.
00:00
I have been recording this course for
00:00
two weeks and I just realized the unintentional pump.
00:00
Anyway, I'll see you back in Lesson 4, for loops.
00:00
As always, I'm your instructor Joe Perry and
00:00
thank you for joining us on Cybrary on-demand.
Up Next