首页 > 为什么"" is False == False,而且"" is True == False

为什么"" is False == False,而且"" is True == False

为什么"" is False == False,而且"" is True == False


感受下这个例子

a = [1, 2, 3]
b = [1, 2, 3]

a == b
True

a is b
False

因为""是字符串,即不是True,也不是False
Python里,虽然没有布尔值,""放到if后面,判断的结果也确实和False相同,但二者毕竟是不能==的。

以下两个倒是为真:

0 == False
1 == True

可见,Python的布尔值,其实是用int实现的,和C++类似。


因为运算符is是用来判断两个操作数是否是同一个,它的结果为真的条件是:

""TrueFalse类型首先就不同,所以两个表达式都为假。

【热门文章】
【热门文章】