Python If Else, If Elif and Nested If Statement Examples

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')
else:
    print('num1 is not greater than num2')

If Elif Statement

num = 2

if num > 0:
    print('Positive number')
elif num == 0:
    print('Zero')
else:
    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")
else:
    print("num is not present")

Software Engineer | Ethical Hacker & Cybersecurity...

Md Obydullah is a software engineer and full stack developer specialist at Laravel, Django, Vue.js, Node.js, Android, Linux Server, and Ethichal Hacking.