Conditions and Boolean expressions
A Boolean expression is either true or false; an if statement uses that result to select a block.
Learning objectives
- Write an if/elif/else chain.
- Trace which branch runs for an input.
Choose one path
PYTHON
marks = 72
if marks >= 90:
result = "excellent"
elif marks >= 40:
result = "pass"
else:
result = "try again"For 72, the first test is false and the second is true, so result becomes "pass". Later branches are skipped once a true condition is found.
Common mistakes
- Use
==to compare values;=is assignment. - Order overlapping tests from the most restrictive condition to the broadest one.
- Indent each suite consistently.