Skip to Content
Course content

48: Conditional Statements: if, elif, else

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Think of a conditional statement like a bouncer at the door of a club. The bouncer has a specific set of rules they have to follow before letting someone in. First, they check if you're on the VIP list. If you are, you walk right in. If you're not, they don't just send you home immediately—they check if you have a standard ticket. If that's true, you're in. If neither of those is true? Well, you're staying on the sidewalk.

In Python, we do the exact same thing. We tell the computer to evaluate a condition (a question that results in True or False) and then execute a specific block of code based on that answer. If the first condition is met, Python runs that block and skips everything else in that sequence. If not, it moves to the next check.

The Primary Gatekeeper

The if statement is your first line of defense. It's the VIP list. If the condition is True, the code indented underneath it runs. If it's False, Python simply ignores that block and moves on.

temperature = 30

if temperature > 25:
    print("It's a scorcher! Turn on the AC.")

Notice the colon and the indentation. I can't stress this enough: in Python, that whitespace isn't just for looks. It's how the language knows which lines of code "belong" to that conditional check.

Handling the Middle Ground

Usually, life isn't just "Yes" or "No." Often, there's a "Maybe" or a "Wait, let me check something else." That's where elif (short for "else if") comes in. It allows you to chain multiple checks together. The key thing to remember is that Python checks these in order. As soon as it finds one that is True, it executes it and exits the entire structure.

temperature = 15

if temperature > 25:
    print("Turn on the AC.")
elif temperature < 10:
    print("Grab a heavy coat.")
elif temperature < 20:
    print("A light sweater should do it.")

In the example above, since the temperature is 15, it fails the first check, fails the second, but hits the third. It prints the sweater message and then stops. It doesn't matter if there are more elif statements below it; once a condition is met, the search is over.

The Catch-All Safety Net

What happens if none of your conditions are met? Without an else statement, your program would just move past the block and do nothing. The else block is your "none of the above" option. It doesn't take a condition because it's simply what happens when every other check has failed.

temperature = 22

if temperature > 25:
    print("Turn on the AC.")
elif temperature < 18:
    print("Turn on the heater.")
else:
    print("The temperature is perfect. Do nothing.")

I like to think of else as a safety net. It ensures that no matter what input your program receives, there is always a defined outcome. It prevents the program from just falling through the cracks.




📋 Practical Task

Building a Movie Ticket Pricing Engine

You've been hired to write the logic for a cinema's automated kiosk. The ticket price varies based on the customer's age. Your task is to create a script that determines the final price based on these rules:

  • If the customer is under 12 years old, the price is $7.
  • If the customer is 65 years or older, the price is $9.
  • For everyone else, the price is $12.

Write a program that initializes an age variable and uses if, elif, and else to print the correct ticket price. Test your code with three different ages to ensure all three price brackets are working correctly.

Rating
0 0

There are no comments for now.

to be the first to leave a comment.