Python IF-statement vs ELIF-statement

I had a moment of clarity recently about the functional difference between the Python IF-statement and ELIF-statement. I’ve used these numerous times before to accomplish what I wanted, but this was one of those moments where you learn something you thought you already knew.

Functional Summary

During execution, an IF-statement tests that the defined conditions are met; if met, the code under the IF-statement is triggered. Take note that if you have multiple IF-statements modifying the same variable, you need to keep in mind that multiple statements might execute and could modify the same variable unexpectedly.

Here is where the ELIF-statement differs:

The elif, also known as else if in other languages, still tests whether the defined conditions are met, but as soon as the first ELIF-statement that meets the defined conditions is found, it executes that block and NO other ELIF-statement conditions run — or are even evaluated — because one of the ELIF-statements was found to be True. Meaning, if you have three ELIF-statements and the first one executes, the other two are ignored.

To summarize: IF-statements go through every single statement to see if the criteria is met, but ELIF-statements stop at the first instance where the criteria is met and exit the block.

Note: This behavior can make an enormous impact on how your software logic works, so keep this in mind when designing your software.

IF-ELSE Statement Example

To start things out let’s look at one of the most basic forms of an IF-statement, the IF-ELSE statement. The below code checks to see if you exceed the 70mph speed limit — if you do it will print “Gotcha!”, otherwise it will print “Nothing to see here”.

speed_limit = 70

if speed_limit > 70:
    result = "Gotcha!"
    print(result)
else:
    result = "Nothing to see here"
    print(result)

Ok, now let’s get to some slightly more complex examples.

IF-statement Examples

This example has a speed_limit set to 70mph. The IF-statements check to see if you are under, meet, or exceed the speed limit and print a result.

speed_limit = 70

if speed_limit > 70:
    result = "Gotcha!"
    print(result)
if speed_limit < 70:
    result = "Nothing to see here"
    print(result)
if speed_limit == 70:
    result = "You like to push the limits"
    print(result)
if speed_limit == 70:
    result = "Just double checking"
    print(result)

Which will print the following to the terminal:

You like to push the limits
Just double checking

Both IF-statements that check whether speed_limit equals 70mph execute. Now let’s see what happens when multiple IF-statements modify the same variable and the result only prints at the end. The last statement to trigger will be the value set to result.

speed_limit = 70

if speed_limit > 70:
    result = "Gotcha!"
if speed_limit < 70:
    result = "Nothing to see here"
if speed_limit == 70:
    result = "You like to push the limits"
if speed_limit == 70:
    result = "Just double checking"
print(result)

Which will print the following to the terminal:

Just double checking

The above only prints the last IF-statement value, so you better be sure this is the behavior you want when designing your logic.

ELIF-statement Example

This example has a speed_limit set to 70mph. The IF and ELIF-statements check to see if you are under, meet, or exceed the speed limit and print a single result.

speed_limit = 70

if speed_limit > 70:
    result = "Gotcha!"
    print(result)
elif speed_limit < 70:
    result = "Nothing to see here"
    print(result)
elif speed_limit == 70:
    result = "You like to push the limits"
    print(result)
elif speed_limit == 70:
    result = "Just double checking"
    print(result)

Which will print the following to the terminal:

You like to push the limits

Only the first condition that evaluates true executes, and once complete the IF-ELIF block exits. This is why the second ELIF-statement is NOT printed to the terminal.