VMware ESXi and vSphere Cluster Management

Python Comparison Operators

Learn how Python comparison operators such as ==, !=, >, <, >=, and <= test values and return True or False.

What comparison operators do

A comparison operator tests the relationship between two values. Each value or expression being compared is an operand. A comparison has this general structure:

left operand  operator  right operand

For example:

3 < 5

Here, 3 is the left operand, < is the comparison operator, and 5 is the right operand. Python evaluates the complete comparison to a Boolean result: True or False.

A Boolean is a value with one of two possibilities: True or False. A comparison is an expression because Python evaluates it to produce a value.

Python comparison operators

OperatorMeaningExample expressionExample Boolean result
==Equal value4 == 4True
!=Not equal; values differ4 != 7True
>Greater than8 > 3True
<Less than3 < 8True
>=Greater than or equal to5 >= 5True
<=Less than or equal to5 <= 5True

Equality with ==

The equality operator, ==, tests whether two values have the same value. It does not change either value.

10 == 10  # True
10 == 12  # False

== is a test. Do not confuse it with =, which assigns a value to a variable:

score = 10       # assignment
score == 10      # equality test

Inequality with !=

The inequality operator, !=, tests whether two values differ.

10 != 12  # True
10 != 10  # False

The result is True when the operands have different values and False when they are equal.

Greater-than and less-than comparisons

The > and < operators make directional comparisons, commonly between numbers.

3 > 1  # True: 3 is greater than 1
1 > 2  # False: 1 is not greater than 2
1 < 2  # True: 1 is less than 2
3 < 2  # False: 3 is not less than 2

These operators do not include equality. For example, 5 > 5 and 5 < 5 are both False.

Inclusive comparisons with >= and <=

The greater-than-or-equal operator, >=, is True when the left operand is greater than or equal to the right operand. The less-than-or-equal operator, <=, is True when the left operand is less than or equal to the right operand.

7 >= 5  # True: 7 is greater
5 >= 5  # True: the values are equal
3 >= 5  # False

3 <= 5  # True: 3 is less
5 <= 5  # True: the values are equal
7 <= 5  # False

The equal case is the important difference between > and >=, and between < and <=. Inclusive comparisons are useful for boundaries such as a minimum or maximum allowed value.

Comparing values stored in variables

Variables can hold the operands of a comparison. The comparison then evaluates the current values stored in those variables.

x = 5
y = 7

x == y   # False
x != y   # True
x > y    # False
x < y    # True
x >= y   # False
x <= y   # True

Because x contains 5 and y contains 7, the values are different, x is less than y, and x is not greater than or equal to y.

Interactive-shell demonstration

You can enter the following in a Python interactive shell. The shell displays the Boolean result of each expression:

>>> x = 5
>>> y = 7
>>> x == y
False
>>> x != y
True
>>> x > y
False
>>> x < y
True
>>> x >= y
False
>>> x <= y
True

Comparisons involving arithmetic expressions

A comparison can contain arithmetic expressions. Python evaluates the arithmetic and then compares the resulting values. For example, with x = 5 and y = 7, the sum x + y is 12.

x = 5
y = 7

x + y == 12  # True
x + y < 15   # True
x + y > 20   # False
x + y <= 12  # True

Arithmetic expressions can appear on either side of a comparison:

x + y == 6 + 6  # True
20 > x + y      # True

In an expression such as x + y <= 12, Python evaluates x + y first and then applies the comparison. This ordering is an example of operator precedence, the order Python uses when evaluating operators. Parentheses can make the intended order explicit:

(x + y) <= 12

Using comparison results in conditional logic

Comparison results are commonly used as conditions in control flow, such as an if statement:

temperature = 20

if temperature >= 18:
    print("Comfortable")

The indented code runs because temperature >= 18 evaluates to True. Comparisons can also be combined with Python's logical operators, such as and, or, and not:

age = 25
has_ticket = True

if age >= 18 and has_ticket:
    print("Entry allowed")

Logical operators are useful when a decision depends on multiple Boolean comparisons.

Common mistakes and troubleshooting

  • Using = to compare: use == for an equality test. The single equals sign assigns a value, as in count = 3.
  • Expecting an original value: a standard comparison returns True or False, not one of the operands.
  • Misreading inclusive operators: >= and <= include the equal case. Thus, 5 >= 5 and 5 <= 5 are both True.
  • Ignoring arithmetic: calculate the arithmetic portion before interpreting the comparison. With x = 5 and y = 7, x + y <= 12 means 12 <= 12, which is True.

Summary

  • A comparison operator tests the relationship between two operands.
  • Every comparison evaluates to the Boolean value True or False.
  • == tests equality, while != tests inequality.
  • > and < test strict ordering.
  • >= and <= test ordering while including equality.
  • Variables and arithmetic expressions can be used as operands.
  • Comparison results commonly provide conditions for if statements and can be combined with logical operators.

For a related reference, see Comparison Operators.