Concepts / Comparison Operators in Python

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.

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

Key point

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.

Value Afirst valueTrue or Falsecondition resultComparison==, <, or >Value Bsecond valueis evaluated withagainstproduces
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.

ConditionTrue or Falseif branchcondition is TrueCode after if-elsecontinues after selectionelse branchcondition is FalseTrueFalsethenthen
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.

Code walkthrough
temperature = 18

if temperature > 20:
    message = "warm"
else:
    message = "cool"

print(message)
print("Finished")
  1. 1
    First, temperature has the value 18.
  2. 2
    Next, Python evaluates the condition temperature > 20.
  3. 3
    Because the condition is false, Python does not execute the indented statement under if.
  4. 4
    Python executes the indented statement under else instead.
  5. 5
    Python 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.
Output
cool
Finished
temperature > 20Falseif branchskippedprint(message)coolprint(Finished)after if-elsemessage = coolexecutesFalseselectsthenthen
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

  • Common mistake

    Leaving out the colon after if or else.

    What it looks like
    if score > 50
        result = "pass"
    Why it fails

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

    Do this instead

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

  • Common mistake

    Using inconsistent indentation inside the branches.

    What it looks like
    if score > 50:
      result = "pass"
    else:
        result = "try again"
    Why it fails

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

    Do this instead

    Use consistent indentation for the statements under if and else.

  • Common mistake

    Tracing both branches instead of selecting one.

    What it looks like

    Treating both result assignments as if they execute for the same condition.

    Why it fails

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

    Do this instead

    Evaluate 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 like

    Assuming the selected branch is the final action in the program.

    Why it fails

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

    Do this instead

    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.
Worked example

Tracing the False Path

Determine the output when points is 40.

  1. 1
    Evaluate

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

  2. 2
    Select

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

  3. 3
    Continue

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

Result

not highChecked

Key Takeaways

  1. 1
    A comparison condition produces a true or false result used to make a decision.
  2. 2
    An if-else statement creates two mutually exclusive branches.
  3. 3
    Exactly one branch executes: the if branch for a true condition or the else branch for a false condition.
  4. 4
    Trace an if-else statement by evaluating the condition first, following the selected branch, and then continuing after the block.
  5. 5
    A 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.

Related concepts

More concepts starting with C