Debugging Comparison Errors
Using = when a comparison is intended is a common and critical error.
Try it: Debug by Tracing Execution
How to debug a program that runs but gives the wrong answer: compare expected and actual output, trace the lines in the order they really run with a variable watch table (or an inserted debug print), find the first line where a value goes wrong, then choose a fix and re-run to check it.
How it works
- State the expected output from the specification, run the program and compare: a difference is the evidence you debug from.
- Trace in execution order, not file order: loops repeat lines, if/elif picks one branch, and the watch table records every variable before each line runs.
- An inserted print('debug: ...', value) shows the same state in the output; it must not change what the program itself prints.
- Look for the first place a value differs from what it should be (an off-by-one range, > instead of >=, a total reset inside the loop, two names for one list, a str from input()). Where the wrong value shows up is only the symptom.
- Change one thing with a reason, re-run, and compare again: a fix that makes an error disappear can still leave the output wrong.
Default run (14 steps): Sum 1..n: Add up the whole numbers from 1 to n (n included) and print the sum. Press Play to trace it line by line. … Finished, but the output is wrong: got sum of 1 to 5 is 10, expected sum of 1 to 5 is 15.
Simplified: The lab's own small Python interpreter runs fixed, hand-written programs (you pick the program, the fix and a value; your text is never executed). Every program the lab can show was run in CPython 3.12 and the lab reproduces its output, its traceback text and its line-by-line trace. Six short programs, one editable value each; input() receives the value you type in the lab. Lines starting with "debug:" are ignored when comparing the output with the expected output.
Loading the simulation…
The Question Behind Every Comparison
When a program compares two operands, it is asking a question about their relationship. The answer is a boolean expression: an expression that evaluates to True or False. True and False are the two possible values of the bool data type.
Suppose a program needs to decide whether a score is at least 50. The comparison asks whether the score meets that condition. Python evaluates the comparison and produces either True or False, which the program can then use as an answer.
Reading Comparison Structure
Every comparison expression has the same basic structure: a left operand, a comparison operator, and a right operand. The operator sits between the two values being compared. Python examines both operands, applies the operator's rule, and produces True or False.
Evaluating a comparison
Determine the result of 12 >= 12.
- 1Identify the operands
The left operand is 12 and the right operand is 12.
- 2Identify the operator
The operator is >=, which allows the left operand to be larger than or equal to the right operand.
- 3Compare the values
The two operands are equal, so the inclusive comparison succeeds.
12 >= 12 evaluates to True.
Assignment Versus Equality
The symbols = and == are not interchangeable in Python. The = operator assigns a value. The == operator compares values and produces a boolean result. Using = when a comparison is intended is a common and critical error.
score = 50The value 50 is assigned.
score == 50The value of score is compared with 50, producing True or False.
The Eight Comparison Operators
Python provides eight operators that compare two operands and produce a boolean result. The first six primarily describe value relationships. The final two test object identity: whether operands refer to the exact same object in memory.
| Operator | Question it asks | Important distinction |
|---|---|---|
| == | Do the operands have equal values? | Value equality |
| != | Do the operands have different values? | Value inequality |
| > | Is the left operand larger than the right? | Exclusive; equality does not qualify |
| < | Is the left operand smaller than the right? | Exclusive; equality does not qualify |
| >= | Is the left operand larger than or equal to the right? | Inclusive; equality qualifies |
| <= | Is the left operand smaller than or equal to the right? | Inclusive; equality qualifies |
| is | Do the operands refer to the exact same object? | Object identity |
| is not | Do the operands refer to different objects? | Object identity |
Python's eight comparison operators and the relationship each one tests.
The distinction between exclusive and inclusive comparisons matters when the operands are equal. The > operator is True only when the left operand is strictly larger. The >= operator is True when the left operand is larger or when both operands are equal. The same pattern applies to < and <=.
Testing equal operands
Compare 7 and 7 using >, >=, <, and <=.
- 1Apply >
7 is not strictly larger than 7, so 7 > 7 is False.
- 2Apply >=
The operands are equal, and >= includes equality, so 7 >= 7 is True.
- 3Apply <
7 is not strictly smaller than 7, so 7 < 7 is False.
- 4Apply <=
The operands are equal, and <= includes equality, so 7 <= 7 is True.
For equal operands, the inclusive comparisons are True and the exclusive comparisons are False.
Equality and Object Identity
The operators == and is look related, but they ask different questions. The == operator compares the values of two operands. The is operator checks whether two operands refer to the exact same object in memory. Therefore, two objects can contain equal values while still being different objects.
Debugging Practice
What do you think happens?
Predict the result of each expression before checking the answers: 9 == 9, 9 != 9, 9 > 9, and 9 >= 9.
Reveal answer
True, False, False, True
The values are equal, so == is True and != is False. The left value is not strictly larger, so > is False. The inclusive operator >= accepts equality, so it is True.
For each expression, identify the operands, identify the operator, and predict whether the result is True or False: 3 < 8; 8 <= 8; 4 != 5; and value == 10 when value has the value 10.
Hints
- Check whether the comparison is exclusive or inclusive.
- For !=, ask whether the values are different.
- For ==, compare values rather than asking whether the operands are the same object.
Common mistake Using = when you mean ==.
What it looks likeWriting score = 50 when the intention is to ask whether score has the value 50.
Why it failsThe = operator assigns a value; it does not compare two values.
Do this insteadUse == when the expression must compare values and produce True or False.Common mistake Treating > as though it includes equality.
What it looks likeExpecting 7 > 7 to be True.
Why it failsThe > operator is exclusive and requires the left operand to be strictly larger.
Do this insteadUse >= when equality should also count.
Common mistake Treating == and is as interchangeable.
What it looks likeUsing is whenever two operands contain equal values.
Why it fails== tests value equality, while is tests whether the operands refer to the exact same object.
Do this insteadChoose == for equal values and is for exact object identity.Common mistake Skipping prediction and checking only the final output.
What it looks likeReading a comparison without identifying its operator or operands.
Why it failsEach operator asks a different question, so overlooking the operator can hide the source of an incorrect result.
Do this insteadRead every comparison as left operand, operator, right operand, then apply the operator's rule.
A Reliable Debugging Routine
- 1Separate assignment from comparison: decide whether the statement stores a value or asks a question.
- 2Identify the left operand and the right operand.
- 3Name the operator exactly: ==, !=, >, <, >=, <=, is, or is not.
- 4Check whether the operator tests values or object identity.
- 5For >, <, >=, and <=, check whether equality is excluded or included.
- 6Predict True or False before executing or inspecting the result.
This routine turns a vague comparison error into a sequence of smaller checks. Most importantly, it makes the operator visible. A comparison cannot be interpreted correctly until you know which question its operator asks.
Key Takeaways
- 1A boolean expression evaluates to True or False, the two values of the bool data type.
- 2The = operator assigns a value, while == compares values.
- 3Python has eight comparison operators: ==, !=, >, <, >=, <=, is, and is not.
- 4The > and < operators exclude equality; >= and <= include equality.
- 5The == operator tests value equality, while is tests whether two operands refer to the exact same object.
- 6To debug a comparison, identify both operands and the operator, apply the operator's rule, and predict the boolean result.
At a glance
- Boolean expressions produce True or False.
- Assignment uses =; comparison uses ==.
- The eight comparison operators ask different questions about values or object identity.
- Inclusive operators accept equality, while exclusive operators do not.
- A dependable debugging method is to identify the operands and operator, then predict the result before execution.