Conditions and branches
A comparison gives an answer; a conditional uses that answer to decide which code to execute. This lets one program handle several situations without writing several separate programs.
The shape of an if statement
An if statement ends its condition with a colon :. The indented body runs only when the condition is true. Python treats indentation as part of the program structure, not as a cosmetic choice.
Four spaces per indentation level is a common convention. Keep all statements belonging to the same branch aligned. The code following the whole if block runs regardless of which branch was taken.
elif and else make exclusive choices
An elif (else-if) branch is tested only if all earlier conditions in the same chain failed. else handles everything not already matched and has no condition.
Order matters. Test a higher threshold before a lower one if both could match. For example, a mark of 95 also satisfies mark >= 50; if the passing branch comes first, the distinction between pass and excellent disappears.
Independent checks are different
Two separate if statements are both examined. An if/elif/else chain selects at most one branch. Neither form is universally better: use a chain for mutually exclusive outcomes and separate conditions for independent facts.
A condition can refer to multiple comparisons using and, or, not, or chained comparisons. Parentheses can make the intent obvious.
Design for boundary values
When you write a rule, test a value below its boundary, a value on the boundary and a value above it. That exposes off-by-one errors in ranges and discounts.
Before offering a catch-all else, decide whether unexpected values should receive a normal result or be flagged as invalid. The browser playground shows uncaught mistakes so you can learn to diagnose them.
Try it yourself.
Choose a starting example or write something completely different. You control the code; the Run button executes what is in the editor, not a hidden preset.
Run a block only when the price exceeds a limit.
Press Run Python to execute the editable code.
Change price to 25. Which line continues to print?
Need a hint?
The second print is not inside the if block.
On first Run, a pinned Python runtime downloads from jsDelivr. Code executes locally in your browser's Web Worker, not on our server. It cannot inspect your normal PC files or Windows hardware. Avoid entering secrets. Heavy/infinite loops are stopped by a time limit.
Check your understanding.
Try answering each question before opening its reference answer. Explain the idea aloud or change an example to test it for yourself.
When should you use two separate
ifstatements instead ofiffollowed byelif?View answer
Use separate
ifstatements when two facts are independent and both actions may need to run, such as “balance is low” and “balance is still positive”. Useif/elif/elsefor mutually exclusive choices where only one result should be selected. An earlier match prevents laterelifbranches running.Why do Python conditions need correctly aligned indentation?
View answer
Indentation tells Python which statements belong inside a branch or loop. An incorrectly aligned
print()may run every time rather than only when the condition matches, or Python may raiseIndentationError. A consistent four-space indent makes the program structure both correct and readable.
Where this leads.
Conditions choose actions once. Loops let us repeat an action across many values.