🚀 Supercharge your YouTube channel's growth with AI.
Try YTGrowAI FreePython Comparison Operators Explained

To make a decision in a Python condition, you need a way to compare values. Python comparison operators check whether values are equal or how they are ordered, and return a result a condition can use.
This article explains what each comparison symbol means and how to write a range check.
TL;DR: Python comparison operators
Use == and != to test equality, or <, <=, >, and >= to test value order.
- Write == in a condition. A single = assigns a value.
- Use a chained expression such as 0 < temperature < 30 for an open range.
- Check operand types before ordering values from different kinds.
What are Python comparison operators?
A comparison evaluates two operands and returns a result used by a condition. The six value comparisons give a compact way to express equality or ordering.
| Operator | Meaning | Example |
|---|---|---|
| == | Equal to | score == 10 |
| != | Not equal to | status != “done” |
| < | Less than | age < 18 |
| <= | Less than or equal to | count <= 5 |
| > | Greater than | price > 20 |
| >= | Greater than or equal to | temperature >= 0 |
String equality checks the complete text, including capitalization and spaces. Thus, “Python” and “python” are unequal.
Python compares strings lexicographically by Unicode code points, with a shorter matching prefix ordered before the longer string.
Python also supports identity and membership tests. They answer whether two references point to the same object or whether an item belongs to a container. The operator overview covers those separate tasks.
How to compare values and test ranges
Use a chained ordering expression when one value must fall between two bounds. In 0 < temperature < 30, both comparisons must pass, so the endpoints do not pass.
from fractions import Fraction
print(5 == 5.0)
print(5 < 5.5)
print(1 < 2 < 3)
print("ant" < "bat")
print(Fraction(1, 3) < 0.34)
print(0.1 + 0.2 == 0.3)
print(Fraction(1, 10) + Fraction(2, 10) == Fraction(3, 10))
print(float("nan") == float("nan"))
I ran python3 comparison_probe.py. Its output is below.
True True True True True False True False

The float sum comparison returns False, while the exact rational sum made with Fraction returns True. See AskPython’s string equality guide for case-sensitive string matching.
The Python expression reference says each expression in a comparison chain is evaluated once or less. The expression a != b != c checks a against b and b against c, not a against c. If every value must differ from every other value, compare each pair explicitly or use a set for hashable values.
Edge cases with Python comparisons
Python’s built-in types differ in which comparisons they support. The cases below show the result and a suitable next step.
| Case | What happens | What to do |
|---|---|---|
| Integer and string comparison | 5 == “5” is False. Ordering raises TypeError: ‘<‘ not supported between instances of ‘int’ and ‘str’. | Convert or validate the input before ordering it. |
| Integer and numeric float | Python compares their numeric values, so 5 == 5.0 is True. | Choose the operator that matches the intended boundary. |
| Decimal-looking float sum | 0.1 + 0.2 == 0.3 is False because binary floating-point values approximate these decimal fractions. | For approximate measurements, choose an appropriate tolerance such as math.isclose(). |
| Exact rational values | Fraction(1, 10) + Fraction(2, 10) == Fraction(3, 10) is True. | Use Fraction when the domain needs exact rational arithmetic. |
| NaN | float(“nan”) == float(“nan”) is False. | Use a NaN-specific check rather than equality. |
| Complex values | Equality is supported, but less-than ordering is not. | Compare a chosen component or magnitude only when it represents the intended rule. |
The Python built-in types reference documents supported comparisons. The fractions documentation describes exact rational values. The operator module provides callable forms such as operator.eq(a, b), useful when a function accepts an operation as an argument.
The module also includes in-place operations, which are separate from comparisons. For example, operator.iadd(a, b) performs the in-place addition method. A mutable list can be changed by that operation, while a number produces an updated value that must be assigned by the caller.
Conclusion
Match the operator to the decision in your condition. Python’s comparison expression documentation gives the full syntax and semantics.
FAQ
These answers clarify two symbols in simple conditions.
Which operator checks equality in Python?
Use == to compare values for equality. A single = assigns a value to a name.
Which operator means not equal in Python?
Use != to check whether two values are unequal.
Can Python order an integer and a string?
No. Ordering an integer against a string raises TypeError. Convert or validate the values first.


