01 / 20

If / Elif / Else
Statements in Python

Learn to write conditional logic that lets your programs make decisions based on Boolean expressions and proper indentation.

Python

What Are Conditional Statements?

Every meaningful program needs to make decisions. Conditional statements let Python choose which block of code to run based on whether a condition is True or False.

Boolean Expressions: The Foundation

Every condition evaluates to a Boolean value: either True or False. Python provides comparison and logical operators to build these expressions.

Comparison
==  !=  <  >  <=  >=
Logical
and  or  not
Membership
in  not in

Anatomy of an if Statement

if age >= 18 : keyword condition (Boolean) colon print("You can vote!") 4 spaces Three required parts: 1. Keyword (if, elif, or else) 2. Condition (expression that evaluates to True/False) 3. Colon

The Full if / elif / else Chain

if score >= 90: grade = "A" elif score >= 80: grade = "B" elif score >= 70: grade = "C" else: grade = "F"

Decision Flow: How Python Evaluates

Start score >= 90? (if) True grade = "A" False score >= 80? (elif) True grade = "B" False else: grade = "F" Continue program...

Key Distinction: Chained vs. Separate

if / elif / else
Mutually exclusive: only one block runs. Python stops checking after the first True condition. Use when choices don't overlap.
Multiple separate ifs
Independent checks: every if is evaluated regardless of the others. Multiple blocks can run. Use when conditions are unrelated.

Why Indentation Matters

Unlike most languages, Python uses indentation (whitespace) to define code blocks. Incorrect indentation causes IndentationError or, worse, silent logic bugs.

1
Use 4 spaces per indentation level (PEP 8 standard). Never mix tabs and spaces.
2
Every line inside an if/elif/else block must be indented to the same level.
3
De-indent to return to the outer scope. The first un-indented line ends the block.
4
Nested ifs get an additional 4 spaces for each level of nesting.

Step-by-Step: Writing Conditionals

1
Identify the decision. What question does the program need to answer? (e.g., "Is the user old enough?")
2
Write the Boolean expression. Translate the question into a comparison: age >= 18.
3
Structure the branches. Start with if, add elif for extra cases, end with else as a catch-all.
4
Indent the body. Each block gets exactly 4 spaces. End every header line with a colon.
5
Test edge cases. Check boundary values (e.g., exactly 18) and the else path.

Example 1: Even or Odd

number = 7 if number % 2 == 0: print("Even") else: print("Odd")

Example 2: Temperature Advice

temp = 35 if temp >= 100: print("Stay inside!") elif temp >= 70: print("Nice day!") elif temp >= 40: print("Grab a jacket.") else: print("Bundle up!")

Example 3: Nested Conditionals

age = 20 has_id = True if age >= 18: if has_id: print("Entry allowed") else: print("Show your ID") else: print("Too young")

Combining Conditions with Logical Operators

# Using 'and' - both must be True if age >= 13 and age <= 19: print("Teenager") # Using 'or' - at least one must be True if day == "Sat" or day == "Sun": print("Weekend!") # Using 'not' - inverts the Boolean if not logged_in: print("Please sign in")

Common Mistakes to Avoid

✕ Missing Colon
Writing if x > 5 without : at the end causes a SyntaxError. Every if, elif, and else line must end with a colon.
✕ Wrong Operator
Using = (assignment) instead of == (comparison) in a condition. Python will raise a SyntaxError inside an if.
✕ Bad Indentation
Inconsistent spacing (3 spaces, then 4) causes IndentationError. Always use exactly 4 spaces per level throughout your code.

Quick Reference Table

Keyword Purpose Condition? Required?
if First check in a chain Yes Yes (always)
elif Additional check(s) Yes No (0 or more)
else Catch-all fallback No No (0 or 1)

Question 1 of 5

What will the following code print?

x = 15
if x > 20:
    print("A")
elif x > 10:
    print("B")
elif x > 5:
    print("C")
else:
    print("D")

Question 2 of 5

Which line will cause a SyntaxError?

Question 3 of 5

How many spaces does PEP 8 recommend for each indentation level in Python?

Question 4 of 5

What is printed when this code runs?

val = 50
if val > 40:
    print("X")
if val > 30:
    print("Y")
if val > 20:
    print("Z")

Question 5 of 5

What keyword completes this code so it prints "child" when age is under 13?

if age >= 18:
    print("adult")
____ age >= 13:
    print("teen")
____:
    print("child")