Table of Contents
Remaining conditional statements in python
Python programming consists of various types of decision making statements.
Conditional statements in python:
- if statement
- if-else statement
- if-elif-else statement
- if-elif-elif-else statement(multiple elif)
- Nested if statement
if-elif-elif-else statement (multiple elif)
We use elif block for multipath decision making. This conditional statements in python allows us to mention condition similar to if statement. In this is we have many separate blocks of code and one of them get executed based of certain condition. We have three block starting with if block, elif block and else block. First if block will check if mentioned condition is true or not, if the condition is true then if block will get executed and if the condition is false then pointer shifts to very next elif block. elif block will check for its condition, if the condition is true then elif block will get executed and if the condition is false then pointer shifts to very next elif block. And this goes on until one condition get satisfied or last block i.e. else block will get executed.
In simple words if block and elif blocks will check for certain condition one by one and who ever’s condition get satisfied first then respective block is executed, and if no condition is satisfied then last block i.e. else block will get executed.
We cannot use elif block and else block without if block.
Like while loop if block and elif block also accepts boolean value which means if we enter 0 as condition then it will be considered as False and non-zero value will be considered as True.

Syntax
Here is the simple syntax for if…elif…else conditional statements in python−
if (condition): code block if condition is True elif (condition 2): code block if condition 2 is True
elif (condition 3):
code block if condition 3 is True
.
.
.
. else: code block if both conditions are False
Example 1
""" Author : ITVoyagers (itvoyagers.in) Date :9th August 2020 Description : Program to show use of if...elif...elif...else statement """ deposit=int(input("Enter a number")) if deposit < 10000: interest=deposit * 0.10 print("interest is",interest) elif deposit < 15000: interest=deposit * 0.20 print("interest is",interest) elif deposit < 25000: interest=deposit * 0.25 print("interest is",interest) else: interest=deposit * 0.30 print("interest is",interest)
OUTPUT
Nested if statement
When we write if statement inside on another if statement is known as nested if-else.
We can nest if statements as many as we want. We can also write if statement is else block of if-else statement.
Indentation is the only way to figure out the level of nesting. They can get confusing, so they must be avoided unless necessary.
Syntax
Here is the simple syntax for if…elif…else statement −
if condition1: if condition2: print("i will print if both conditions are True") else: print("I will print if condition 2 is False") else: print("I will print if condition 1 is False")
Code given below is one of the classic example of nested if-else statement. In this example we try to find out greater number among given three number(p,q and r).
First we compare value of p with value of q, the condition is “p>q” which is false, hence if block will not get executed instead else block will get executed.
In else block we have one more if-else statement. This time condition in if statement is “q>r” which is also false, hence else block will get executed.
""" Author : ITVoyagers (itvoyagers.in) Date :9th August 2020 Description : Program to show use of nested if...else statement """ p=int(input("Enter a number")) q=int(input("Enter a number")) r=int(input("Enter a number")) print("p=",p,"q=",q,"r=",r) if p>q: if p>r: print("p is greater") else: print("r is greater") else: if(q>r): print("q is greater") else: print("r is greater")
OUTPUT

For other python basics related posts:
For other advanced python related posts: