8.9 Chained conditionals
From Foundations of Python ProgrammingGNU FDL 1.3Original pagePython1 class period3 exercises4 graded tests
8.9 Chained conditionals
Read first in the book: 8.7 Omitting the else Clause: Unary Selection, 8.8 Nested conditionals.
Python provides an alternative way to write nested selection such as the one shown in the previous section. This is sometimes referred to as a chained conditional.
if x < y:
print("x is less than y")
elif x > y:
print("x is greater than y")
else:
print("x and y must be equal")
The flow of control can be drawn in a different orientation but the resulting pattern is identical to the one shown above.

elif is an abbreviation of else if. Again, exactly one branch will be
executed. There is no limit of the number of elif statements but only a
single (and optional) final else statement is allowed and it must be the last
branch in the statement.

Each condition is checked in order. If the first is false, the next is checked, and so on. If one of them is true, the corresponding branch executes, and the statement ends. Even if more than one condition is true, only the first true branch executes.
Here is the same program using elif.
x = 10
y = 10
if x < y:
print("x is less than y")
elif x > y:
print("x is greater than y")
else:
print("x and y must be equal")
The following image highlights different kinds of valid conditionals that can be used. Though there are other versions of conditionals that Python can understand (imagine an if statement with twenty elif statements), those other versions must follow the same order as seen below.

Check your understanding
Check your understanding: a multiple-choice question in the book.
Check your understanding: a multiple-choice question in the book.
Exercise 1
Create one conditional to find whether "false" is in string str1. If so, assign variable output the string "False. You aren't you?". Check to see if "true" is in string str1 and if it is then assign "True! You are you!" to the variable output. If neither are in str1, assign "Neither true nor false!" to output.
Open the
01folder and editmain.py. Run Tests checks it.
Exercise 2
Create an empty list called resps. Using the list percent_rain, for each percent, if it is above 90, add the string 'Bring an umbrella.' to resps, otherwise if it is above 80, add the string 'Good for the flowers?' to resps, otherwise if it is above 50, add the string 'Watch out for clouds!' to resps, otherwise, add the string 'Nice day!' to resps. Note: if you're sure you've got the problem right but it doesn't pass, then check that you've matched up the strings exactly.
Open the
02folder and editmain.py. Run Tests checks it.
Exercise 3
We have created conditionals for you to use. Do not change the provided conditional statements. Find an integer value for x that will cause output to hold the values True and None. (Drawing diagrams or flow charts for yourself may help!)
Open the
03folder and editmain.py. Run Tests checks it.
For teachers
8.9 Chained conditionals
Students read Foundations of Python Programming's “Chained conditionals” in CS Room and work through its 3 Python coding exercises (3 graded).
- Time: 1 class period
- Tests: 4 in 3 graded exercises, set up for you when you import the lesson
Reading first
Exercises
| Folder | Grading | Tests |
|---|---|---|
01 | Autograded | 1 |
02 | Autograded | 1 |
03 | Autograded | 2 |