Python online course

Python Comparison Operators

Learn how Python comparison operators evaluate values and produce True or False, with examples using numbers, variables, arithmetic, and thresholds.

Python comparison operators evaluate the relationship between two operands. An operand is a value, variable, or expression on either side of an operator. Every comparison produces a Boolean result: True or False.

For example, 8 > 3 asks whether 8 is greater than 3. Python evaluates the expression and returns True.

>>> 8 > 3
True
>>> 8 < 3
False

A comparison is different from assignment. Assignment stores a value in a variable with =. Equality testing uses ==.

>>> score = 80       # assignment
>>> score == 80     # equality comparison
True

Python comparison operators

OperatorMeaningExample expressionExample result
==Equal to5 == 5True
!=Not equal to5 != 3True
>Greater than9 > 4True
<Less than4 < 9True
>=Greater than or equal to5 >= 5True
<=Less than or equal to5 <= 5True

Equality with ==

The equality operator, ==, tests whether two evaluated values have the same value. Equality is not assignment.

>>> 12 == 12
True
>>> 12 == 7
False
>>> 4 + 3 == 7
True
>>> 10 * 2 == 25
False

Variables can appear on either side. Python evaluates each variable to its stored value before comparing.

>>> expected = 42
>>> actual = 40 + 2
>>> actual == expected
True

Inequality with !=

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

>>> 6 != 2
True
>>> 6 != 6
False
>>> 3 + 3 != 7
True

Greater-than and less-than

Read a comparison from left to right. The expression x > y means “x is greater than y.” The expression x < y means “x is less than y.” These are strict comparisons, so equal values do not satisfy them.

>>> 9 > 4
True
>>> 4 > 9
False
>>> 4 < 9
True
>>> 9 < 4
False
>>> 5 > 5
False
>>> 5 < 5
False

Inclusive comparisons: >= and <=

>= means greater than or equal to, while <= means less than or equal to. Unlike > and <, these operators accept equality.

>>> 5 > 5
False
>>> 5 >= 5
True
>>> 5 < 5
False
>>> 5 <= 5
True
Operator pairWhen equality is acceptedExample with equal operands
> versus >=> rejects equality; >= accepts it10 >= 10 is True
< versus <=< rejects equality; <= accepts it10 <= 10 is True

Inclusive operators are useful for boundaries. For example, a score meets a minimum when it is at least 60.

>>> score = 60
>>> score >= 60
True

Comparisons with variables

Suppose x stores 5 and y stores 7. Python substitutes the stored values when evaluating each comparison; the comparison itself does not change either variable.

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

These results follow directly from the values: 5 and 7 differ, 5 is less than 7, and 5 is therefore not greater than or equal to 7. A comparison evaluates values; it does not assign a new value.

Comparisons involving arithmetic expressions

An arithmetic expression is evaluated before Python performs the comparison. In x + y < 15, Python first calculates x + y, then compares the sum with 15.

>>> x = 5
>>> y = 7
>>> x + y == 12
True
>>> x + y < 15
True
>>> x + y > 10
True
>>> x + y <= 12
True
>>> x + y > 12
False

Here, x + y becomes 12 before each comparison. Parentheses can make grouping explicit when an expression contains several operations.

>>> (x + y) >= 12
True
>>> x + (y * 2) == 19
True

Using Boolean comparison results

A Boolean result can be displayed, stored in a variable, or used as a condition in a conditional statement.

>>> is_ready = 7 > 3
>>> is_ready
True
>>> print(10 == 10)
True

Comparisons are commonly used with if, elif, and else to choose what code runs. Multiple comparisons can also be combined with logical operators such as and, or, and not.

>>> age = 20
>>> age >= 18 and age <= าง 65
True

The last example contains a typo if copied exactly: use the numeric value 65 without any non-ASCII character.

>>> age >= 18 and age <= 65
True

For more practice, review Python arithmetic operators, Python logical operators, and the Python if statement.

Common comparison mistakes

Using = instead of ==

= assigns a value, while == tests equality. Use == inside a comparison expression.

>>> value = 5
>>> value == 5
True

Expecting strict operators to accept equality

> and < require a strictly larger or smaller value. Use >= or <= when the boundary should count.

Reading the operands backward

In x > y, the left operand, x, is being tested relative to the right operand, y. Read it as “x is greater than y.”

Assuming comparison changes a variable

A comparison only produces True or False. It does not modify either operand. Use a separate assignment when a variable needs to change.

Forgetting arithmetic evaluation

For x + y < 15, Python computes the sum first and then compares it with 15. Add parentheses when they make the intended grouping clearer.