Lesson 11 of 60
โฎPrev Nextโฏ

PY-101 ยท Conditions

if, elif and else

โฑ 8 min ๐Ÿ Real Python 3 ๐ŸŽฌ Animated

11of 60

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    โšก The 30-second answer

    An if statement runs a block of code only when its condition is True. elif checks another condition when the ones before it were False, and else runs when none of them were. Python checks from the top and runs only the first branch that matches.

    • Write if, elif and else
    • Know that only one branch runs
    • Indent blocks correctly

    Try it

    Watch it come alive, run it yourself, then prove it. Real Python, right on this page.

    Watch Python run the example. Every variable is a jar; watch the values change and the output appear.

    if, elif and else, running
    1. total = 350
    2. if total >= 500:
    3. delivery = 0
    4. elif total >= 200:
    5. delivery = 40
    6. else:
    7. delivery = 80
    8. print("Delivery:", delivery)
    9. print("Pay:", total + delivery)
    Press โ–ถ to watch Python run this, one line at a time.
    1. The problemA shop gives free delivery over โ‚น499, โ‚น40 delivery over โ‚น199, and โ‚น80 below that. Every order needs exactly one of those answers, picked automatically.
    2. The Python ideaif condition:An if statement runs a block of code only when its condition is True.
    3. In real lifeBanks use the same shape to set interest rates by balance band, and HR systems use it to pick tax slabs.

    Start with a question

    A shop gives free delivery over โ‚น499, โ‚น40 delivery over โ‚น199, and โ‚น80 below that. Every order needs exactly one of those answers, picked automatically.

    Every program that reacts to data, from a login screen to a game, makes decisions. if/elif/else is how Python makes them.

    Explain it like I'm 10

    It is like a traffic light: if it is green, go; else if it is yellow, slow down; otherwise stop. You only ever do one of them.

    The technical explanation

    The condition after if is anything that is True or False. A colon ends the line, and the block underneath is indented by four spaces.

    elif means "else if". Python tries each condition in order and stops at the first True one. The rest are skipped.

    else has no condition. It catches everything that did not match. You can leave it out if nothing should happen.

    Syntax

    if condition:
        ...
    elif other_condition:
        ...
    else:
        ...

    โ–ถ Run the example ๐ŸŽฌ Watch it come alive

    Real-world scenario

    Banks use the same shape to set interest rates by balance band, and HR systems use it to pick tax slabs.

    Ready for more?The full lesson adds the technical explanation, interview questions and quick revision.

    Test yourself

    5 questions. Get 4 right and this lesson is marked complete.

    1. 1. How many branches of an if/elif/else chain run?
    2. 2. What ends the if line?
    3. 3. x = 5. What prints? if x > 3: print("big") else: print("small")
    4. 4. When does else run?
    5. 5. Which keyword means "else if" in Python?

    Questions people ask

    What is the difference between if and elif in Python?

    Each if starts a new check that always runs. elif belongs to the if above it and only runs when everything before it in the chain was False.

    Does Python have switch statements?

    Python 3.10 added match/case, which works like a more powerful switch. For simple ranges, if/elif/else is still the usual choice.

    Interview questions

    How this topic comes up in Python interviews. Answer out loud first, then open the model answer.

    beginner What is the difference between if and elif in Python?

    Each if starts a new check that always runs. elif belongs to the if above it and only runs when everything before it in the chain was False.

    beginner Does Python have switch statements?

    Python 3.10 added match/case, which works like a more powerful switch. For simple ranges, if/elif/else is still the usual choice.

    intermediate How many branches of an if/elif/else chain run?

    At most one. Python runs the first matching branch and skips the rest.

    intermediate What ends the if line?

    A colon. Python uses a colon, then an indented block.

    Quick revision

    Remember

    • Write if, elif and else
    • Know that only one branch runs
    • Indent blocks correctly
    • Order matters. Put the most specific condition first: if you check score >= 50 before score >= 90, a score of 95 never reaches the 90 branch.

    Syntax

    if condition:
        ...
    elif other_condition:
        ...
    else:
        ...

    Official documentation