为什么(1 == 2!= 3)在Python中评估为False?

Mo Xu :

为什么(1 == 2 != 3)评估,以False在Python,而这两个((1 == 2) != 3)(1 == (2 != 3))评估来True

这里使用什么运算符优先级?

考希克(Kaushik)NP:

这是由于运营商chaining phenomenonPydoc将其解释为:

比较可以任意链接,例如,x <y <= z等于x <y和y <= z,除了y仅被评估一次(但是在两种情况下,当x <y被发现时,z都不被评估。是假的)。

如果你看一下优先的的==!=运营商,你会发现,它们具有相同的优先级,因此适用于链接现象

所以基本上会发生什么:

>>>  1==2
=> False
>>> 2!=3
=> True

>>> (1==2) and (2!=3)
  # False and True
=> False

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么“ range(2)== True中的1”评估为False?

为什么语句[1,2] <[2,1]在python中评估为True

为什么“ []是[]”在python中评估为False

为什么null不in(1,2,3)为false

为什么布尔表达式“(1,2,3)== True中的1”为False?

为什么isinstance([1,2,3],List [str])评估为true?

为什么我在Python中的if语句评估为False?

为什么`1> undefined`评估为false?

波浪号对布尔值的影响-为什么在Python中〜True为-2和〜False为-1?

为什么在JavaScript中,`2 << -1'为零而不是1?

为什么在JavaScript中“ 1” + +“ 1” =“ 11”但“ 1”--“ 1” = 2

为什么-3 >> 1等于-2?

为什么!! 1 ==“ 1”等于true,而!! 2 ==“ 2”等于false?

为什么“ 1” <2等于R中的FALSE?

为什么VBA认为-1 <3 <2为真?

为什么1 =='1,2'为真?

为什么3-1 * 8 + 2 * 3等于1

为什么3> 2> 1返回false,而1 <2 <3返回true?

为什么[1,1,2] | [] == [1,2]在Ruby中是否为true?

为什么`“%,2,%”之类的“,1,2,3,”`返回零?

为什么([1,0]中的1 = = True)评估为False?

为什么在Python 1.0中== 1 >>> True; -2.0 == -2 >>> True等?

在 JavaScript 中,为什么 2 > 1 > 0 true 而 8 > 1 > 1 false?

为什么`minimum(1,2)`在Haskell中返回2?

为什么1&1 === 1返回true,而2&2 === 2返回false?

当str设置为opt2时,为什么分数会评估分数+1

为什么 Python 为 [0xfor x in (1, 2, 3)] 返回 [15]?

为什么在JavaScript中1+ +“ 2” +3的结果为6?

为什么“ [[1> 2]”的计算结果为True?