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