Nested Conditions
The conditional block inside another if/else conditional block is called as nested conditional block. In the below example, Block 2 is nested conditional block and condition B is called nested conditional statement.
Code
PYTHON
Input
Output
Input
Output
Nested Condition in Else Block
We can also write nested conditions in Else Statement.
In the below example Block 2 is a nested conditional block.
Code
PYTHON
Output
Elif Statement
Use the elif statement to have multiple conditional statements between if and else.
The elif statement is optional.
Multiple Elif Statements
We can add any number of
elif
statements after if
conditional block.Execution of Elif Statement
Python will execute the elif block whose expression evaluates to true.
If multiple
elif
conditions are true, then only the first elif block which is True will be executed.Optional Else Statement
Else statement is not compulsory after
if - elif
statements.Code
PYTHON
Output
Possible Mistake
Cannot write an elif statement after
else
statement.