Python Conditional Statements: Mastering if, elif, and else
Python Conditional Statements: Mastering if, elif, and else
How do Python programs make decisions? Conditional statements allow programs to execute different instructions depending on whether a condition is true or false. In this beginner-friendly lesson, you will learn how to use if, elif, and else, combine conditions with logical operators, understand nested conditional statements, and avoid common programming mistakes. Explore practical examples, interactive decision-making activities, and step-by-step coding exercises to develop your Python programming skills.

Learning objectives
- Understand the purpose of conditional statements in Python.
- Explain how programs make decisions using Boolean conditions.
- Write simple if statements.
- Use if and else to choose between two actions.
- Use elif to evaluate multiple alternatives.
- Understand the order of condition evaluation.
- Identify the first matching branch in a conditional chain.
- Combine conditions using and, or, and not.
- Use comparison operators inside conditional statements.
- Distinguish independent if statements from an if-elif-else chain.
- Create and interpret nested conditional statements.
- Use indentation correctly in Python code blocks.
- Test boundary values in conditional expressions.
- Recognize and correct common conditional statement errors.
- Apply conditional statements to practical programming problems.
Introduction
Programs often need to choose between different actions. A quiz can display feedback based on a score, and a game can check whether a player has completed a level. Conditional statements let Python make these decisions by evaluating expressions as true or false.
Learning objectives
- Write if, elif and else statements with correct indentation.
- Combine conditions and check boundaries.
- Trace branches and distinguish independent if statements from a single chain.
Prerequisites: variables, print(), comparison operators and Boolean logic. All examples use Python 3.
1. The if statement
An if statement evaluates a condition. When it is true, Python executes the indented body; otherwise it skips that body. The colon introduces the block. Use four spaces for indentation.
score = 85
if score >= 70:
print("Goal reached")
print("Finished")Since 85 is at least 70, both messages appear. With score = 60, only Finished appears. The last print is outside the block.
2. Choosing between two actions: if and else
The else branch executes when the if condition is false. It has no condition of its own.
number = 7
if number % 2 == 0:
print("Even")
else:
print("Odd")The remainder of 7 divided by 2 is 1, so the equality is false and Python displays Odd. With 8, the remainder is 0 and Python displays Even.
3. Multiple choices: elif
Use elif (else if) to test another condition only when all earlier conditions in the chain were false. Python executes at most one branch. An optional else handles every remaining case.
score = 85
if score >= 90:
print("Excellent")
elif score >= 70:
print("Good")
else:
print("Keep practicing")First, 85 >= 90 is false. Next, 85 >= 70 is true, so Python displays Good and skips else. Assume scores are integers from 0 to 100.
| Score | Output |
|---|---|
| 69 | Keep practicing |
| 70 | Good |
| 89 | Good |
| 90 | Excellent |
Order matters: if the condition score >= 70 came first, a score of 95 would match it and never reach the Excellent branch.
4. Interactive branch explorer
85: Good. First condition false; second condition true.
Try 69, 70, 89 and 90. Predict the branch before moving the slider. This activity simulates the Python example in JavaScript; it is not a Python interpreter.
5. Combining conditions
Use and when both Boolean conditions must be true, or when at least one must be true, and not to reverse a truth value.
score = 75
submitted = True
if score >= 70 and submitted:
print("Goal reached")
else:
print("Check your work")Both conditions are true, so Goal reached appears. If submitted is False, the else branch runs.
day = "Saturday"
if day == "Saturday" or day == "Sunday":
print("Weekend")
completed = False
if not completed:
print("Task pending")The first condition is true because day is Saturday. The second is true because not False is True. Both messages appear.
6. Independent and nested conditions
Separate if statements are evaluated independently. More than one body may execute.
n = 12
if n > 0:
print("Positive")
if n % 2 == 0:
print("Even")Both conditions are true, so both messages appear. A nested if is a condition inside another block.
score = 95
if 0 <= score <= 100:
if score >= 90:
print("Excellent")
else:
print("Valid score")
else:
print("Invalid score")The outer condition validates the range. Only then is the inner condition tested. For 95, the output is Excellent; for 105, it is Invalid score.
7. Common mistakes
- Use == for equality, not =.
- End if, elif and else headers with a colon.
- Indent each body consistently.
- Do not attach a condition to else.
- Check boundary values and order overlapping conditions carefully.
- Do not confuse separate if statements with an if/elif chain.
8. Practice exercises
Exercise 1 — Predict the output
For score = 65, what does the first if example display?
Show solution
Finished only, because 65 >= 70 is false.
Exercise 2 — Even or odd
Use the even/odd example with number = 18.
Show solution
18 % 2 is 0, so Even appears.
Exercise 3 — Boundary score
Predict the feedback for score = 90.
Show solution
Excellent, because the first condition is true.
Exercise 4 — Middle branch
Predict the feedback for score = 72.
Show solution
Good: the first condition is false and the second is true.
Exercise 5 — AND condition
Use score = 80 and submitted = False in section 5.
Show solution
Check your work, because both conditions are not true.
Exercise 6 — OR condition
Use day = "Sunday" in the weekend example.
Show solution
Weekend, because the second comparison is true.
Exercise 7 — NOT condition
Use completed = True in section 5.
Show solution
No Task pending message appears because not True is False.
Exercise 8 — Independent conditions
Use n = 6 in section 6.
Show solution
Positive and Even, because both independent conditions are true.
Exercise 9 — Nested conditions
Use score = -1 in the nested example.
Show solution
Invalid score, because the outer range check is false.
Exercise 10 — Write a program
Write an if/elif/else chain that displays Positive, Zero or Negative for an integer n.
Show solution
if n > 0:
print("Positive")
elif n == 0:
print("Zero")
else:
print("Negative")Key takeaways
Use if to test a condition, elif to test alternatives, and else for remaining cases. A chain executes at most one branch. Separate if statements can execute multiple branches. Always test boundaries and use consistent indentation.
Open the original interactive version
Definitions
Conditional Statement
A programming structure that controls which instructions are executed according to the truth value of a condition.
Condition
An expression evaluated to determine whether it is true or false in a Boolean context.
Boolean Expression
An expression that produces a Boolean value, True or False.
if Statement
A conditional statement that executes its body when its condition is true.
elif Statement
An alternative conditional branch evaluated when all preceding conditions in the same chain are false.
else Statement
An optional branch executed when none of the preceding conditions in the same conditional chain are true.
Branch
One possible path of execution within a conditional structure.
Control Flow
The order in which instructions are executed within a program.
Code Block
A group of instructions belonging to the same programming structure, identified in Python by indentation.
Indentation
Whitespace at the beginning of a line that determines the structure of Python code.
Nested Conditional
A conditional statement located inside another conditional statement.
Logical Operator
An operator such as and, or, or not used to combine or reverse truth values.
Boundary Value
A value at or near a threshold where the outcome of a condition may change.
Independent if Statements
Separate if statements whose conditions are evaluated independently, allowing multiple bodies to execute.
SyntaxError
An exception indicating that Python cannot parse code because it violates the language's syntax rules.
IndentationError
A type of SyntaxError caused by invalid indentation.
Common mistakes to avoid
- Using = instead of == in an equality comparison.
- Forgetting the colon after if, elif, or else.
- Using incorrect or inconsistent indentation.
- Writing else if instead of elif.
- Adding a condition directly after else.
- Writing elif without a preceding if statement.
- Placing an elif branch after an else branch.
- Confusing independent if statements with a single conditional chain.
- Assuming every true condition in an if-elif-else chain will execute.
- Placing overlapping conditions in the wrong order.
- Ignoring boundary values when using comparison operators.
- Confusing and with or.
- Writing true and false instead of True and False.
- Using && or || instead of Python's logical operators.
- Incorrectly indenting nested conditional statements.
- Forgetting to validate values before applying decision rules.
Key takeaways
- Conditional statements allow Python programs to make decisions.
- The if statement executes its body when its condition is true.
- The elif keyword introduces an alternative condition.
- The else branch handles cases not matched by earlier conditions.
- Python evaluates conditional chains from top to bottom.
- At most one branch executes in a single if-elif-else chain.
- Comparison operators help construct conditions.
- The and operator requires both Boolean operands to be true.
- The or operator requires at least one Boolean operand to be true.
- The not operator reverses a truth value.
- Nested conditional statements allow decisions inside other decisions.
- Indentation determines which instructions belong to a block.
- The order of overlapping conditions affects the result.
- Testing boundary values helps identify logical errors.
- Conditional statements are fundamental to algorithms and program control flow.