An elif clause adds another condition to a Python conditional. Python checks conditions from top to bottom, runs the indented block for the first true condition, and skips the remaining elif clauses and else. section 4.3.3
Classifying ages
A program can select one message for each age range: ages 30–39 trigger “Binging Friends,” 20–29 trigger “Binging The Office,” and so on. The final else handles ages outside the listed ranges. Try boundary values such as 30, 39, 20, 19, 9, and values above 39. section 4.3.3
Use an elif chain when:
- A single decision has more than two possible outcomes. section 4.3.3
- Only the first matching branch should run. section 4.3.3
- You need to combine comparisons with and, such as an inclusive range. section 4.3.3
Listing 4.2 Function to determine the larger of two values
def larger(num1, num2):
if num1 > num2: #1
return num1 #2
else: #3
return num2 #4
Compare the conditional-chain listing with separate if statements to see why elif prevents later branches from being checked after a match. section 4.3.3


