Python If Else, If Elif and Nested If Statement Examples

Published: Feb 26, 2022 - Updated: Mar 16, 2022

If else statement in programming is a conditional statement that runs a different set of statements depending on whether an expression is true or false.

In this snippet, we'll take a look at the examples of "if else", "if elseif else" and nested if statement.

Table of Contents

If Else Statement

num1 = 5
num2 = 3

if num1 > num2:
    print('num1 is greater than num2')
    print('num1 is not greater than num2')

If Elif Statement

num = 2

if num > 0:
    print('Positive number')
elif num == 0:
    print('Negative number')

Nested If Statement

num = 15

if num == 10:
    print("num is 10")
elif num == 15:
    print("num is 15")
elif num == 20:
    print("num is 20")
    print("num is not present")