(upbeat music)
- One of the fairly common scenarios you'll run
into in programming is making decisions.
Your program needs to compare values
and execute code based upon some logical outcome.
And that's where conditional logic comes into play.
In Python, conditionals are handled by the if statement.
And that's what we're going to look at now.
And you can see here in the main function of this example,
I've got two variables, x and y,
and they are 10 and 100 to start with.
Let's add our first conditional.
All right, if (x < y):
st = "x is less than y"
And then down here,
I'll write print (st).
All right. This code is comparing x to y.
And if x is less than y,
then the value of the st variable
will be the string x is less than y,
and then we print the result.
So let's go ahead and run this.
And you can see, sure enough,
the program prints "x is less than y."
So let's see what happens
if I change the value of x to be 1000.
I'll save it.
And I'll run it again.
And you can see I'm getting an error
because the st variable was referenced
before it got assigned,
which means that this line
of code right here never got executed.
Let's go ahead and fix this, right?
The assignment didn't happen
because x is not less than y anymore.
Now, it's greater than y.
Let's go ahead and make that change.
So I'll add else: st = "x is greater than y"
So now, we have the if condition
and we have the else condition.
When the if condition is true,
then this block executes
and the other block executes in all other cases.
So let's run this again.
And sure enough, now we can see
that x is greater than y.
So now, let's try making these two variables exactly equal
to each other.
So we'll save.
And I'll run.
And sure enough, it says,
"Well, x is greater than y."
That's not really true, though. Right?
They're the same.
The problem is that the condition here
is evaluating to false.
Therefore, this condition is evaluating to true
even though it doesn't check to make sure
that x is actually greater than y.
So let's make one more change to fix that problem.
What I'll do is in here,
I'll write elif,
and the elif will be (x == y):
then st will be "x is the same as y"
All right. So now we have if, elif, and else.
And this gives us a way
of chaining more than one condition together.
Now, if you're used to using a language like JavaScript,
you're probably used to seeing this written as if
and then else if
and then else if and so on.
Python just takes if and else
and combines them into the one operator, elif.
All right. So let's run this.
And sure enough, we get the correct output
that x is the same as y.
Now, you might be wondering
if there is the equivalent of a switch case in Python.
Some other languages have this notion
of a compound conditional known as switching case.
And the answer is no.
Python, in its efforts to be simple,
sticks to the if else construct
and uses elif as a substitute
for writing a switch case block.
So if you're familiar with other programming languages
and wondering if there's the equivalent of that,
then in Python,
as of right now, there's not.
So there's one more thing to look at before we move on.
And that's an additional way
of writing conditional statements.
Sometimes, you'll run into situations where you have
just an if and an else.
So you've got four lines or whatever intended
to do a simple comparison.
Now, Python has a construct called a conditional statement.
The conditional statement let's
you write a common if else construct all in one line.
And it's a more concise way of writing the comparison logic.
Let's add the statement and you'll see what I mean.
So down here,
I'll write st equals
and then "x is less than y."
And then I'll write if (x<y)
else "x is greater than or the same as y."
So this code does the work of an if else construct
but it uses just one line.
And JavaScript has something similar
with the ternary operator,
if you're already familiar with that.
Let's go ahead and put the print statement.
We'll copy that and put that down here as well.
So I'll run this.
And you can see that the result
is x is greater than or the same as y.
And if I change it to 10 and run it again,
then we're getting the x is less than y function.
So it's a nice, concise way
of writing a conditional statement
instead of having to write out a more verbose if else block.
(upbeat music)