Concepts / Comparison Operators in Python

Comparison Operators in Python

An if-else statement creates two mutually exclusive branches: one executes if the condition is true, the other if it is false.

  • Programming

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.

is evaluated withagainstproducesValue Afirst valueTrue or Falsecondition resultComparison==, <, or >Value Bsecond value
How does a comparison produce the Boolean condition that selects an if-else branch?

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.

TrueFalsethenthenConditionTrue or Falseif branchcondition is TrueCode after if-elsecontinues after selectionelse branchcondition is False
What happens next when the condition is true versus false, and why does only one branch execute?
python
Output
pass

Tracing a Branch

What do you think happens?

The value of temperature is 18. Which message will this program print?

  • warm
  • cool
Reveal answer

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")

Falseselectsthenthentemperature > 20Falseif branchskippedprint(message)coolprint(Finished)after if-elsemessage = coolexecutes
How does control flow move from evaluating the condition to the selected branch and then to the code after the if-else block?

Writing 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.

python

Mistakes That Change the Path

  • Leaving out the colon after if or else.

    The if and else parts of the structure require a colon.

    Fix: Write if score > 50: and include a colon after else as well.

  • Using inconsistent indentation inside the branches.

    Both blocks must be indented consistently so Python can identify the statements belonging to each branch.

    Fix: Use consistent indentation for the statements under if and else.

  • Tracing both branches instead of selecting one.

    The two branches are mutually exclusive. Exactly one branch runs.

    Fix: Evaluate the condition first, then follow only the branch matching the result.

  • Forgetting that execution continues after the if-else block.

    After the selected branch finishes, Python continues with code written after the if-else statement.

    Fix: Trace the selected branch and then inspect the statements that follow the entire block.

Try the Trace

EASY

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.

Evaluate: The condition points > 60 is false because points has the value 40.

Select: The if branch is skipped, and the else branch executes.

Continue: The else branch assigns not high to status, and execution then reaches both print statements.

not high Checked

Key Takeaways

  1. A comparison condition produces a true or false result used to make a decision.
  2. An if-else statement creates two mutually exclusive branches.
  3. Exactly one branch executes: the if branch for a true condition or the else branch for a false condition.
  4. Trace an if-else statement by evaluating the condition first, following the selected branch, and then continuing after the block.
  5. A colon is required after if and else, and both branches must use consistent indentation.

Key Takeaways

  • 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.