Python If Else and Nested If Else
Python If Else and Nested If Else
Python If Else
Python If Else – Basic idea of an if else condition block is that, when the condition is satisfied, the statements
in the if block are executed, and when the condition is not satisfied, the statements in the else block are
executed.
Output
Note : Observe the indentation and alignment of the statements inside the if-block or else-block. Unlike
programming languages like java or c where the statements are enclosed in curly braces, python considers the
alignment of statements to consider them in the block.
Nested if-else
This is an extension for the idea of if-else block. Any statement in the if or else blocks could be another if or if-
else block.
number_1 = 5
number_2 = 6
if number_1 * number_2 == 20:
print("The condition is true.")
print("Inside if block")
if (number_1 == number_2):
print("Both numbers are same")
else:
print("Both numbers are not same")
else:
print("The if condition is false.")
print("Inside else block")
if number_1 + number_2 == 11:
print("Addition of two numbers equal 11.")
else:
print("Addition of two numbers is not equal to 11.")
print("Outside nested if-else condition block.")
Output
Conclusion
In this Python Tutorial, we have learnt if-else conditional statement, and how any statement in if or else block
could be another if or if-else conditional statements.
Python Programming
⊩ Python Tutorial
⊩ Install Python
⊩ Python Variables
⊩ Python Comments
Control Statements
⊩ Python If
⊩ Python If Else
Python String
Functions
⊩ Python Functions
Python Collections
⊩ Python List
⊩ Python Dictionary
Advanced
⊩ Python Multithreading
Useful Resources