Boolean operations

2020-02-06 02:02发布

I'm confused on how Python evaluates boolean statements.

For ex.

False and 2 or 3

returns 3

How is this evaluated? I thought Python first looks at 'False and 2', and returns False without even looking at 'or 3'. What is the order of what Python sees here?

Another is:

1 or False and 2 or 2 and 0 or 0

returns 1

From what I gathered from the first example, I thought Python would evaluate from left to right, so '1 or False' would return 1, then '1 and 2' would return 2, then '2 or 2' would return the first 2, then '2 and 0' would return 0, then '0 or 0' would return the second 0.

As you can tell I'm pretty perplexed here, please help!

Thanks!

5条回答
何必那么认真
2楼-- · 2020-02-06 02:49

Check out section 5.15 of this page: http://docs.python.org/reference/expressions.html

or has a lower precendence than and, so your statement is evaluated as

(false and 2) or 3
查看更多
等我变得足够好
3楼-- · 2020-02-06 02:54

The issue here is that there is an order to evaluation, and and has a higher precedence than or. As such, they are evaluated left-to-right here.

Given this, False and 2 or 3 is, to python, (False and 2) or 3 - so it evaluates to False or True, then to True.

In your next example, Python short-circuits, so 1 or False evaluates to True as 1 does, and therefore returns 1. The rest never gets evaluated. This makes sense as if the one part of an or is True, you know the whole thing must be - so why bother doing extra work?

A good way to check this is to define functions that print:

>>> def test(x):
...     print("Test: "+str(x))
...     return x
... 
>>> test(1) or test(0) or test(3)
Test: 1
1
>>> test(0) or test(0) or test(3)
Test: 0
Test: 0
Test: 3
3
>>> test(False) and test(2) or test(3)
Test: False
Test: 3
3
>>> test(1) or test(False) and test(2) or test(2) and test(0) or test(0)
Test: 1
1

This makes it easy to see what gets evaluated, and in what order.

查看更多
Ridiculous、
4楼-- · 2020-02-06 02:56

I read about this a while back on the python docs page. If I find it, I'll post the reference. It stated something along the lines of, in a boolean statement, Python will return the first True object. Hence....the and was pre-falsed with False, but the or only had one argument, which was 3 (True).

查看更多
家丑人穷心不美
5楼-- · 2020-02-06 03:05

and has higher precedence than or.

False and 2 or 3

is evaluated as

((False and 2) or 3)

Since the first part (False and 2) is False, Python has to evaluated the second part to see whether the whole condition can still become True or not. It can, since 3 evaluates to True so this operand is returned.

Similar for 1 or False and 2 or 2 and 0 or 0 which is evaluated as

(1 or ((False and 2) or ((2 and 0) or 0)))

Since 1 evaluates to True, the whole condition will be True, no matter which value the other operands have. Python can stop evaluating at this point and again, returns the operand that determines the final value.

Stopping as early as the final result is determined is called short-circuit evaluation and can be described as follows:

Whenever the final result of the expression is determined, the evaluation is stopped and in Python the value of the operand that determines the final value is returned. That is, assuming a left-to-right evaluation:

  • for the and operator, the left-most operand that evaluates to False (or the last one)
  • for the or operator, the left-most operand that evaluates to True (or the last one)
查看更多
看我几分像从前
6楼-- · 2020-02-06 03:05

It is evaluated like that:

result = (False and 2) or 3

So basically if False and 2 is true-ish, then it is returned, otherwise 3 is returned.

查看更多
登录 后发表回答