Comparison Operators
A comparison operator compares its operands and returns either True or False.
A Question with Two Answers
A comparison operator asks a question about two values. For example, comparing x with y using the less-than operator asks whether x is less than y. The result has only two possible forms: True or False. In Python, the capitalization matters: write True and False with initial capital letters.
A comparison operator compares its operands and returns either True or False.
Comparing x and y
The operator sits between two operands, such as x and y. Python evaluates the relationship described by the operator. With the less-than operator, the result is True when x is less than y. Otherwise, the result is False. The same two-result pattern applies to every comparison operator.
The Six Comparison Symbols
| Operator | Relationship it compares |
|---|---|
| == | equal to |
| != | not equal to |
| > | greater than |
| < | less than |
| >= | greater than or equal to |
| <= | less than or equal to |
Python comparison operators
Tracing a Less-Than Comparison
Suppose x has the value 3 and y has the value 5. Determine the result of comparing x and y with the less-than operator.
- 1Read the operator
The symbol < asks whether the value on its left is less than the value on its right.
- 2Compare the values
The left value is 3 and the right value is 5. The comparison asks whether 3 is less than 5.
- 3Produce the result
Because the comparison is true, the comparison produces True.
True
The operator determines the question, while the compared values determine whether the answer is True or False.
Symbols That Cause Errors
Common mistake Using a single equal sign when you want to compare two values.
What it looks likeWriting = instead of == for an equality comparison.Why it failsThe single equal sign is an assignment operator, while the double equal sign is a comparison operator.
Do this insteadUse == when the question is whether two values are equal.Common mistake Writing the greater-than-or-equal or less-than-or-equal symbols in the wrong order.
What it looks likeWriting =< or =>.Why it failsPython does not have comparison operators written as =< or =>.
Do this insteadUse <= for less than or equal to and >= for greater than or equal to.
Common mistake Changing the capitalization of the result names.
What it looks likeWriting true or false instead of True or False.
Why it failsThe comparison results are written as True and False with initial capital letters.
Do this insteadPreserve the capitalization: True and False.
Check Your Operator Choice
For each prompt, choose the comparison operator that expresses the relationship. Identify the operator for: values that are equal, values that are not equal, a left value greater than a right value, a left value less than a right value, a left value greater than or equal to a right value, and a left value less than or equal to a right value.
Hints
- Equality uses two equal signs, not one.
- The symbols >= and <= combine an inequality symbol with an equal sign.
- Every selected comparison operator will produce either True or False when evaluated.
What do you think happens?
If x has the value 8 and y has the value 8, what result does the less-than comparison x < y produce?
Reveal answer
False
The less-than operator asks whether x is less than y. Equal values do not satisfy a less-than comparison.
What to Remember
- 1Comparison operators compare two operands.
- 2The six comparison operators are ==, !=, >, <, >=, and <=.
- 3Every comparison operator returns either True or False.
- 4Use == for comparison and = for assignment.
- 5Write the inequality combinations as >= and <=, never => or =<.
At a glance
- Comparison operators ask whether a relationship between two operands is true.
- Python comparison results are True or False.
- The available operators are ==, !=, >, <, >=, and <=.
- The single equal sign performs assignment; the double equal sign performs comparison.
- Correct symbol order and capitalization are essential.