Python’s conditional statements are fundamental building blocks for creating dynamic and responsive programs. They allow your code to make decisions based on different conditions, executing specific blocks of code only when certain criteria are met. This post will look into the core conditional statements: if
, elif
(else if), and else
, providing clear explanations and practical examples.
The if
Statement: The Foundation of Decision-Making
The simplest conditional statement is the if
statement. It checks a condition; if the condition evaluates to True
, the code block indented under the if
statement is executed. If the condition is False
, the code block is skipped.
= 20
age if age >= 18:
print("You are an adult.")
In this example, the condition age >= 18
is evaluated. Since 20 is greater than or equal to 18, the output will be:
You are an adult.
Adding More Conditions with elif
When you need to check multiple conditions sequentially, the elif
(else if) statement comes into play. Python checks each elif
condition in order, only executing the code block associated with the first condition that evaluates to True
.
= 85
grade
if grade >= 90:
print("A")
elif grade >= 80:
print("B")
elif grade >= 70:
print("C")
else:
print("F")
In this scenario, the output is “B” because the condition grade >= 80
is the first condition to be true.
The else
Statement: Handling Default Cases
The else
statement provides a default action to be executed if none of the preceding if
or elif
conditions are true. It’s optional but often useful for handling situations where none of the specific conditions match.
= "sunny"
weather
if weather == "rainy":
print("Take an umbrella.")
elif weather == "snowy":
print("Wear a warm coat.")
else:
print("Enjoy the sunshine!")
If weather
is “sunny,” the output will be “Enjoy the sunshine!”.
Nested Conditional Statements: Combining Conditions
You can nest conditional statements within each other to create more complex logic. This allows you to handle decision-making processes.
= 10
x = 5
y
if x > 5:
if y < 10:
print("x is greater than 5 and y is less than 10")
else:
print("x is greater than 5 but y is not less than 10")
else:
print("x is not greater than 5")
Conditional Expressions (Ternary Operator): Concise Conditionals
Python offers a concise way to express simple conditional logic using a ternary operator:
= 22
age = "Adult" if age >= 18 else "Minor"
status print(status) # Output: Adult
This single line achieves the same result as a longer if-else
statement.
Boolean Operators: Enhancing Conditional Logic
Boolean operators (and
, or
, not
) allow you to combine multiple conditions within a single if
statement, creating more complex decision-making processes.
= 25
temperature = True
is_sunny
if temperature > 20 and is_sunny:
print("Perfect day for a picnic!")
This example demonstrates the use of and
to ensure both conditions are true before executing the print
statement. Experiment with or
and not
to further refine your conditional logic.