Python: Comparison Operators

OperatorDescription
==Equal to
!=Not Equal
>Greater than
<Less than
>=Less than or Equal to
<=Less than or Equals to

Notes

All comparison operators have the same priority.

The priority of comparison operators is lower than the mathematical operators.

Let’s check the evaluation and usage of the operators-

Equal to Comparison

print(10 == 10)
print("big box code" == "big box code")

print(10 == 200)
print("big box code" == "Big Box Code")

print(10 == 10.00)
Python

Output:

True
True

False
False

True
Plaintext

Not Equal to Comparison

print(10 != 10)
print(10 != 10.00)

print(10 != 200)
print(200 != 10)

print("big box code" != "big box code")

print("big box code" != "Big Box Code")
print("Big Box Code" != "big box code")
Python

Output:

False
False

True
True

False

True
True
Plaintext

Greater than Comparison

print(10 > 10)
print(10 > 10.00)

print(10 > 200)
print(200 > 10)

print("big box code" > "big box code")

print("big box code" > "Big Box Code")
print("Big Box Code" > "big box code")
Python

Output:

False
False

False
True

False

True
False
Plaintext

Less than Comparison

print(10 < 10)
print(10 < 10.00)

print(10 < 200)
print(200 < 10)

print("big box code" < "big box code")

print("big box code" < "Big Box Code")
print("Big Box Code" < "big box code")
Python

Output:

False
False

True
False

False

False
True
Plaintext

Greater than or Equal to Comparison

print(10 >= 10)
print(10 >= 10.00)

print(10 >= 200)
print(200 >= 10)

print("big box code" >= "big box code")

print("big box code" >= "Big Box Code")
print("Big Box Code" >= "big box code")
Python

Output:

True
True

False
True

True

True
False
Plaintext

Less than or Equal to Comparison

print(10 <= 10)
print(10 <= 10.00)

print(10 <= 200)
print(200 <= 10)

print("big box code" <= "big box code")

print("big box code" <= "Big Box Code")
print("Big Box Code" <= "big box code")
Python

Output:

True
True

True
False

True

False
True
Plaintext

Chaining Comparison Operators

We can chain the use of comparison operators, and those will be evaluated as separate conditions with “and” checking-

x = 10
y = 20

print(10 == x == 10)    # equivalent to (10 == x and x == 10)
print(y == x == 10)     # equivalent to (y == x and x == 10)
print(y  > x == 10)     # equivalent to (y > x and x == 10)
Python

Output:

The expressions are evaluated as below

ExpressionEquivalent Expression
10 == x == 1010 == x and x == 10
y == x == 10y == x and x == 10
y > x == 10y > x and x == 10

Here are the result of the above code-

True
False
True
Plaintext

Check some more examples-

x = 5
y = 10
z = 20

print(x < y < z)        # equivalent to (x < y and y < z)
print(x > y <= z)       # equivalent to (x > y and y <= z)
Python

Output:

The evaluation will be done as below-

ExpressionEquivalent Expression
x < y < zx < y and y < z
x > y <= zx > y and y <= z

Here is the output-

True
False
Plaintext

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.