Python Basics — if/elif/else

2 minute read

if/elif/else

The if statement is Python’s primary way to make decisions. It checks a condition and runs a block of code only when the condition is True.

Basic if Statement

temperature = 35

if temperature > 30:
    print("It's hot outside!")

Output:

It's hot outside!

The indented block runs only if the condition is True. If the condition is False, Python skips it entirely.

temperature = 20

if temperature > 30:
    print("It's hot outside!")

print("Program continues...")

Output:

Program continues...

if-else

When you need two branches — one for True and one for False:

age = 15

if age >= 18:
    print("You can vote.")
else:
    print("You cannot vote yet.")

Output:

You cannot vote yet.

Think of it like a fork in the road: Python always takes exactly one path.

if-elif-else Chains

When you have multiple conditions to check:

score = 85

if score >= 90:
    grade = "A"
elif score >= 80:
    grade = "B"
elif score >= 70:
    grade = "C"
elif score >= 60:
    grade = "D"
else:
    grade = "F"

print(f"Your grade: {grade}")

Output:

Your grade: B

Key points:

  • Python checks conditions from top to bottom
  • It runs the first matching block and skips the rest
  • else catches everything that didn’t match
  • else is optional

Multiple Conditions

Use and, or, and not to combine conditions:

age = 25
has_license = True

if age >= 18 and has_license:
    print("You can drive.")

Output:

You can drive.
day = "Saturday"

if day == "Saturday" or day == "Sunday":
    print("Weekend!")
else:
    print("Weekday.")

Output:

Weekend!
is_raining = False

if not is_raining:
    print("No umbrella needed.")

Output:

No umbrella needed.

Real-World Examples

Grade Calculator

score = int(input("Enter your score (0-100): "))

if score < 0 or score > 100:
    print("Invalid score!")
elif score >= 90:
    print(f"Score: {score} → Grade: A")
elif score >= 80:
    print(f"Score: {score} → Grade: B")
elif score >= 70:
    print(f"Score: {score} → Grade: C")
elif score >= 60:
    print(f"Score: {score} → Grade: D")
else:
    print(f"Score: {score} → Grade: F")

Simple Login System

stored_username = "admin"
stored_password = "secret123"

username = input("Username: ")
password = input("Password: ")

if username == stored_username and password == stored_password:
    print("Login successful! Welcome.")
elif username != stored_username:
    print("Username not found.")
else:
    print("Incorrect password.")

Leap Year Checker

year = int(input("Enter a year: "))

if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
    print(f"{year} is a leap year.")
else:
    print(f"{year} is not a leap year.")

Common Mistakes

Mistake 1: Using = Instead of ==

# Wrong — this is assignment, not comparison
# if x = 5:  # SyntaxError

# Correct
x = 5
if x == 5:
    print("x is 5")

Mistake 2: Forgetting the Colon

# Wrong
# if x > 5   # SyntaxError: expected ':'

# Correct
if x > 5:
    print("x is greater than 5")

Mistake 3: Wrong Indentation

# Wrong — IndentationError
# if x > 5:
# print("x is greater than 5")

# Correct
if x > 5:
    print("x is greater than 5")

Mistake 4: Redundant Comparisons

is_valid = True

# Redundant — don't do this
if is_valid == True:
    print("Valid")

# Pythonic — do this instead
if is_valid:
    print("Valid")

Summary

  • if runs code when a condition is True
  • else provides a fallback when all conditions are False
  • elif lets you check multiple conditions in sequence
  • Python checks conditions top-to-bottom, runs the first match
  • Use and, or, not to combine conditions
  • Always use == for comparison, not =

Next up: Nested Conditions →

Updated: