Write Simple if Statements
An if-else statement selects exactly one of those blocks.
Try it: if / elif / else
How Python tests if / elif conditions from the top, runs only the first true branch, and how and / or stop evaluating as soon as the answer is known (short-circuiting).
How it works
- Python evaluates the if condition first.
- If it is False, it tries each elif in order; the first True one runs and the rest are skipped without being tested.
- If none is True, the else branch runs.
- and stops at the first False, or stops at the first True — the right side may never be evaluated.
Default run (5 steps): score = 84; A = 90, B = 80, C = 70. Python tests the conditions from the top. … print(grade) shows B.
Loading the simulation…
Two Possible Paths
Many programs need to respond differently depending on a condition. Alternative execution handles this situation by providing two possible blocks of code: one block for when a condition is true and another block for when it is false. An if-else statement selects exactly one of those blocks.
A First Execution Trace
number = 7
if number % 2 == 0:
result = "even"
else:
result = "odd"
print(result)- 1Python evaluates number % 2 == 0 first.
- 2The condition is False for the value 7.
- 3Because the condition is false, Python does not execute the indented if block.
- 4Python executes the indented else block instead.
- 5Execution continues to the print statement after the if-else statement.
- The variable number has the value 7.
- The condition checks whether the remainder from dividing number by 2 equals 0.
- The condition is False for 7, so the if block is skipped.
- The else block assigns the value odd to result.
- The print statement displays odd.
oddHow Python Chooses
When Python encounters an if-else statement, it evaluates the condition first. A condition is a boolean expression, so its result is either True or False. If the result is True, Python executes the indented code under if and skips the else block. If the result is False, Python skips the if block and executes the indented code under else. Exactly one block runs: never both and never neither.
Writing the Structure
The condition follows if, and a colon must follow the condition. The statements controlled by if must be indented. The else line also requires a colon, and the statements controlled by else must be indented. The two indented regions are the two alternative blocks.
Checking an Unexpected Result
temperature = 20
if temperature < 10:
message = "cold"
else:
message = "not cold"
print(message)Expected the cold branch to run.
temperature = 20
if temperature < 10:
message = "cold"
else:
message = "not cold"
print(message)The condition 20 < 10 is False, so the else branch runs and the output is not cold.
Debugging an if statement begins at the condition. Compare the actual input with the condition and decide whether the result is True or False. Then check whether the program follows the corresponding branch. If the branch is correct but the result still seems wrong, inspect the statements inside that indented block.
Common mistake Leaving out the colon after the condition.
What it looks likeif score >= 60Why it failsThe if statement requires a colon after its condition.
Do this insteadWrite if score >= 60:Common mistake Leaving out the colon after else.
What it looks likeelse
Why it failsThe else branch also requires a colon before its indented block.
Do this insteadWrite else:
Common mistake Failing to indent the code controlled by the condition.
What it looks likeif score >= 60: print("pass")Why it failsThe code that belongs to the if block must be indented.
Do this insteadIndent the statement under if.
Common mistake Assuming both branches execute.
What it looks likeA condition is True, but expecting both the if and else messages.
Why it failsAn if-else statement executes exactly one of its two blocks.
Do this insteadEvaluate the condition and follow only the matching branch.
Branch Prediction Practice
What do you think happens?
Which message is printed when points has the value 42?
Reveal answer
high
The condition points >= 40 is True for 42. Python executes the if block and skips the else block.
Trace the code before running it. Write down the condition, decide whether it is True or False, identify the branch that executes, and state what the program prints.
Hints
- Start with the value of points.
- Evaluate points >= 40.
- Only one indented block executes.
Key Takeaways
- An if-else statement provides two alternative execution paths.
- Python evaluates the condition first and uses its True or False result to select a branch.
- Exactly one block executes in an if-else statement; the other block is skipped.
- A correct statement needs a colon after the if condition and after else, plus proper indentation for each block.
- To debug unexpected behavior, evaluate the condition with the actual input and trace the selected branch.