You've learned how the if
statement allows your program to execute a block of code only when a specific condition evaluates to True
. But what if you have multiple possible conditions to check, or if you need a default action when the initial condition isn't met? This is where the elif
and else
clauses come into play, extending the if
statement to handle more complex decision-making scenarios.
elif
Imagine you want to check more than just one condition. For example, determining if a number is positive, negative, or zero requires checking multiple possibilities. The elif
statement, short for "else if," lets you test additional conditions only if the preceding if
(or elif
) conditions were False
.
You can chain multiple elif
statements after an initial if
. Python evaluates the conditions one by one from top to bottom. As soon as it finds a condition that is True
, it executes the code block associated with that condition and then skips the rest of the entire if-elif-else
structure.
Here's the general structure:
if condition_1:
# Block 1: Executes if condition_1 is True
statement(s)
elif condition_2:
# Block 2: Executes if condition_1 is False AND condition_2 is True
statement(s)
elif condition_3:
# Block 3: Executes if condition_1 AND condition_2 are False,
# AND condition_3 is True
statement(s)
# ... you can have more elif blocks
Let's modify our number example:
number = -10
if number > 0:
print("The number is positive.")
elif number < 0:
print("The number is negative.")
# If number is 0, neither condition above is True, so nothing happens yet.
In this case, if number
is -10:
number > 0
is False
, so the first block is skipped.number < 0
is True
, so the message "The number is negative." is printed. The program then skips any subsequent elif
or else
clauses in this structure.What if the number is 0? Neither number > 0
nor number < 0
is true. In the code above, nothing would be printed. To handle cases where none of the if
or elif
conditions are met, we use the else
clause.
else
The else
clause provides a default block of code that executes only when all preceding if
and elif
conditions in the structure have evaluated to False
. It acts as a catch-all. An else
clause, if used, must always come last, after all if
and elif
clauses, and it doesn't have a condition associated with it.
The complete structure looks like this:
if condition_1:
# Block 1: Executes if condition_1 is True
statement(s)
elif condition_2:
# Block 2: Executes if condition_1 is False AND condition_2 is True
statement(s)
# ... more elif blocks (optional) ...
else:
# Block N: Executes if ALL preceding conditions (condition_1, condition_2, ...) are False
statement(s)
Let's complete our number sign example using else
:
number = 0
print(f"Checking the number: {number}")
if number > 0:
print("The number is positive.")
elif number < 0:
print("The number is negative.")
else:
# This block runs only if number is not > 0 AND number is not < 0
print("The number is zero.")
print("Finished checking.")
If you run this with number = 0
:
number > 0
is False
.number < 0
is False
.else
block executes, printing "The number is zero."The following diagram illustrates the flow of control for this example:
Flowchart showing the decision process for classifying a number as positive, negative, or zero using if-elif-else.
It's important to remember two things about if-elif-else
structures:
Order Matters: The conditions are checked in the order they appear. If multiple conditions could potentially be true, only the block associated with the first true condition encountered will execute. Consider a grading example:
score = 85
if score >= 70: # This is True first
grade = 'C'
elif score >= 80: # This is also True, but checked later
grade = 'B'
else:
grade = 'Not C or B'
print(grade) # Output: C
Because score >= 70
is checked first and is true for 85, the grade is set to 'C', and the elif score >= 80
check is never reached. To get the correct grade, you should order the checks from most specific (highest score) to least specific (lowest score).
score = 85
if score >= 90:
grade = 'A'
elif score >= 80: # Checked second, True for 85
grade = 'B'
elif score >= 70: # Checked third
grade = 'C'
# ... other grades
else:
grade = 'F'
print(grade) # Output: B
Only One Block Executes: Within a single if-elif-...-else
chain, at most one block of code will be executed. Even if later elif
conditions might also be true, they are skipped once an earlier if
or elif
condition evaluates to True
. The else
block executes only if none of the conditions above it are true.
By combining if
, elif
, and else
, you can construct clear and effective logic to handle various alternatives within your programs, making them react appropriately to different inputs and situations.
© 2025 ApX Machine Learning