
Syntax: Elif Statements
Creating secondary conditions with Python elif statements.
Python elif Conditionals
Sometimes you want to create a conditional statement where more than one condition can be true, but you want to run different blocks of code depending on what specific condition is true. In those cases you want to use an elif statement.
An elif statement can only follow an if statement, which means the either the if code or the elif code will run, but not both. Any code afterwards that isn't indented will always run.
Multiple elif statements can be used together for checking more than two conditional statements.
If desired, an else statement can be added to the end of all of the elif statements to run a code block that should run only if none of the conditional statements evaluate to True. else statements are completely optional.
Note the example below where a range of numbers are checked:
age = 22
if age < 16:
print("You can't drive a car.")
elif age < 18:
print("You can't sign a contract.")
elif age < 25:
print("Your car insurance rates are high!")
else:
print("You are getting to the perks!")
print("We all go through these stages.")
> "Your car insurance rates are high!"
> "We all go through these stages."
Notice in the above example that because the value of age is 22 the print statements indented beneath the checks for less than 16 and less than 18 do not show up. Also, the code is the else block does not print.
Its important to realize that once a conditional statement in a block of if and elif statements evaluates to True, no other conditional statements are evaluated.
Let's modify the value of age in another example and see the result:
age = 17
if age < 16:
print("You can't drive a car.")
elif age < 18:
print("You can't sign a contract.")
elif age < 25:
print("Your car insurance rates are high!")
else:
print("You are getting to the perks!")
print("We all go through these stages.")
> "You can't sign a contract."
> "We all go through these stages."
Note that this time, because the value of age is 17, the if statement for less than 16 evaluates to False, then the elif statement for less than 18 evaluates to True, the the indented code for that conditional statement runs and prints the string.
Notice also that the less than 25 check and the else statement are not evaluated and only the print statement after the else statement runs.
To summarize, an elif statement is very useful when more than one conditional statement needs to be grouped together so that only one of the group can evaluate to True at any one time.
- Note: If you want to check for multiple conditions individually because you more than one of them can evaluate to
Trueat the same time, use multiple separateifstatements instead of anif/elifblock.
Practice: if-elif-else Conditionals
Based on the information above. Complete the following steps:
-
Create a variable named
ageand assign it a numeric value that represents your age. -
Create an
if/elifblock provides checks for the following conditions (Make sure to add an appropriate indented print message for each check):- less than 20
- less than 30
- less than 40
- less than 50
-
Add an
elsestatement with an appropriate print message. -
Confirm that the indented print statement prints correctly for your individual age.
HINT*: Click here for a hint
> You might need a medical procedure soon!
SOLUTION*: Click here for an example solution
age = 41 if age < 20: print("You might know all there is to know.") elif age < 30: print("You might realize you don't quite know everything.") elif age < 40: print("You might realize there is a lot to learn!") elif age < 50: print("You might need a medical procedure soon!") else: print("You made it to at least half a century!") -
Next, continue to repeat the above steps and alternate the value of age until you get each message to print correctly.
When the above is completed and you are getting the proper output, you are ready to move onto the next exercise!