If, Else, and Elif in Python: A Guide to Conditional Statements

Conditional statements are a fundamental concept in programming, allowing you to make decisions and control the flow of your code. In Python, we have the if, else, and elif statements to handle different cases based on certain conditions. In this blog post, we’ll explore these statements and provide examples to help you understand their usage.

Learning Objectives

By the end of this post, you will:

  • Understand the syntax and usage of if, else, and elif statements in Python.
  • Be able to write conditional statements to make decisions in your code.

Example Scenario: Deciding What to Wear Based on Weather

Let’s start with a practical scenario to demonstrate the use of conditional statements. Imagine you’re getting ready in the morning, and you want to decide what to wear based on the weather. We’ll use this scenario throughout the post to illustrate the concepts.

The if Statement

The if statement is used to execute a block of code only if a condition is true. Here’s how it looks in Python:

if condition:

# Code to be executed if the condition is true

For our weather scenario, let’s say that if the temperature is above 20 degrees Celsius, you want to wear shorts. Otherwise, you’ll wear jeans. Here’s how you can implement this in Python:

temperature = 25

if temperature > 20:

print("Wear shorts.")

In this example, the code inside the if block will be executed because the condition temperature > 20 is true. So, the output will be “Wear shorts.”

The else Statement

The else statement is used to specify a block of code that should be executed when the condition in the if statement is false. Here’s the syntax:

if condition:

# Code to be executed if the condition is true

else:

# Code to be executed if the condition is false

Let’s enhance our weather scenario by adding the else statement. If the temperature is not above 20 degrees Celsius, we’ll wear jeans. Here’s how it looks:

temperature = 18

if temperature > 20:

print("Wear shorts.")

else:

print("Wear jeans.")

In this case, since the condition temperature > 20 is false, the code inside the else block will be executed. The output will be “Wear jeans.”

The elif Statement

The elif statement, short for “else if,” is used to specify additional conditions to check if the previous conditions are false. It allows you to handle multiple cases without nesting multiple if statements. Here’s the syntax:

if condition:

# Code to be executed if condition1 is true

elif condition2:

# Code to be executed if condition2 is true

else:

# Code to be executed if all conditions are false

Let’s expand our weather scenario further. If the temperature is above 25 degrees Celsius, wear shorts. If it’s between 20 and 25 degrees, wear capris. Otherwise, wear jeans. Here’s the code:

temperature = 23

if temperature > 25:

print("Wear shorts.")

elif temperature >= 20:

print("Wear capris.")

else:

print("Wear jeans.")

In this example, the condition temperature > 25 is false, so we move to the next condition. Since temperature >= 20 is true, the code inside the elif block will be executed. The output will be “Wear capris.”

Prerequisites

To make the most of this post, you should have a basic understanding of programming concepts, such as variables and comparisons. Familiarity with Python syntax is also beneficial.

Conclusion

Conditional statements are powerful tools in programming that enable you to make decisions based on different conditions. In this blog post, we explored the if, else, and elif statements in Python. We learned how to use them and saw examples in our weather scenario. Armed with this knowledge, you can now incorporate conditional statements into your Python code to handle various scenarios and make your programs more dynamic.