Elif statement in Python: complement the if statement and the else statement


Keywords: if statement, conditional statement, else statement, elif statement

if else statement 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.

  • A conditional statement that determines the actions the program should execute based on a logical condition
  • Similar to the if statement has a logical condition that is true or false
  • Must be used in conjunction with an if statement

How elif statement in Python works?

The image below shows a flow chart that can be described as an “Elif ladder”.

Elif Statement Python Programming

What the flow chart shows is that:

  • 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.

How to create elif statement in Python?

The Elif statement has similar syntax as the if statement.

  • 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

Syntax: Declare elif statement in Python

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 elif statement should perform

else (logical expression):
   what else statement should do

Example: Elif statement in Python

A store has the following discount system:

  • When 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.
  • Finally, say that 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.

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%")

else:                                           
    print("No discount");
  • 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 indented 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, 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:

The printout in this case becomes

No discount

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

Summary: Elif statement in Python

In summary

  • The elif statement is used in conjunction with the if statement and the else statement
  • An elif statement is used to test additional conditions, and if true, execute a specified code block
  • If the if statements condition is true, the else if statement conditions will not be evaluated, as it has a lower priority
  • The elif statement is declared with the reserved words else if followed by a logical expression in parentheses ( ). The logical expression evaluates a condition that is either true or false
  • For more technical and specific information about the Elif statements in Python we recommend the Python docs website

Syntax: Declare elif statement in Python

if (logical expression):
   statement executing

elif (logical expression):
   what elif statement should perform

else (logical expression):
   what else statement should do