热线电话:13121318867

登录
2018-12-07 阅读量: 710
python运算符的==

运用==的操作者操作数和检查两者的值相等的值进行比较。虽然运营商检查两个操作数是否指向同一对象或没有。

# python3 code to

# illustrate the

# difference between

# == and is operator

# [] is an empty list

list1 = []

list2 = []

list3=list1

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")

  • 第一个if条件的输出为“True”,因为list1和list2都是空列表。
  • 第二条if条件显示“False”,因为两个空列表位于不同的内存位置。因此list1和list2引用不同的对象。我们可以在python中使用id()函数检查它,它返回一个对象的“标识”。
  • 第三个if条件的输出为“True”,因为list1和list3都指向同一个对象。
0.0000
5
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子