The Difference Between is and == in Python

2026-11-28来源 dev.toEnglish

When I first started Python, I used is and == interchangeably. Then I learned the hard way that they are very different.

== checks if the values are the same.
is checks if they are the exact same object in memory.

a = [1, 2, 3] b = [1, 2, 3] print(a == b) print(a is b) 

Always use == unless you are specifically checking for None (like if x is None:).

原文:https://www.a8q9.cn/en/article/the-difference-between-is-and-in-python-6d2b90.html

相关文章