Home → Articles → How to Use Python If Else Statement

How to Use Python If Else Statement

Introduction

The Python If Else statement allows you to execute different blocks of code based on certain conditions. It is one of the fundamental control flow tools that makes your programs dynamic and responsive. You can use If Else statements to check conditions, such as whether a number is positive, and perform actions accordingly. This makes it essential for building decision-making logic in applications.

This guide shows you how to use the Python If Else statement.

Prerequisites

Before you begin:

Declare Python If Else Statement

The If Else statement in Python helps to make decisions in your code. It checks a condition, and if the condition evaluates to True, the if block executes. Otherwise, the else block runs.

Here's a basic syntax:

Python
if condition:
    # Code to run if the condition is true
else:
    # Code to run if the condition is false

Example:

Python
number = 5

if number > 0:
    print("The number is positive")
else:
    print("The number is not positive")

In this example, the code checks whether number is greater than 0 and prints the corresponding message based on the result.

Combine Multiple Conditions

You can combine multiple conditions using logical operators such as and, or, and not. This helps when decisions rely on more than one condition.

Example:

Python
age = 20
income = 30000

if age > 18 and income > 20000:
    print("Eligible for the credit card")
else:
    print("Not eligible")

In the above example, the program ensures both conditions (age > 18 and income > 20000) are evaluates to True before printing the eligibility message. For nested or complex logic, group conditions using parentheses.

Nested If Else Statements

In some cases, you may need to check multiple conditions in a hierarchical manner. Nested If Else statements help you handle such situations. A nested If Else occurs when one If Else is placed inside another.

Example:

Python
age = 16

if age > 18:
    print("Adult")
else:
    if age > 12:
        print("Teenager")
    else:
        print("Child")

This example checks if age is greater than 18 first. If not, it moves to the next condition (age > 12) and so on.

Although powerful, avoid overusing nested statements as they can make code harder to read and maintain.

Implement Python If Else Statement Best Practices

When using If Else statements, follow these best practices:

Example with elif:

Python
score = 85

if score > 90:
    print("Grade: A")
elif score > 80:
    print("Grade: B")
else:
    print("Grade: C")

Using elif improves clarity, especially when dealing with multiple conditions.

Discover If Else Statement Practical Use Cases

The Python If Else statement plays a key role in many real-world scenarios:

Example for validating user input:

Python
password = "python123"
user_input = input("Enter your password: ")

if user_input == password:
    print("Access granted")
else:
    print("Access denied")

This example demonstrates basic password validation.

Conclusion

This guide explains the Python If Else statement, including its syntax, combining multiple conditions, nested logic, best practices, and practical use cases. These statements are crucial for creating responsive and dynamic code, allowing you to build smarter applications. Understanding how to implement If Else effectively can significantly improve your programming skills and the quality of your applications.