Python: How to Use if-then-else Statements

Posted in Python by Dirk - last update: Dec 19, 2023

In programming, if-then-else statements are used for conditional execution, allowing you to control the flow of your program based on certain conditions. Python has multiple ways to handle this, from a simple if statement to complex nested if statements

Simple if statement

This is the most basic form of the if statement. It checks a condition, and if it evaluates to True, the indented block of code beneath it is executed.

if condition:
    # Code to be executed if the condition is True

if - else statement

In this form, if the condition is True, the code inside the first block is executed; otherwise, the code inside the second block (after else) is executed.

if condition:
    # Code to be executed if the condition is True

else:
    # Code to be executed if the condition is False

if - elif - else statement

This allows you to check multiple conditions in sequence. If the first condition is True, the corresponding block of code is executed. If not, it checks the next condition (elif stands for else if).

If none of the conditions are True, the code in the else block is executed.

if condition1:
    # Code to be executed if condition1 is True

elif condition2:
    # Code to be executed if condition2 is True

else:
    # Code to be executed if none of the conditions are True

Nested if statements:

You can also nest if statements within each other to create more complex conditional structures.

if condition1:
    # Code to be executed if condition1 is True

    if condition2:
        # Code to be executed if both condition1 and condition2 are True

    else:
        # Code to be executed if condition1 is True but condition2 is False

else:
    # Code to be executed if condition1 is False

Shorthand if else statements

Python also supports a one-liner conditional expression, also known as the ternary operator or . It provides a concise way to write simple if-else statements.

result = value_if_true if condition else value_if_false

Logical operators

Logical operators are used to combine multiple conditions in Python’s if statements to create more complex conditions. Here are some examples using logical operators:

and operator

The and’ operator requires both conditions to be True for the combined condition to be True.


age = 25
income = 50000

if age > 18 and income > 30000:
    print("Eligible for a loan")

else:
    print("Not eligible for a loan")

In the example above - both conditions (age above 25 and income above 50.000) need to be fulfilled to get a loan.

or operator

The or operator requires at least one of the conditions to be True for the combined condition to be True.

is_student = True
is_employee = False

if is_student or is_employee:
    print("You are part of the academic community.")
else:
    print("You are not part of the academic community.")

In the example above - one can be or student or employee of the academic community at the university.

not operator

The not operator negates the condition, making True conditions False and vice versa.

has_permission = False

if not has_permission:
    print("Access denied.")

else:
    print("Access granted.")

In the example above - if you don’t have a permission - access is denied.

Combining multiple conditions

You can use logical operators to combine more than two conditions.

is_weekend = True
has_time_off = True
is_sunny = False

if is_weekend and has_time_off and not is_sunny:
    print("Let's have a relaxing indoor weekend.")

else:
    print("Plan your activities accordingly.")

In this example - you plan an indoor weekend when it is weekend, you are free, and it is not sunny.

References:

Other articles