Understanding the difference between is and in in Python is really important — especially when you're trying to write clean and bug-free code. In this short guide, I’ve shared my learning and examples that helped me understand how these two operators work differently.
The is operator checks identity, not equality.
- It returns
Trueif two variables refer to the same object in memory. - It’s often used to check if something is
None.
a = [1, 2, 3] b = a c = [1, 2, 3] print(a is b) # True – same memory reference print(a is c) # False – different objects even if content is same🔸 Use
iswhen you care about object identity, not just if values match.
A good use case:
x = None if x is None: print("x has no value")The in operator checks membership — it tells you if a value exists inside a list, string, tuple, dictionary, etc.
fruits = ["apple", "banana", "cherry"] print("banana" in fruits) # True print("grape" in fruits) # FalseIt also works with strings:
text = "hello world" print("world" in text) # True print("python" in text) # False| Expression | Description | Output |
|---|---|---|
a is b | True if a and b are same object | True |
a == b | True if a and b have same value | True |
x in y | True if x is inside container y | True/False |
-
❌ Using
isto compare values:a = 1000 b = 1000 print(a is b) # Might be False even though a == b
-
✅ Use
==when comparing values, and useisfor identity:print(a == b) # True
-
✅ Always use
is None, not== None.
- Use
iswhen comparing withNoneor checking identity. - Use
into check if a value exists inside something. - Don't mix them up — Python won’t stop you, but your bugs will get harder to find!