Make Your Code Smarter with If, Else, and Elif

Learn how to control the flow of your Python programs using conditional statements. …

Updated August 26, 2023



Learn how to control the flow of your Python programs using conditional statements.

Welcome! In this article, we’ll dive into one of the most fundamental concepts in programming – conditional statements. These powerful tools allow us to create dynamic and intelligent code that can make decisions based on specific conditions.

Think about it like this: you encounter a fork in the road. Which path you take depends on your destination. Similarly, conditional statements let your Python program choose different paths of execution based on whether certain conditions are met.

Why Are Conditional Statements Important?

Conditional statements are essential because they empower our programs to:

  • React to User Input: Imagine a simple program that asks the user for their age. Using a conditional statement, you can check if the age is above 18 and display a “Welcome” message only if it is.
  • Process Data Differently: Let’s say you have a list of numbers. You could use a conditional statement to identify even numbers and perform a specific calculation on them, while odd numbers are handled differently.
  • Control Program Flow: Conditional statements act like traffic signals, directing the execution of your code along different routes depending on the circumstances.

The Key Players: if, elif, and else

Let’s break down the core components of conditional statements in Python:

  1. if Statement: The if statement is the starting point. It checks a condition. If the condition is True, the code block indented beneath it will be executed.

    temperature = 25
    
    if temperature > 30:
        print("It's a hot day!")
    

    In this example:

    • We have a variable temperature set to 25.
    • The if statement checks if temperature is greater than 30. Since it’s not, the indented code block (which prints “It’s a hot day!”) won’t be executed.
  2. elif Statement: elif stands for “else if.” It allows you to check additional conditions if the preceding if or elif conditions were False.

    temperature = 25
    
    if temperature > 30:
        print("It's a hot day!")
    elif temperature > 20:
        print("It's a pleasant day.") 
    

    Here, because temperature is 25 (greater than 20), the code under the elif statement will execute, printing “It’s a pleasant day.”

  3. else Statement: The else statement acts as a catch-all. If none of the preceding if or elif conditions are True, the code block under else will be executed.

    temperature = 15
    
    if temperature > 30:
        print("It's a hot day!")
    elif temperature > 20:
        print("It's a pleasant day.")
    else:
        print("It's a bit chilly.")
    

    Since temperature is 15, neither the if nor the elif conditions are met. Therefore, the code under the else statement prints “It’s a bit chilly.”

Common Mistakes and Tips:

  • Indentation Matters: Python relies heavily on indentation to define code blocks. Make sure your code within if, elif, and else statements is properly indented (usually four spaces).

  • Boolean Expressions: Conditions inside conditional statements must evaluate to either True or False. Familiarize yourself with comparison operators (==, !=, >, <, >=, <=) and logical operators (and, or, not).

  • Keep It Concise: Aim for clear and readable code. Avoid overly complex nested conditional statements if possible.

Practical Example: Checking Grades

score = 85

if score >= 90:
    print("You got an A! Excellent work!")
elif score >= 80:
    print("You got a B. Great job!")
elif score >= 70:
    print("You got a C. Keep studying!")
else:
    print("You need to improve. Don't give up!")

Beyond the Basics

Conditional statements are just the beginning. As you progress in your Python journey, you’ll encounter more advanced control flow structures like loops and functions. These tools will help you build even more sophisticated and powerful programs.


Stay up to date on the latest in Computer Vision and AI

Intuit Mailchimp