Example Of An if/else Expression In Python – Detailed FAQ

If and if..else statements were covered in earlier lectures. We will cover the Python if-elif-else expression in this tutorial. For situations when we need to check many conditions, we utilize the if..elif..else statement.

Python’s if-elif-else statement syntax

We are checking many conditions in this manner.

if condition:
elif condition_2:
elif condition_3:

1. Multiple “elif” blocks are permitted, but only one “else” block is permitted.
2. Only one block of code is actually executed out of all of these blocks. The code inside “if” is run if the condition is true; if it is false, the condition linked with “elif” is evaluated, and so on. The code contained in the brackets “else” is run if none of the conditions are true.

Example of an if..elif..else sentence in Python

Using the if..elif..else expression, we are verifying many criteria in this example.

num = 1122
if 9 < num < 99:
    print("Two digit number")
elif 99 < num < 999:
    print("Three digit number")
elif 999 < num < 9999:
    print("Four digit number")
    print("number is <= 9 or >= 9999")

Python if elif else statement

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button