If-Else Statements in Python

    Kedar Kabra3 min readUpdated

    An if-else statement lets your program make decisions. It runs one block of code when a condition is True and another when it's False:

    age = 17
    
    if age >= 18:
        print("You can vote.")
    else:
        print("You are not old enough to vote yet.")
    

    Because age is 17, the condition age >= 18 is False, so the else block runs. Let's break down how this works.

    The basic structure

    An if statement has three parts:

    1. The keyword if, a condition, and a colon :.
    2. An indented block that runs when the condition is True.
    3. An optional else: with its own indented block for when the condition is False.
    temperature = 38
    
    if temperature > 37:
        # This indented line only runs if the condition is True
        print("You may have a fever.")
    else:
        print("Temperature looks normal.")
    

    The indentation (usually 4 spaces) is not optional in Python — it's how Python knows which lines belong to the if.

    Checking multiple conditions with elif

    When you have more than two outcomes, use elif (short for "else if"). Python checks each condition top to bottom and runs the first one that is True:

    marks = 72
    
    if marks >= 90:
        grade = "A"
    elif marks >= 75:
        grade = "B"
    elif marks >= 60:
        grade = "C"        # 72 lands here
    else:
        grade = "Fail"
    
    print(f"Your grade is {grade}")
    

    Once a branch matches, Python skips the rest. You can have as many elif branches as you need, but only one else at the end.

    Combining conditions

    You can use and, or, and not to build richer conditions:

    age = 20
    has_ticket = True
    

    Want to learn this properly?

    Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.

    Browse courses

    Both must be True for entry

    if age >= 18 and has_ticket: print("Welcome in!") else: print("Sorry, you can't enter.")

    
    ## Nested if statements
    
    You can put an `if` inside another `if` when one decision depends on another. Keep nesting shallow though — too many levels get hard to read.
    
    ```python
    logged_in = True
    is_admin = False
    
    if logged_in:
        if is_admin:
            print("Showing admin dashboard")
        else:
            print("Showing user dashboard")
    else:
        print("Please log in first")
    

    A complete example

    # A simple number classifier
    
    num = int(input("Enter a number: "))   # convert text to int
    
    if num > 0:
        print("Positive number")
    elif num < 0:
        print("Negative number")
    else:
        print("You entered zero")
    
    # Bonus: check even or odd using modulo
    if num % 2 == 0:
        print("It is even")
    else:
        print("It is odd")
    

    Common mistakes

    • Forgetting the colon: Every if, elif, and else line must end with :. Missing it causes a SyntaxError.
    • Wrong indentation: Mixing tabs and spaces, or indenting inconsistently, causes an IndentationError. Pick 4 spaces and stick to it.
    • Using = instead of ==: if x = 5: is an error. Comparing needs ==.
    • Putting elif/else without an if: They must always follow an if in the same block.
    • Expecting multiple branches to run: Only the first matching branch in an if-elif-else chain runs. If you want independent checks, use separate if statements.

    FAQ

    Can I write an if without an else? Yes. The else is optional. An if alone just does nothing when the condition is False.

    Is there a switch/case statement? As of Python 3.10+ there is match/case for pattern matching, but for simple checks, if/elif/else is perfectly fine and most common.

    Can I write a one-line if? Yes, using a conditional expression: status = "adult" if age >= 18 else "minor". Use it for simple cases only.


    Decisions often go hand-in-hand with repetition. Learn Loops in Python next, and revisit Operators in Python to sharpen your conditions. More topics on the Python learning hub.

    Want to learn this properly? Join the waitlist for our Python course — taught in Jalgaon, beginner-friendly.

    Want to learn this properly?

    Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.

    Browse courses
    Kedar Kabra

    Instructor, Infoplanet

    Kedar Kabra teaches Python at Infoplanet, helping beginners become confident programmers through hands-on, project-first practice.

    Related guides