1不是'1'==真 - 基本理解

时间:2018-05-29 08:48:57

标签: python boolean-operations

我目前很难理解为什么输出

1 is not '1' == True

False

事情就是当我插入这样的括号时:

1 is not ('1' == True)
(1 is not '1') == True

都返回True

我得到第二部分,因为1,即True,不是False,这是字符串“1”和True的比较结果。此外,整数1不是字符串1,它是true,因此等于True。但是一旦我没有放任何括号,它就会返回False。那是为什么?

1 个答案:

答案 0 :(得分:0)

is用于比较地址(引用),而==用于比较值。因此,1 is not '1' == True始终为False

正如@deceze所提到的,这是第二部分:

>>> 1 is not ('1' == True)
>>> ('1' == True)
>>> False
>>> 1 is not False
>>> True

>>> (1 is not '1') == True
>>> (1 is not '1')
>>> True
>>> True == True
>>> True