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
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
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
-5C -> Freezing
10C -> Cold
20C -> Mild
30C -> Hot6. 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
1. What happens if none of the conditions in an if-elif chain (with no else) are True?
2. Which symbol is used to define a block under an if statement in Python?
3. What is the output of: x = 0; print('yes' if x else 'no')
4. How many elif clauses can appear between a single if and an optional else?
5. Since which Python version is the match-case statement available as an alternative to long if-elif chains?
6. What error does inconsistent indentation inside an if block raise?
Was this page helpful?
You May Also Like
for Loop in Python
Understand how Python's for loop iterates over sequences and iterables like lists, strings, and ranges.
while Loop in Python
Learn how Python's while loop repeats code as long as a condition stays True, and how to avoid infinite loops.
Operators in Python
An overview of Python's arithmetic, comparison, logical, assignment, bitwise, identity, and membership operators with precedence rules.
break, continue and pass in Python
Master Python's three loop control statements: break to exit early, continue to skip an iteration, and pass as a no-op placeholder.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics