beginnerPython
Previous
How does the exponentiation operator work?
Next
What is the difference between != and is not?
What is the difference between == and is?
Answer
Clear, interview-ready explanation
The operator compares the values of two objects, while the == operator checks whether two references point to the same object in memory.
is
first = [1, 2, 3]
second = [1, 2, 3]
print(first == second)
print(first is second)
Output:
True
False
The lists contain equal values, but they are separate objects.
Use:
-
==to compare values. -
isto compare object identity.
The is operator is commonly used when checking for None:
if result is None:
print("No result is available")
Do not use to compare strings or numbers because object reuse is an implementation detail and may produce misleading results.
is