"else" and "elif" Clauses
After meeting the if
-statement, in this video you’ll have a look at the else
and elif
clauses and how they work together.
00:00
All right. Section 2: else
and elif
Clauses. Let’s get going! So far, we’ve covered the if
statement. So, we have something happening if our expression evaluates to True
.
00:13
But what if we want something to happen on a False
or something to happen by default? That’s where we’ll use the else
statement in conjunction with our if
statement. Additionally, if there are other conditions that we want to assess, we can use elif
, or else if, to do that.
00:30
Let’s take a look at the syntax for those. So, you already know the if
syntax. else
is just tacked on and given another statement.
00:38
So, if the expression evaluates to True
, run the first statement, else run the second statement. And, like I said, if you want to evaluate multiple conditions, you have else if, shortened to elif
in Python, to be able to evaluate for multiple conditions and run a statement if one of those conditions exist. In this syntax example, our program will run through each of these clauses and evaluate it for True
or False
. As soon as it finds a True
one, it will run the statement.
01:10
This is known as short circuiting. So, if our second elif
statement runs in this example, none of the additional expressions will be assessed and none of the additional statements will be run.
01:23 Let’s jump back into the console and check out an example. Okay, here I am in PyCharm, my development environment. I am working on a blank Python file here because we’re going to make, kind of, a mini program.
01:36 You should open up a blank Python file for yourself. And let’s put this program together. I’ve been talking a lot about eating during these examples, so let’s put together a calorie counter app.
01:50
We need to start with a few variables. Let’s call it cals_consumed
(calories consumed). Let’s say 1500
. cals_in_item
(calories in the item) we want to eat.
02:02
And then total_cals
will be the sum of those two.
02:12
First, let’s print the total_cals
we got
02:20
with an f-string. Now, we can set up our if
statement. So let’s say if total_cals
is greater than or equal to 1600
, don’t eat it!
02:44 Okay. And let’s do a condition for if we haven’t eaten enough today, let’s say we’ve eaten less than 300 calories.
02:59
Print that we want to eat more, "Eat a larger portion."
And we’ll use else
for anything in between those two conditions, 300
calories or 1600
calories. And we’ll just say "Sure you can eat it!"
And that should be it! Let’s run this. Actually, let’s review here.
03:27
cals_consumed = 1500
, cals_in_item = 100
, we sum those, print them out, and then we run it through our if
statement.
03:40
Total Calories Today: 1600
, Don't eat it!
What happens if we get under that 300 calorie mark? So, let’s say we’ve only eaten 150 calories and our item’s 100 calories. Let’s run this now.
RMS on April 22, 2020
@km An expression evaluates to a value A statement performs some actions
Become a Member to join the conversation.
km on Jan. 1, 2020
Hello Sir - What is the difference between expression and a statement?