Python Conditional Statements

basic
Published

October 7, 2024

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.

age = 20
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.

grade = 85

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.

weather = "sunny"

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.

x = 10
y = 5

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:

age = 22
status = "Adult" if age >= 18 else "Minor"
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.

temperature = 25
is_sunny = True

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.