Introduction:

Now that we’ve explored if statements, else, and comparisons with strings and numbers, it’s time to make your code even more powerful with elif (else-if) statements and logical operators like and, or, and not. These concepts will allow your code to make multiple decisions more efficiently and handle more complex conditions.

Introduction to elif

In the last lesson, we learned about nested if statements, which allowed us to handle multiple conditions by checking them one after the other. While this works, elif (short for else if) offers a cleaner and more readable way to extend your if statements when you need to check several conditions.

Real-World Example:

Imagine you’re checking the temperature to decide what to wear:

  • If the temperature is greater than 25°C, you might wear shorts.
  • Else if it’s between 15°C and 25°C, you might wear a light jacket.
  • Else, you wear a warm coat.

Instead of nesting these checks, elif lets you structure the conditions in a more readable way:

temperature = 18
if temperature > 25:
    print("Wear shorts.")
elif temperature >= 15:
    print("Wear a light jacket.")
else:
    print("Wear a warm coat.") 

In this example:

  • The program checks the first condition (temperature > 25).
  • If that’s false, it moves to the elif and checks if the temperature is between 15 and 25.
  • If both are false, the else block handles the final condition.

Predict:

What will this code print? Make a prediction. What will happen if the grade is 85? What about if the grade is 92 or 65?

grade = 85
if grade >= 90:
    print("You got an A!")
elif grade >= 80:
    print("You got a B!")
elif grade >= 70:
    print("You got a C!")
else:
    print("You need to improve.")

Run:

Now, let’s try using elif in the Python IDE. Run the code below and see how elif can cleanly handle multiple conditions.

Explanation:

  • The program checks if the score is greater than or equal to 90 (A).
  • If not, it checks if it’s 75 or more (B).
  • If neither of these is true, it checks if it’s 50 or more (C).
  • If none of these conditions are true, the else clause handles the rest.

Introducing Logical Operators

In addition to using elif, you can make your conditions even smarter by combining multiple conditions using logical operators: and, or, and not.

Logical Operators:

  • and: Checks if both conditions are true.
  • or: Checks if at least one condition is true.
  • not: Reverses the condition (checks if something is not true).

Real-World Example:

You want to decide whether to go outside:

  • If it’s sunny and above 15°C, you’ll go for a walk.
  • If it’s raining or below 10°C, you’ll stay home.

With logical operators, we can combine these conditions like this:

is_sunny = True
temperature = 12
if is_sunny and temperature > 15:
    print("Go for a walk!")
elif not is_sunny or temperature < 10:
    print("Stay home.")
else:
    print("Maybe later.")

This code helps decide whether to go for a walk based on two conditions: whether it’s sunny and the current temperature. It uses logical operators (and, or, and not) to combine these conditions.

  • if is_sunny and temperature > 15:
    This checks two conditions:

    1. is_sunny: If it’s sunny (which is True in this case).
    2. temperature > 15: If the temperature is greater than 15°C.

    If both of these conditions are true, the program prints "Go for a walk!".

  • elif not is_sunny or temperature < 10:
    This checks either of these conditions:

    1. not is_sunny: If it’s not sunny (this reverses the is_sunny condition).
    2. temperature < 10: If the temperature is less than 10°C.

    If either of these conditions is true, the program prints "Stay home.".

  • else:
    If neither of the above conditions are met, the program defaults to printing "Maybe later."

How to Read This Code:

  1. First condition: Check if it’s sunny and the temperature is above 15°C.
  2. Second condition: If the first condition isn’t true, check if it’s not sunny or the temperature is below 10°C.
  3. Default action: If neither condition is true, print "Maybe later."

Predict:

Here’s a piece of code using logical operators. If time = 14 and is_hungry = True, what will be printed?

is_hungry = True
time = 14         #14:00 (2 PM)
if is_hungry and time < 12:
    print("Have breakfast.")
elif is_hungry and time >= 12 and time < 18:
    print("Have lunch.")
else:
    print("Have dinner.")

Run:

Now, let’s run a program using and to combine multiple conditions.

Explanation:

  • The program first checks if the person is 18 or older and a student.
  • If not, it checks if the person is younger than 18 and a student.
  • If neither of these conditions is true, the program prints that the person isn’t eligible for a discount.

Investigate:

Now, let’s investigate how changing values in a condition with logical operators affects the outcome. Run this code and then experiment by changing has_ticket and is_over_18 to see how the output changes.

Instructions:

  • Try changing has_ticket to True and see how the output changes.
  • Can you predict what happens when both has_ticket and is_over_18 are True?

Modify:

Here’s a piece of code that’s not quite right. It’s meant to check if someone is eligible for a family discount. Can you modify it to make it work?

Hint: The logic should check if the person is less than 12 or has a family card. Try adjusting the condition.

Scroll to Top