🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

Comparison Operators

AI & DATA SCIENCE // comparison-operators

Comparison operators (==, !=, and the four ordering operators) compare two values and always evaluate to a boolean, True or False.

Syntax

a == b
a != b
a < b
a > b
a <= b
a >= b

Deep Dive Course

Python's comparison operators work on numbers by their mathematical value, on strings and sequences lexicographically, element by element, and can be chained in a single expression, so checking that x is between two bounds can be written without repeating x. == checks for value equality, calling the object's __eq__ method, which is different from the is operator, which checks whether two names refer to the exact same object in memory.

1Understanding Comparison Operators

Python's comparison operators work on numbers by their mathematical value, on strings and sequences lexicographically, element by element, and can be chained in a single expression, so checking that x is between two bounds can be written without repeating x. == checks for value equality, calling the object's __eq__ method, which is different from the is operator, which checks whether two names refer to the exact same object in memory.

💡

Chained comparisons like checking that a value is between two bounds are evaluated the same way as writing the two conditions separately joined by `and`, but the middle value is only evaluated once — more concise and slightly more efficient than writing the `and` version by hand.

editor.html
print(5 == 5.0)
print(1 < 5 < 10)
print("apple" < "banana")
localhost:3000

2Practical Example

Here is a real-world application of Comparison Operators showing how it is used in production Python code.

editor.html
class Point:
    def __init__(self, x, y):
        self.x, self.y = x, y
    def __eq__(self, other):
        return self.x == other.x and self.y == other.y

print(Point(1, 2) == Point(1, 2))
localhost:3000

3Best Practices

Follow these guidelines when working with Comparison Operators:

1. Use == for value equality and is only for identity checks, like comparing to None

2. Take advantage of chained comparisons instead of writing them out with and

3. Override __eq__ (and usually __hash__ alongside it) on custom classes if you want == to compare by value instead of identity

⚠️

Tip: Chained comparisons like checking that a value is between two bounds are evaluated the same way as writing the two conditions separately joined by `and`, but the middle value is only evaluated once — more concise and slightly more efficient than writing the `and` version by hand.

editor.html
print(5 == 5.0)
print(1 < 5 < 10)
print("apple" < "banana")
localhost:3000

Examples

Example 01Basic Usage
print(5 == 5.0)
print(1 < 5 < 10)
print("apple" < "banana")
Example 02Advanced Example
class Point:
    def __init__(self, x, y):
        self.x, self.y = x, y
    def __eq__(self, other):
        return self.x == other.x and self.y == other.y

print(Point(1, 2) == Point(1, 2))

Best Practices

  • Use == for value equality and is only for identity checks, like comparing to None
  • Take advantage of chained comparisons instead of writing them out with and
  • Override __eq__ (and usually __hash__ alongside it) on custom classes if you want == to compare by value instead of identity

Interview Question

What's the difference between == and is, and when should you use is instead of ==?

Hint: One checks value, the other checks identity.

== calls the object's __eq__ method to compare values, which can be customized by a class, while is checks object identity — whether two names point to the literal same object in memory. is is the correct and idiomatic choice specifically for comparing against None, True, or False, since there's only ever one instance of each, but == is the right choice for comparing the actual content of two objects, like two separately-built lists with the same items.

Exercises

MediumPractice using Comparison Operators in a real scenario.
View Solution
print(5 == 5.0)
print(1 < 5 < 10)
print("apple" < "banana")

Frequently Asked Questions

What's the difference between == and is, and when should you use is instead of ==?

== calls the object's __eq__ method to compare values, which can be customized by a class, while is checks object identity — whether two names point to the literal same object in memory. is is the correct and idiomatic choice specifically for comparing against None, True, or False, since there's only ever one instance of each, but == is the right choice for comparing the actual content of two objects, like two separately-built lists with the same items.

Related Functions

logical-operatorsidentity-operatorsisinstance()