解释 Python 中 == 和 is 运算符之间的差异。
== 运算符
== 运算符通过检查对象的数值相等性比较运算对象。
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
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP