Comparison Operators in Python
In Python, an if-else statement provides two mutually exclusive branches: one branch runs when the condition is true, and the other runs when the condition is false.
The Decision Point
Programs often need to respond differently depending on whether a condition is true or false. In Python, an if-else statement provides two mutually exclusive branches: one branch runs when the condition is true, and the other runs when the condition is false. This is called alternative execution.
An if-else statement always selects exactly one of its two branches because the condition must be either true or false.
Conditions Become Decisions
A comparison condition evaluates to a Boolean result: true or false. That result controls which branch Python follows. For example, a condition can compare two values with an operator such as ==, <, or >. The important control-flow question is not which comparison operator was used, but whether the completed condition is true or false.
Two Mutually Exclusive Paths
After Python evaluates the condition, control moves into only the matching branch. The true branch runs when the condition is true. The else branch runs when the condition is false. Python does not run both branches during the same evaluation of the if-else statement.
passTracing a Branch
What do you think happens?
The value of temperature is 18. Which message will this program print?
Reveal answer
cool
Python evaluates temperature > 20 first. With temperature equal to 18, the condition is false, so the else branch runs. The message after the if-else statement is then printed.
temperature = 18
if temperature > 20:
message = "warm"
else:
message = "cool"
print(message)
print("Finished")- 1First, temperature has the value 18.
- 2Next, Python evaluates the condition temperature > 20.
- 3Because the condition is false, Python does not execute the indented statement under if.
- 4Python executes the indented statement under else instead.
- 5Python then reaches the code after the if-else block and prints the selected message followed by Finished.
- The value 18 is assigned to temperature.
- Python evaluates temperature > 20.
- The condition is false, so the if branch is skipped.
- The else branch assigns cool to message.
- Execution continues with the code after the if-else statement.
cool
FinishedWriting the Structure Correctly
An if-else statement requires a colon after if and after else. The statements belonging to each branch must be indented consistently. The indentation shows which statements belong to the if branch and which belong to the else branch.
Mistakes That Change the Path
Common mistake Leaving out the colon after if or else.
What it looks likeif score > 50 result = "pass"Why it failsThe if and else parts of the structure require a colon.
Do this insteadWrite if score > 50: and include a colon after else as well.
Common mistake Using inconsistent indentation inside the branches.
What it looks likeif score > 50: result = "pass" else: result = "try again"Why it failsBoth blocks must be indented consistently so Python can identify the statements belonging to each branch.
Do this insteadUse consistent indentation for the statements under if and else.
Common mistake Tracing both branches instead of selecting one.
What it looks likeTreating both result assignments as if they execute for the same condition.
Why it failsThe two branches are mutually exclusive. Exactly one branch runs.
Do this insteadEvaluate the condition first, then follow only the branch matching the result.
Common mistake Forgetting that execution continues after the if-else block.
What it looks likeAssuming the selected branch is the final action in the program.
Why it failsAfter the selected branch finishes, Python continues with code written after the if-else statement.
Do this insteadTrace the selected branch and then inspect the statements that follow the entire block.
Try the Trace
Trace this program without running it. Decide which branch executes and write the two lines of output.
points = 40
if points > 60:
status = "high"
else:
status = "not high"print(status)
print("Checked")Hints
- Evaluate points > 60 using points equal to 40.
- Only one branch assigns a value to status.
- After the selected branch, trace both print statements.
Tracing the False Path
Determine the output when points is 40.
- 1Evaluate
The condition points > 60 is false because points has the value 40.
- 2Select
The if branch is skipped, and the else branch executes.
- 3Continue
The else branch assigns not high to status, and execution then reaches both print statements.
not highChecked
Key Takeaways
- 1A comparison condition produces a true or false result used to make a decision.
- 2An if-else statement creates two mutually exclusive branches.
- 3Exactly one branch executes: the if branch for a true condition or the else branch for a false condition.
- 4Trace an if-else statement by evaluating the condition first, following the selected branch, and then continuing after the block.
- 5A colon is required after if and else, and both branches must use consistent indentation.
At a glance
- Comparison conditions resolve to true or false.
- If-else provides alternative execution through two mutually exclusive branches.
- Exactly one branch runs for each evaluation.
- Correct tracing requires evaluating the condition before following a branch.
- Colons and consistent indentation are required parts of the if-else structure.