Home > Blockchain >  Explain Boolean expression output
Explain Boolean expression output

Time:09-14

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

This expression is supposed to print false and it does, but what is the explanation behind it?

CodePudding user response:

Boolean expressions are evaluated from left to right. 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)

CodePudding user response:

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
  • Related