← Back to Learn
🤔

Beginner â€ĸ Python

If / Else

Lesson 1 of 7
Step 1

🤔 Making Decisions

An if statement allows Python to run some code only when a condition is True. Conditions help programs make decisions based on information.

🐍 Python
age = 12

if age >= 10:
    print("You can play!")
â–ļ Output
You can play!
⭐ Earn 10 XP
🧩 Want to practice? Try the If / Else exercises →

📚 Everything in If / Else

Teach your computer how to make decisions. Here is every step in this topic in one place, so you can review what you learned or jump straight to a step.

  1. 🤔 Making Decisions

    An if statement allows Python to run some code only when a condition is True. Conditions help programs make decisions based on information.

    age = 12
    
    if age >= 10:
        print("You can play!")
  2. 🔀 If and Else

    The else block runs when the if condition is False. This allows a program to choose between two different actions.

    age = 7
    
    if age >= 10:
        print("You can play!")
    else:
        print("You are too young!")
  3. âš–ī¸ Using Comparison Operators

    Conditions often use comparison operators such as >, <, ==, >=, and <=. Python checks the comparison and gets either True or False.

    score = 80
    
    if score >= 50:
        print("You passed!")
    else:
        print("Try again!")
  4. 🔀 Multiple Choices with elif

    The elif keyword allows a program to check another condition when the previous if condition was False. You can use multiple elif blocks when there are several possible choices.

    score = 85
    
    if score >= 90:
        print("Excellent!")
    elif score >= 50:
        print("Good job!")
    else:
        print("Keep practicing!")
  5. 🔗 Using and / or in Conditions

    Sometimes a decision depends on more than one thing. We can use and when both conditions must be True, and or when only one of them needs to be True. This lets us write smarter conditions in a single if statement.

    age = 12
    has_ticket = True
    
    if age >= 10 and has_ticket:
        print("Enjoy the ride!")
    else:
        print("Sorry, you cannot ride.")
    
    day = "Sunday"
    
    if day == "Saturday" or day == "Sunday":
        print("It's the weekend!")
  6. 🔁 Nested if Statements

    An if statement can be placed inside another if statement. This is called nesting. The inner if is only checked when the outer condition is True. Remember to indent the inner block a little further so Python knows which code belongs to which if.

    is_weekend = True
    is_sunny = False
    
    if is_weekend:
        print("No school today!")
        if is_sunny:
            print("Let's go to the park!")
        else:
            print("Let's play board games!")
  7. 🏆 Mini Grade Checker

    We can use if, elif, and else together to build a grade checker. Python checks each condition from top to bottom and runs the first block whose condition is True. Once one block runs, Python skips the rest.

    score = 72
    
    if score >= 90:
        print("Grade: A")
    elif score >= 75:
        print("Grade: B")
    elif score >= 60:
        print("Grade: C")
    else:
        print("Grade: D")