Concepts / Comparison Operators

Comparison Operators

A comparison operator compares its operands and returns either True or False.

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

Key point

A comparison operator compares its operands and returns either True or False.

Comparing x and y

xleft operandTruex is less than y<less thanFalsex is not less than yyright operandcomparison is truecomparison is false
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

Worked example

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.

  1. 1
    Read the operator

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

  2. 2
    Compare the values

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

  3. 3
    Produce the result

    Because the comparison is true, the comparison produces True.

Result

True

Key point

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 like
    Writing = instead of == for an equality comparison.
    Why it fails

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

    Do this instead
    Use == 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 like
    Writing =< or =>.
    Why it fails

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

    Do this instead

    Use <= 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 like

    Writing true or false instead of True or False.

    Why it fails

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

    Do this instead

    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. 1
    Comparison operators compare two operands.
  2. 2
    The six comparison operators are ==, !=, >, <, >=, and <=.
  3. 3
    Every comparison operator returns either True or False.
  4. 4
    Use == for comparison and = for assignment.
  5. 5
    Write 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.

Related concepts

More concepts starting with C