解释 Python 中 == 和 is 运算符之间的区别。
pythonserver side programmingprogramming更新于 2025/10/13 8:12:17
== 运算符
== 运算符通过检查对象值的相等性来比较操作数。
is 运算符
is 运算符通过检查对象是否相同来比较操作数。
示例
以下是 Python 中的程序,用于展示差异。
list1 = [1]
list2 = [1]
list3 = list1
print(id(list1))
print(id(list2))
if (list1 == list2):
print("True")
else:
print("False")
if (list1 is list2):
print("True")
else:
print("False")
if (list1 is list3):
print("True")
else:
print("False")
输出
140380664377096 140380664376904 True False True
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

