# Conditional Statements (if/else) in Python

One of the fundamental concepts in programming is conditional statements. In Python, if/else statements are used to control the flow of execution by testing the values of variables and making decisions based on those values.

## The Syntax of If/Else Statements

The basic structure of an if/else statement in Python is as follows:

``````if condition:
# Code to execute if condition is True
else:
# Code to execute if condition is False
``````

The condition is a logical expression that can be evaluated to either True or False. If the condition evaluates to True, the code inside the if block is executed. If the condition is False, the code inside the else block is executed.

For example, let’s say we have a variable x that is equal to 5. We can use an if/else statement to test if x is greater than 10:

``````x = 5

if x > 10:
print("x is greater than 10")
else:
print("x is not greater than 10")
``````

In this example, the condition `x > 10` is False, so the code inside the else block is executed, and the output is `x is not greater than 10`.

## Multiple Conditions with elif

Sometimes we have more than two possible outcomes, and we need to test more than one condition. In Python, we can use the elif statement to test multiple conditions. The syntax is similar to if/else:

``````if condition1:
# Code to execute if condition1 is True
elif condition2:
# Code to execute if condition2 is True
else:
# Code to execute if all conditions are False
``````

The elif statement allows us to test multiple conditions, where each condition is tested only if the preceding condition is False. If all conditions are False, the code inside the else block is executed.

For example, let’s say we have a variable grade that can take on the values “A”, “B”, “C”, “D”, or “F”. We can use an if/elif/else statement to assign a message based on the grade:

``````grade = "B"

message = "Excellent"
message = "Good"
message = "Fair"
message = "Poor"
else:
message = "Fail"

print(message)
``````

In this example, the variable message is assigned the value “Good” because the grade is “B”.

## Nesting If/Else Statements

Sometimes we need to test more than one condition for a single outcome. In Python, we can nest if/else statements inside other if/else statements to accomplish this. The syntax is as follows:

``````if condition1:
if condition2:
# Code to execute if condition1 and condition2 are True
else:
# Code to execute if condition1 is True and condition2 is False
else:
# Code to execute if condition1 is False
``````

In this example, two conditions must be true for the code inside the nested if block to execute. If condition1 is True but condition2 is False, the code inside the nested else block is executed.

For example, let’s say we have two variables x and y. We want to test if x is greater than 0 and if y is greater than 0:

``````x = 5
y = 10

if x > 0:
if y > 0:
print("x and y are both positive")
else:
print("x is positive but y is not")
else:
print("x is not positive")
``````

In this example, both conditions are True, so the output is `x and y are both positive`.

## Conclusion

Conditional statements are a powerful tool for controlling the flow of execution in Python. They allow us to make decisions based on the values of variables and test multiple conditions for different outcomes. By using if/else statements, elif statements, and nested if/else statements, we can write more complex programs that can make decisions based on a variety of input conditions.

Scroll to Top