Making Decisions in Python: If/Else Statements Made Simple

Learn Python if/else statements through real-world examples and simple illustrations. Master conditional logic to make your Python programs smart and interactive.

๐Ÿ•’ 6 min read
Python code showing if/else statements with traffic light analogy

Hey there! Welcome back to our Python journey. ๐Ÿ

Remember when we learned about variables and data types? Well, today we’re going to make our programs smart by teaching them how to make decisions!

Think about your daily life:

  • If it’s raining, you take an umbrella โ˜”
  • Else if it’s sunny, you wear sunglasses ๐Ÿ˜Ž
  • Else you check the weather app! ๐Ÿ“ฑ

Python’s if/else statements work exactly the same way. Let me show you how simple and powerful they are!

The Basic If/Else Structure

Let’s start with a simple real-world example:

# Simple analogy: Deciding what to wear
weather = "rainy"

if weather == "rainy":
    print("Take an umbrella! โ˜”")
else:
    print("Enjoy the sunshine! โ˜€๏ธ")

What’s happening here?

We check if the weather is “rainy”

If true, Python runs the indented code below

If false, it runs the code under else

๐Ÿšฆ Traffic Light Analogy Think of if/else statements like traffic lights:

light_color = "red"

if light_color == "red":
    print("๐Ÿ›‘ STOP!")
elif light_color == "yellow":
    print("โš ๏ธ SLOW DOWN!")
else:  # green light
    print("โœ… GO!")

Comparison Operators: The Decision Tools Before we dive deeper, let’s understand the tools Python uses to make comparisons:

Equal to

age = 25
if age == 25:  # True
    print("You're 25 years old!")

Not equal to

if age != 30:  # True
    print("You're not 30 years old")

Greater than

if age > 18:   # True
    print("You're an adult")

Less than

if age < 65:   # True  
    print("You're not a senior yet")

Greater than or equal to

if age >= 21:  # True
    print("You can drink in the US")

Less than or equal to

if age <= 25:  # True
    print("You're 25 or younger")

Multiple Conditions with Elif What if we have more than two options? Enter elif (short for “else if”):

Grading system example

score = 85

if score >= 90:
    grade = "A"
    message = "Excellent! ๐ŸŽ‰"
elif score >= 80:
    grade = "B"
    message = "Great job! ๐Ÿ‘"
elif score >= 70:
    grade = "C" 
    message = "Good work! ๐Ÿ‘"
elif score >= 60:
    grade = "D"
    message = "You passed! โœ…"
else:
    grade = "F"
    message = "Let's practice more! ๐Ÿ“š"

print(f"Grade: {grade} - {message}")

Important: Python checks conditions from top to bottom and stops at the first true condition!

Logical Operators: Combining Conditions Sometimes you need to check multiple things at once:

AND operator - both conditions must be True

age = 16
has_money = True

if age >= 13 and has_money:
    print("You can buy a movie ticket! ๐ŸŽฌ")

OR operator - at least one condition must be True

day = "saturday"
is_holiday = True

if day == "saturday" or day == "sunday" or is_holiday:
    print("It's the weekend! Time to relax! ๐Ÿ–๏ธ")

NOT operator - reverses the condition

is_raining = False

if not is_raining:
    print("Perfect day for a walk! ๐Ÿšถ")

Real-World Example: Movie Ticket System Let’s build a complete movie ticket system:

print("=== Welcome to Python Cinema ===")

age = int(input("How old are you? "))
has_money = input("Do you have money? (yes/no) ").lower() == "yes"
is_weekend = input("Is it weekend? (yes/no) ").lower() == "yes"

Decision making time!

if age < 5:
    print("๐ŸŽฌ Free ticket for little ones!")
elif age < 13:
    print("๐ŸŽฌ Child ticket: $8")
elif age >= 65:
    print("๐ŸŽฌ Senior discount: $10")
else:
    print("๐ŸŽฌ Adult ticket: $12")

Additional conditions

if has_money:
    print("๐Ÿ’ฐ You can purchase snacks!")
else:
    print("๐Ÿ’ธ Maybe just enjoy the movie!")

if is_weekend:
    print("๐ŸŽ‰ Weekend surcharge applies (+$2)")

Your Turn: Practice Exercise Try this simple game - a number guessing challenge:

import random

Computer picks a secret number

secret_number = random.randint(1, 10)

print("๐ŸŽฏ I'm thinking of a number between 1 and 10!")

# Get user's guess
guess = int(input("What's your guess? "))

Check the guess

if guess == secret_number:
    print("๐ŸŽ‰ Amazing! You guessed it!")
elif guess > secret_number:
    print("๐Ÿ“‰ Too high! Try a lower number.")
    print(f"The secret number was {secret_number}")
else:
    print("๐Ÿ“ˆ Too low! Try a higher number.") 
    print(f"The secret number was {secret_number}")

Challenge: Modify this game to give the user 3 tries to guess the number!

Common Mistakes to Avoid

  1. Forgetting the Colon

โŒ Wrong

if age == 25
    print("You're 25!")

โœ… Correct

if age == 25:
    print("You're 25!")


2. Using = Instead of ==

โŒ Wrong (this assigns instead of comparing)

if age = 25:
    print("You're 25!")

โœ… Correct

if age == 25:
    print("You're 25!")
  1. Incorrect Indentation

โŒ Wrong

if age > 18:
print("You're an adult")  # Missing indentation

โœ… Correct

if age > 18:
    print("You're an adult")  # Proper indentation
  1. Too Many Nested Conditions

โŒ Hard to read

if condition1:
    if condition2:
        if condition3:
            print("This is confusing!")

โœ… Better

if condition1 and condition2 and condition3:
    print("Much clearer!")

Advanced Example: Smart Weather Advisor Let’s build something really useful:

print("๐ŸŒค๏ธ  Smart Weather Advisor")

temperature = int(input("What's the temperature? (ยฐF) "))
is_raining = input("Is it raining? (yes/no) ").lower() == "yes"
is_weekend = input("Is it weekend? (yes/no) ").lower() == "yes"

print("\n๐Ÿ’ก My recommendations:")

Temperature-based recommendations

if temperature > 85:
    print("โ€ข Stay hydrated! ๐Ÿ’ง")
    print("โ€ข Wear light clothing ๐Ÿ‘•")
elif temperature > 60:
    print("โ€ข Perfect weather! ๐Ÿ˜Š")
    print("โ€ข Great for outdoor activities ๐Ÿšด")
else:
    print("โ€ข Bundle up! ๐Ÿงฅ")
    print("โ€ข Hot drink weather โ˜•")

Rain considerations

if is_raining:
    print("โ€ข Don't forget your umbrella! โ˜”")
    print("โ€ข Perfect movie day ๐ŸŽฌ")
else:
    print("โ€ข No umbrella needed! ๐ŸŒž")
    print("โ€ข Great for a walk ๐Ÿšถ")

Weekend specials

if is_weekend and not is_raining and temperature > 60:
    print("โ€ข Perfect for a picnic! ๐Ÿงบ")
    print("โ€ข Maybe visit the park ๐ŸŒณ")


Debugging Tips
Problem: My if/else statement isn't working!
Solution:

Check your colons :

Verify indentation (4 spaces)

Use print() to see variable values

Test each condition separately

Debugging example

age = 25
print(f"Debug: age = {age}")  # See the actual value

if age == 25:
    print("Condition is true!")
else:
    print("Condition is false!")

What’s Next? Python Loops! In our next post, we’ll learn about loops - how to make Python repeat tasks for you! We’ll cover:

for loops for repeating tasks a specific number of times

while loops for repeating until a condition changes

Building a number guessing game with unlimited tries

Automating repetitive tasks

Your Mission Create a “Restaurant Order System” that:

Asks for the customer’s age

Asks what they want to order

Suggests drinks based on age (alcohol for 21+, soda for others)

Applies discounts for seniors (65+)

Shows a custom message based on their order

Bonus: Add a loyalty program check and apply points!

Wrapping Up Today you learned how to make your Python programs smart! You can now:

โœ… Use if, elif, and else statements

โœ… Compare values using operators

โœ… Combine conditions with and, or, not

โœ… Build interactive programs that make decisions

โœ… Avoid common beginner mistakes

Remember: Programming is about solving real problems. Every time you write an if/else statement, you’re teaching your program to think!

Practice makes perfect - try modifying the examples, break them, fix them, and create your own decision-making programs!

Happy coding! ๐Ÿ

Stuck on any of the examples? Have a cool if/else project you built? Share in the comments below! I’d love to see what you create.

Comments & Discussion

Join the conversation using your GitHub account. Comments are powered by Utterances.

ad ad