Skip to main content

Conditional Statements: if-elif-else (Part 3)

Following our exploration of Conditional Statements: if-else (Part 2), this article introduces the if-elif-else statement, which allows you to check multiple conditions in a sequence.


📚 Prerequisites

A basic understanding of Python's if and if-else statements.


🎯 Article Outline: What You'll Master

In this article, you will learn:

  • The syntax of the if-elif-else statement.
  • How to use the if-elif-else statement to handle multiple conditions.
  • The difference between chained and nested conditionals.

🧠 Section 1: The if-elif-else Statement

The if-elif-else statement allows you to check multiple conditions in a sequence. The elif keyword is short for "else if".

Syntax:

if condition1:
# code to be executed if condition1 is true
elif condition2:
# code to be executed if condition1 is false and condition2 is true
else:
# code to be executed if all preceding conditions are false

Python will evaluate the conditions from top to bottom. As soon as it finds a True condition, it will execute the corresponding block of code and then skip the rest of the if-elif-else statement.


💻 Section 2: Handling Multiple Conditions

Let's look at an example of grading based on a score:

score = 85

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

In this example, the first condition (score >= 90) is False. The program then moves to the next condition (score >= 80), which is True. The code inside this elif block is executed, and the rest of the statement is skipped.


🛠️ Section 3: Chained vs. Nested Conditionals

  • Chained Conditionals: Use if, elif, and else to create a series of alternative branches. This is the preferred way to handle multiple conditions.

  • Nested Conditionals: An if statement inside another if statement. While this is possible, it can make your code harder to read and should be avoided if possible.

Nested Conditional Example:

num = 10

if num >= 0:
if num == 0:
print("Zero")
else:
print("Positive number")
else:
print("Negative number")

This can be rewritten more cleanly using if-elif-else:

if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")

💡 Conclusion & Key Takeaways

You've now learned how to use the if-elif-else statement to handle multiple conditions in your Python programs.

Let's summarize the key takeaways:

  • if-elif-else Statement: A way to check multiple conditions in a sequence.
  • elif: Short for "else if".
  • Chained vs. Nested: Prefer chained conditionals over nested conditionals for readability.

Challenge Yourself: Write a script that takes a month as input (e.g., "January", "February") and prints the season ("Winter", "Spring", "Summer", or "Fall").


➡️ Next Steps

In the next article, we'll learn about the "Ternary Operator".

Happy coding!


Glossary (Python Terms)

  • Chained Conditional: A conditional statement with multiple branches using if, elif, and else.
  • Nested Conditional: A conditional statement inside another conditional statement.

Further Reading (Python Resources)