Drop links or images here to add them to the editor.

An if and an else give a program two branches: one for when the condition is true, one for everything else. Plenty of decisions have more than two possible answers, though. A weather app does not only know rain and not-rain, and a guessing game does not only know right and not-right.

For a third outcome, and a fourth, and a tenth, Python has elif.

elif is short for else if

elif goes between the if and the else, and it carries a condition of its own. You can add as many elif branches as you need.

Here is a weather program with three outcomes. The elif in the middle is the new part:

weather = "snow"

if weather == "rain":
    print("Take your umbrella")
elif weather == "snow":
    print("Take your scarf")
else:
    print("No special advice for you")
Take your scarf

weather does not hold "rain", so the if branch is skipped. Python moves on to the elif and checks its condition. weather does hold "snow", so that branch runs and the else is never reached.

Adding another elif

Four outcomes need two elif branches. Nothing else about the program changes:

weather = "sunny"

if weather == "rain":
    print("Take your umbrella")
elif weather == "snow":
    print("Take your scarf")
elif weather == "sunny":
    print("Take your sunglasses")
else:
    print("No special advice for you")
Take your sunglasses

Two conditions are false this time before a true one is found, so the program has to look at three branches to output one line.

How Python picks a branch

The rules are short, and every program in this chapter follows them:

  1. The conditions are checked from top to bottom, starting at the if.
  2. An elif condition is only checked when every condition above it was false.
  3. The first condition that is true wins. Its branch runs, and Python jumps straight past the rest of the chain.
  4. The else comes last and has no condition. It runs only when every condition above it was false.

Point 3 is the one that catches people out. A number can satisfy two conditions at once, but only the higher one in the chain ever gets to act on it. Ordering your conditions is part of writing the program.

Building a chain, step by step

  1. Start with an if, and set the first condition.
  2. Add as many elif branches as you need, and give each one a new condition.
  3. Finish with an else, which needs no condition.

The number of elif branches you need is two less than the number of outcomes: one outcome is covered by the if, and one by the else.

Coding tips

This program compares two numbers the user types in, and it has three outcomes: the first number is bigger, the second number is bigger, or they are equal.

print("Enter a number:")
num1 = int(input())
print("Enter another number:")
num2 = int(input())

if num1 > num2:
    print(str(num1) + " is bigger")
elif num1 < num2:
    print(str(num2) + " is bigger")
else:
    print("The numbers are the same")

Seven things to check every time you write a chain like this one:

  1. Start with if.
  2. Write a condition for your first outcome.
  3. Do not forget the colon at the end of the condition.
  4. Indent the code that should run when the condition is true.
  5. Add an elif, and write a condition for your second outcome.
  6. Keep adding elif branches with conditions until only one outcome is left.
  7. Add an else for that final outcome. No condition needed.

The if, the elif and the else all start at the left hand margin. Only the code inside a branch is indented, and that indentation is what tells Python which branch a line belongs to.

The same idea as a flowchart

If you draw the chain out, it is a row of decisions rather than a single one, shown here as a flowchart.

Start if condition True run the if code False first elif condition True run the first elif code False second elif condition True run the second elif code All conditions checked and false run the else code End

Reading it from the top:

Every route through that diagram ends in exactly one branch. However long the chain gets, a selection statement runs one branch and one branch only.

Try it yourself

Run the code below and type rain, then run it again with snow, sunny and something that is none of the three. Then add a fourth kind of weather with another elif, and check that your new branch runs.

print("What is the weather like?")
weather = input()

if weather == "rain":
    print("Take your umbrella")
elif weather == "snow":
    print("Take your scarf")
elif weather == "sunny":
    print("Take your sunglasses")
else:
    print("No special advice for you")
Help! My code doesn't work!

Make sure that you check for the following things:

  • Selection begins with if, has as many elif branches in the middle as you need, and ends with else.
  • There is a colon at the end of each if, elif and else line.
  • Every line inside a branch is indented, and all of them by the same amount. Never mix tabs and spaces in one file.
  • The if, elif and else lines themselves are not indented.
  • There is a condition after each if and each elif.
  • There is no condition after else.
  • You have used a double equals (==) in a condition that checks whether two pieces of data are the same. A single = stores data in a variable.