100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Python

if-elif-else in Python

Learn how Python's if, elif, and else statements let your program make decisions based on conditions.

Control FlowBeginner8 min readJul 7, 2026
Analogies

1. Introduction

Conditional statements let a program choose between different paths of execution based on whether a condition is True or False. Python provides the if, elif (short for 'else if'), and else keywords to build these decisions. Unlike many languages, Python does not use braces {} to mark blocks — it relies entirely on consistent indentation to define which statements belong to which branch.

🏏

Cricket analogy: A captain's field-setting decision tree - if the batsman is a lefty, if not then check if new ball, else default field - mirrors if/elif/else, and just as the umpire enforces the crease line strictly, Python enforces indentation instead of braces.

You can chain as many elif clauses as needed between if and else, and Python evaluates them top to bottom, executing the first block whose condition is True and skipping the rest.

🏏

Cricket analogy: A DRS review checks conditions top to bottom - first LBW, then edge, then bounce - stopping at the first confirmed reason, just as Python evaluates elif clauses in order and executes only the first True branch, skipping the rest.

2. Syntax

python
if condition1:
    # runs if condition1 is True
    statement(s)
elif condition2:
    # runs if condition1 is False and condition2 is True
    statement(s)
elif condition3:
    # additional elif branches are optional
    statement(s)
else:
    # runs if none of the above conditions were True
    statement(s)

3. Explanation

Each condition is any expression that Python can evaluate as truthy or falsy — comparisons like x > 5, boolean values, or even objects (an empty list or string is falsy, a non-empty one is truthy). The elif and else parts are optional: you can have a bare if, an if/else pair, or an if with several elif branches and a final else. Only the block belonging to the first True condition executes; Python does not check the remaining branches once a match is found.

🏏

Cricket analogy: A selector's truthy check - does the player have any recent form? an empty run tally is falsy, a string of fifties is truthy - decides selection, and once a batsman is picked no other candidate is considered, like Python stopping after the first True branch.

Indentation is not just style in Python — it is syntax. All statements inside a branch must be indented by the same amount (typically 4 spaces), and mismatched indentation raises an IndentationError.

🏏

Cricket analogy: Just as every fielder must stand exactly within the boundary rope or the shot is ruled differently, every line inside a Python branch must be indented by the exact same amount, or an IndentationError is raised.

Python has no traditional switch/case statement. Long elif chains are the classic replacement. Since Python 3.10, the match-case statement offers structural pattern matching as a cleaner alternative for matching against multiple discrete values, but plain if-elif-else remains the most common and portable approach.

Remember to use == for equality comparison, not a single =. Writing 'if x = 5:' is a syntax error in Python, which helps catch this common mistake early — unlike C, Python does not allow assignment inside a condition by accident.

4. Example

python
def classify_temperature(celsius):
    if celsius <= 0:
        return "Freezing"
    elif celsius < 15:
        return "Cold"
    elif celsius < 25:
        return "Mild"
    else:
        return "Hot"

readings = [-5, 10, 20, 30]
for temp in readings:
    print(f"{temp}C -> {classify_temperature(temp)}")

5. Output

text
-5C -> Freezing
10C -> Cold
20C -> Mild
30C -> Hot

6. Key Takeaways

  • if, elif, and else form Python's primary decision-making construct.
  • Indentation (not braces) defines which statements belong to a branch.
  • Only the first branch whose condition is True runs; the rest are skipped.
  • elif and else are both optional, but else must come last if present.
  • Python has no switch/case; long elif chains or match-case (3.10+) serve that role.
  • Use == for comparison — a single = inside a condition is a syntax error.

Practice what you learned

Was this page helpful?

Topics covered

#Python#PythonProgrammingStudyNotes#Programming#IfElifElseInPython#Elif#Else#Syntax#Explanation#StudyNotes#SkillVeris