Concepts / Comparison Operators

Comparison Operators

Returns whether x is less than y. All comparison operators return True or False . Note the capitalization of these names.

  • Programming

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

comparison is truecomparison is falsexleft operandTruex is less than y<less thanFalsex is not less than yyright operand
How does comparing the values of x and y produce a True or False result?

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

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

Read the operator: The symbol < asks whether the value on its left is less than the value on its right.

Compare the values: The left value is 3 and the right value is 5. The comparison asks whether 3 is less than 5.

Produce 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

  • Using a single equal sign when you want to compare two values.

    The single equal sign is an assignment operator, while the double equal sign is a comparison operator.

    Fix: Use == when the question is whether two values are equal.

  • Writing the greater-than-or-equal or less-than-or-equal symbols in the wrong order.

    Python does not have comparison operators written as =< or =>.

    Fix: Use <= for less than or equal to and >= for greater than or equal to.

  • Changing the capitalization of the result names.

    The comparison results are written as True and False with initial capital letters.

    Fix: Preserve the capitalization: True and False.

Check Your Operator Choice

EASY

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?

  • True
  • False
Reveal answer

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

  1. Comparison operators compare two operands.
  2. The six comparison operators are ==, !=, >, <, >=, and <=.
  3. Every comparison operator returns either True or False.
  4. Use == for comparison and = for assignment.
  5. Write the inequality combinations as >= and <=, never => or =<.

Key Takeaways

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