Python elif

Python elif

The elif statement is used to check multiple conditions and execute the specific block of statements depending upon the true condition among them.

Syntax

The elif statement can be optional such as else. When the if expression is true, the statement of expression 1 will be printed otherwise control moves to elif expression, if that elif condition evaluates false then control transfers to next elif. It will check all elif expression until it gets a true expression.

Note: Python does not provide a switch or case statement as other programming languages

Flow diagram

Python elif Flow Diagram

 

In the above flow diagram, Python evaluates each expression (i.e., the condition) one by one and if a true condition is found the statement block under that expression will be executed. If the false condition is detected, then statement block under else condition will be executed. In the following example, we have applied if, series of elif, and else to get the type of a variable.

1. Write a program to check the grade of the student

Output:

2. Write a program to check whether its morning, noon, or night.

Output:

3. Write a program of the simple calculator

Output:

4. Write a program to check the type of triangle.

Output: