QA
Beginner LevelPython

What is the difference between the `is` operator and the `==` operator in Python?

❓ Interview Prompt: Compare identity comparison (`is`, checking memory addresses via id()) vs value equality (`==`, checking object values via __eq__).

1. Value Equality ('=='):

Evaluates whether the contents / values of two objects are equal by calling the '__eq__()' dunder method.

2. Identity Comparison ('is'):

Evaluates whether two variables point to the exact same object in memory, comparing their memory addresses ('id(a) == id(b)').

Rule of Thumb:

•
Use 'is' ONLY for checking singleton objects ('None', 'True', 'False').
•
Use '==' for comparing numbers, strings, lists, and objects!

Code Implementation Example

python
a = [1, 2, 3]
b = [1, 2, 3]

print(a == b) # True  (Contents are identical!)
print(a is b) # False (Different memory addresses! id(a) != id(b))

# Correct singleton check:
res = None
if res is None:
    print('Response is None')
💡Comparing object value equality (==) vs identity (is).
🎯 Interview Key Takeaway

Use == to compare object values; use 'is' only when comparing singleton objects like None, True, or False.

#is operator#== operator#Identity#Equality#Beginner
Back to all Python Questions→

More Python Interview Questions

Beginner LevelPython

What is the Python Global Interpreter Lock (GIL) and how does it affect multi-threading?

Beginner LevelPython

What is the difference between a List, Tuple, Set, and Dictionary in Python?

Beginner LevelPython

Why are mutable default arguments (like def func(items=[])) dangerous in Python and how do you fix them?