You can create a chain of if statements using the keywords elif and else. This is a lot easier to read than having to read ‘if if if’ all over again.

So using the keywords elif and else. That way, we can walk through all options for the condition. Imagine a weather app: if snow, elif rain, elif thunder, else sunny. We can execute different code for each condition.

Related Course: Complete Python Programming Course & Exercises

If statement

Recall that you can create a branch in your code using an if statement, which could be one single code line or a block of code.

if <condition>:
<run line of code>

if <condition>:
<line of code>
<line of code>
<line of code>

Instead of block of code, we say branch.

This creates a branch that is executed based on the condition. If the condition is True, the code is executed. If the condition is False, the code is skipped.

Each branch is indented with 4 spaces.

How to use “else condition”

So what if the <condition> is not True?

The program continues execution.

if <condition>:
<code>

<normal code>

If you want to add a False check, you can add else.

if <condition>:
<code>
else:
<code>

code

if <condition> is True it runs the first branch.
If <condition> is False, it jumps into the second branch.

Related Course: Complete Python Programming Course & Exercises

If elif and else

You can also do that if we have multiple conditions. Instead of doing this:

x = 3

if x < 5:
print('x smaller than 5')

if x >= 5:
print('x is too big')

You can connect the if statements with the keyword else. The example below shows how to do that.

x = 3

if x < 5:
print('x smaller than 5')
else:
print('x is too big')
It will make the decision based on the value of x, also known as the condition. You can run the program using the Python interpreter or in an IDE.

python if elif and else

In the above example there are two branches, each branch has a single line of code (but it can be multiple).

Because the value of x is 3 x = 3 it jumps into the first branch and then exists. If you change the value of x to be greater or equal to five, it will jump in the second branch.

After completion of the if statement it will continue executing the program in the normal flow.

When “else condition” does not work

It may be that the else doesn’t give the desired result. For instance, in this program:

x = 5

if x < 5:
print('x smaller than 5')
else:
print('x is larger than 5')

In this case the value of x is equal to five.

To correct this you can add an equality check (==) by adding the elif clause which we will talk about more in the next section.

>>> x = 5
>>> if x < 5:
... print('x smaller than five')
... elif x == 5:
... print('x equal to five')
... else:
... print('x is larger than five')
...

How to use “elif” condition

You can test multiple conditions by using elif.

  • First define the default condition
  • then add as many other test conditions you want by adding elif.

Each if code should be indentend with four spaces. The if codes body ends when the four spaces are removed again.

>>> x = 2
>>> if x > 10:
... print('x > 10')
... elif x > 5:
... print('x > 5')
... elif x > 0:
... print('x > 0')
...

This can be as long as you want. You can keep adding the conditions you need.

>>> if x > 10:
... print('greater than ten')
... elif x > 9:
... print('greater than nine')
... elif x > 8:
... print('greater than eight')
... elif x > 7:
... print('greater than seven')
... elif x > 6:
... print('greater than six')
... elif x > 5:
... print('greater than five')

Like the example above. You can optionally add another else to the end of the list.

Related Course: Complete Python Programming Course & Exercises

Switch statement

Instead of having a long list of elif, many programming languages allow you to use a switch statement.

For instance, in Java you can do this:

switch(argument) {
case 0:
return "x is zero";
case 1:
return "x is one";
case 2:
return "x is two ";
default:
return "nothing";
};

This is a lot cleaner than a long list of elif cases. But,

Python does not have a switch statement.

Instead you can use a dictionary.

Please note that this is a trick, and a normal if statement or elif cases are preferred.

The example below defines a function with a dictionary in it. By calling the function with a parameter, it simulates a switch statement.

>>>   
... def SwitchDemo(argument):
... switcher = {
... 0: "x is zero",
... 1: "x is one",
... 2: "x is two",
... 3: "x is three",
... 4: "x is four",
... }
... return switcher.get(argument, "")
...
>>> x = 2
>>> print(SwitchDemo(x))
x is two
>>>

This works, but does not let you check greater than >, smaller than <, greater or equal >= and other conditions.

In general it’s better to use if statements combined with elif and else.

Summary

In this article we talked about else and elif clauses, which test other for other conditions.

  • if tests the default condition (is True?)

  • Each branch must have 4 spaces

  • code is only executed if a condition is True

  • else tests if the default condition(s) are False

  • elif tests multiple conditions

  • Python does not have a switch statement, but it can be simulated

If you are a Python beginner, then I highly recommend this book.

Download exercises