We're actively developing the Python course; advanced AI courses will be released soon.

Learn AI Way

Python If, Elif, Else


1. If Condition


It is very important to learn if condition in Python language because it is the decision-making heart of every program.

They are used in almost every application:

•    Chatbots
•    AI applications
•    Web portals
•    Any business logic

The if statement allows your program to check a condition and take action based on whether it is true or false.

Syntax:

if condition:
    # Code to execute if condition is True
elif another_condition:
    # Code runs if first condition is False and this is True
else:
    # Runs if all conditions above are False

Example 1:

marks = 82

if marks >= 90:
    print("Grade: A")
elif marks >= 75:
    print("Grade: B")
else:
    print("Grade: C")

Output: 

Grade: B

It checks the value of marks and prints the grade accordingly. Since 82 is greater than 75, hence it prints output as Grade: B 

Please note that Python checks conditions from top to bottom, and it stops once a True condition is found in the program.

Real-World Example:

Suppose there is an online shopping website that decides discount based on cart-value.

Example 1:

cart_value = 3500

if cart_value > 5000:
    print("20% discount")
elif cart_value > 2000:
    print("10% discount")
else:
    print("No discount")

Output: 

10% Discount

Application:

1.    It is used in E-commerce price logic.
2.    It is also used in loan approval, interest rate calculation, or insurance premium logic.

Why It Matters:

1.    Decision-making is used in every intelligent system.
2.    In every automation project, if conditions are used whether you need to validate login credentials or to filtering data.
3.    It helps you start thinking like a real programmer.

2. Nested If Statements in Python 


A nested if statement means an if inside another if block.

It is used when you want your code to perform a deeper level of decision-making.

For example, if you want to check multiple levels of conditions before executing anything, then nested if statements are used.

Syntax:

if condition1:
    if condition2:
        # Runs only if both are True

Example:

age = 25
has_license = True
has_car = True

if age >= 18:
    print("You are an adult")
    if has_license:
        print("You can legally drive")
        if has_car:
            print("Enjoy your ride!")
        else:
            print("You need a car to drive")
    else:
        print("You need to get your driving license first ")
else:
    print("You're still a minor")

Output:

You are an adult
You can legally drive
Enjoy your ride!


Above given code checks multiple conditions (step by step):

•    If the person is an adult
•    Has a driving license
•    And has a car as well

If all conditions are true, then it prints the following messages:

•    You are an adult
•    You can legally drive
•    Enjoy your ride

Real-World Example

In an e-commerce application, nested if ensures that multiple steps (like given below) are executed in an order:

•    First, you add an item to a cart
•    After that, you add the delivery address for delivery
•    Finally, the customer makes the payment

After all these steps, an e-commerce application confirms your order and generates an order number.

Why It Matters:

a) Nested if is suitable for complex workflows because it gives your program depth in logic.
b) It is very useful when you need to write a logic to decide multiple layers of validation.

Note:

It is powerful and useful tool, but too many layers will make your code messy. So always use it wisely.
If you have a deep logic, then it is always good to use either logical operators (like and, or) or functions as they will help to simplify your code.

3. The Ternary Operator


The ternary operator helps you to write if-else statements in a short and simpler way.
You can write your code in a single line instead of writing it in multiple lines of code.

Syntax:

value_if_true if condition else value_if_false

Example:

age = 20
message = "Adult" if age >= 18 else "Minor"
print(message)

Output: 

Adult

It first checks the condition (age >= 18).
If it is true, then it prints the first value (Adult); otherwise, it prints the second one (Minor).

Another way of writing the above code with if-else statements is:

age=20

if age >= 18:
    print("Adult")
else:
    print("Minor")

The ternary operator helps you to write simple, compact, and clean code, so that one could understand the logic fast and make decisions faster.

Real World Example

In real-world applications, we often need to show different messages after checking if a user is logged in or not.
Instead of writing multiple lines of code with if-else statements, we can also write a clean and compact version using the ternary operator, as shown below:

Example:

is_logged_in = False
status = "Welcome back" if is_logged_in else "Please log in"
print(status)

Output: 

Please log in

Why It Matters

In real-world production code, many developers use it to make their code more efficient and faster to execute.

Learn more about Comparison Operators in our “Python Comparison Operators” chapter.