简体   繁体   English

解释 Boolean 表达式 output

[英]Explain Boolean expression output

2 > 5 and ((10.= 10 or 5 >= 5) or .5 <= 1/2) 2 > 5 和 ((10.= 10 或 5 >= 5) 或 .5 <= 1/2)

This expression is supposed to print false and it does, but what is the explanation behind it?这个表达式应该打印 false 并且它确实打印了,但它背后的解释是什么?

Boolean expressions are evaluated from left to right. Boolean 表达式从左到右计算。 So in this case because the first expression 2 > 5 is false and the operator following that expression is AND then we assume the entire line evaluates to false (because False AND anything else is still false so there is no need to evaluate the entire line)所以在这种情况下,因为第一个表达式 2 > 5 是假的,并且该表达式后面的运算符是 AND,那么我们假设整行的计算结果为假(因为 False AND 其他任何内容仍然是假的,所以不需要计算整行)

You can split statements and check,您可以拆分语句并检查,

In [1]: 2 > 5 and ((10 != 10 or 5 >= 5) or .5 <= 1/2)
Out[1]: False

In [2]: 2 > 5
Out[2]: False

In [3]: 10 != 10
Out[3]: False

In [4]: 5 >= 5
Out[4]: True

In [5]: .5 <= 1/2
Out[5]: True

In [6]: False and ((False or True) or True)
Out[6]: False

Explanation,解释,

1. False and ((False or True) or True)
# False or True >> True

2. False and (True or True)
# True or True >> True

3. False and True
False and True >> False

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM