Elif statement in Python

The Elif statement in Python is an extension of the if statement where additional conditions are specified. If the if-statement condition is not met (false), the code passes to the elif statement condition. If these conditions are met (true), then the operations specified in the elif statement are performed.

What is an Elif statement in Python?

The Elif statement helps to solve problems when you have several different operations to be performed. Depending on which condition is met, a specific operation should be performed, but not all at the same time. To achieve this there is the else-if statement which is a combination of the if and else statement. The image below shows a flow chart that can be described as an “Elif ladder”. What the flow chart shows is that:

Elif statement Python
  • When If condition 1 is true then the If operation is performed and the program then proceeds without testing the remaining conditions.
  • When If condition 1 is false, then the program goes ahead and checks for If condition 2.
  • The program goes on until it comes to the condition that is true. If no condition is true, then the Else statement is executed.

The Elif statement is an extension of the if statement where additional conditions are specified

Important to note:

  • It is possible to use multiple Elif statements


  • If the logical expression in the if statement is true, then the elif statement (s) will not be executed


  • There must be an else statement when an elif statement has been created. This is so that the program can proceed if none of the if conditions are met.

Create an Elif statement in Python – Syntax

The Elif statement has similar syntax as the if statement. First, the reserved words elif are specified and followed by a logical expression. Within the brackets, you specify the operations to be performed if the conditions of the elif statement are met (true). Note that the if statement has higher priority than the elif statement. This means that when the conditions for the if statement are true, then the elif and the else statement will be ignored.


Note that an elif statement can only be created in conjunction with an if statement and must be followed by an else statement.

if (logical expression):
   statement executing

elif (logical expression):
   what this statement supposed to do

else (logical expression):
   what this statement supposed to do
  • the elif statement is easily declared by the reserved word elif


  • The logical expression indicates a condition that is either true or false


  • All operations indented after the statements will be executed if the statement is true

Example ElIf statement in Python

A store has the following discount system:

  • If the total purchase is greater than 500 €, you receive a 5% discount.
  • If the total purchase is greater than 1000 €, you will receive a 10% discount.
  • If the total purchase is greater than 1500 €, you get a 15% discount.
  • If the total purchase is greater than 2000 €, you will receive a 20% discount.


We now want to program a program that prints how much discount the customer should receive. We will now try to program the system using if-, else- and elif statements.


  • What the compiler does when the program is running is to first check the top if statement
  • When the if statement is true, all commands inside the brackets {} are performed
  • When the if statement is false, the compiler skips all instructions in the if statement and checks the elif statement
  • If the elif statement is true, all instructions are executed inside the elif statement’s brackets {}, otherwise you move on to the next elif statement.
  • If none of the conditions in the if or elif statements are met, the command is executed in the else statement! See the following code for the cash register:
cost = 400;    # The cost for the purchase is set to 400

if(cost >= 2000):                              
    print("The discount is 20%")      

elif(cost >= 1500):                         
    print("The discount is 15%")      

elif(cost >= 1000):                        
    print("The discount is 10%")     

elif(cost >= 500):                           
    print("The discount is 5%")

    print("No discount");

The printout in this case becomes


No discount

Since all conditions in the above if and else-if statements are false

For more technical and specific information about the Elif statements in Python we recommend the Python docs website