Search⌘ K
AI Features

Comparison Operators

Explore Python's comparison operators and identity operators to understand how they compare values and memory locations. Learn about boolean outcomes, the concept of integer interning, and why floating-point comparisons require special consideration due to binary representation limits.

Comparison operators can be used to compare values in mathematical terms. The precedence of comparison operator is same.

Symbol

Operator

>

Greater Than

<

Less Than

>=

Greater Than or Equal To

<=

Less Than or Equal To

==

Equal To

!=

Not Equal To

Output of comparison operators

The result of a comparison is always a bool. If the comparison is correct, the value of the bool will be True. Otherwise, its value will be False.

Try it yourself

Let’s look at a few examples. Our AI Mentor can explain the code as well.

Python 3.10.4
num1 = 5
num2 = 10
num3 = 10
print(num2 > num1)
print(num1 > num2)
print(num2 == num3)
print(num3 != num1)
print(3 + 10 == 5 + 5)
print(3 <= 2)

Identity operators

In Python, the is and is not operators are used to compare the memory ...