[vc_row css_animation=”” row_type=”row” use_row_as_full_screen_section=”no” type=”full_width” angled_section=”no” text_align=”left” background_image_as_pattern=”without_pattern”][vc_column][vc_column_text]

Elif statement in Python

[/vc_column_text][vc_separator type=”normal” up=”1″][vc_column_text css=”.vc_custom_1570783368496{background-color: #f6f6f6 !important;}”]

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.

[/vc_column_text][vc_empty_space height=”20px”][vc_column_text]

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:[/vc_column_text][vc_empty_space height=”20px”][vc_row_inner row_type=”row” type=”full_width” text_align=”left” css_animation=””][vc_column_inner width=”1/4″][/vc_column_inner][vc_column_inner width=”1/2″][vc_single_image image=”19442″ img_size=”full” alignment=”center” onclick=”img_link_large” img_link_target=”_blank” qode_css_animation=””][/vc_column_inner][vc_column_inner width=”1/4″][/vc_column_inner][/vc_row_inner][vc_empty_space height=”10px”][vc_row_inner row_type=”row” type=”full_width” text_align=”left” css_animation=””][vc_column_inner][vc_column_text]

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

[/vc_column_text][/vc_column_inner][/vc_row_inner][vc_empty_space height=”20px”][vc_column_text]

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

[/vc_column_text][vc_empty_space height=”20px”][vc_column_text]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.

[/vc_column_text][vc_empty_space height=”10px”][vc_column_text]

[/vc_column_text][vc_empty_space height=”10px”][vc_separator type=”normal” thickness=”2″][vc_empty_space height=”10px”][vc_column_text]

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.[/vc_column_text][vc_empty_space height=”20px”][vc_row_inner row_type=”row” type=”full_width” text_align=”left” css_animation=””][vc_column_inner width=”1/2″][vc_empty_space height=”20px”][vc_column_text]

if (logical expression):
   statement executing

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

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

[/vc_column_text][/vc_column_inner][vc_column_inner width=”1/2″][vc_column_text]

  • 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

[/vc_column_text][/vc_column_inner][/vc_row_inner][vc_empty_space height=”20px”][vc_column_text]

[/vc_column_text][vc_empty_space height=”10px”][vc_separator type=”normal” thickness=”2″][vc_empty_space height=”10px”][vc_column_text]

Example ElIf statement in Python

[/vc_column_text][vc_empty_space height=”10px”][vc_column_text]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:

[/vc_column_text][vc_empty_space height=”20px”][vc_column_text]

[/vc_column_text][vc_empty_space height=”30px”][/vc_column][/vc_row][vc_row css_animation=”” row_type=”row” use_row_as_full_screen_section=”no” type=”full_width” angled_section=”no” text_align=”left” background_image_as_pattern=”without_pattern”][vc_column][vc_column_text]

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

[/vc_column_text][vc_empty_space height=”10px”][vc_column_text]The printout in this case becomes

 

No discount

Since all conditions in the above if and else-if statements are false[/vc_column_text][vc_empty_space height=”20px”][vc_column_text]

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

[/vc_column_text][vc_empty_space height=”40px”][vc_separator type=”normal”][vc_empty_space height=”10px”][/vc_column][/vc_row][vc_row css_animation=”” row_type=”row” use_row_as_full_screen_section=”no” type=”full_width” angled_section=”no” text_align=”left” background_image_as_pattern=”without_pattern”][vc_column][vc_column_text]

Previous Page    |    Next Page

[/vc_column_text][vc_empty_space][/vc_column][/vc_row]