python中逻辑语句NOT AND & OR的优先级

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16679272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:21:26  来源:igfitidea点击:

Priority of the logical statements NOT AND & OR in python

pythonpython-3.xboolean-expression

提问by Akshar Gupta

As far as I know, in C & C++, the priority sequence for NOT AND & OR is NOT>AND>OR. But this doesn't seem to work in a similar way in Python. I tried searching for it in the Python documentation and failed (Guess I'm a little impatient.). Can someone clear this up for me?

据我所知,在C&C++中,NOT AND & OR的优先顺序是NOT>AND>OR。但这在 Python 中似乎并没有以类似的方式工作。我尝试在 Python 文档中搜索它并失败了(我猜我有点不耐烦了。)。有人可以帮我解决这个问题吗?

回答by mgilson

notbinds tighter than andwhich binds tighter than oras stated in the language reference

not绑定比and哪个绑定比语言参考or中所述的绑定更紧密

回答by Kyle Heuton

It's NOT, AND, OR, from highest to lowest according to the documentation on Operator precedence

根据运算符优先级的文档,它不是、与、或从高到低

Here is the complete precedence table, lowest precedence to highest. A row has the same precedence and chains from left to right

这是完整的优先级表,从最低优先级到最高优先级。一行具有相同的优先级和从左到右的链

  1. :=
  2. lambda
  3. if – else
  4. or
  5. and
  6. not x
  7. in, not in, is, is not, <, <=, >, >=, !=, ==
  8. |
  9. ^
  10. &
  11. <<, >>
  12. +, -
  13. *, @, /, //, %
  14. +x, -x, ~x
  15. **
  16. await x
  17. x[index], x[index:index], x(arguments...), x.attribute
  18. (expressions...), [expressions...], {key: value...}, {expressions...}
  1. :=
  2. 拉姆达
  3. 如果别的
  4. 或者
  5. 不是 x
  6. 在,不在,是,不是,<、<=、>、>=、!=、==
  7. |
  8. ^
  9. &
  10. <<, >>
  11. +, -
  12. *, @, /, //, %
  13. +x, -x, ~x
  14. **
  15. 等待 x
  16. x[index], x[index:index], x(arguments...), x.attribute
  17. (表达式...), [表达式...], {key: value...}, {expressions...}

回答by Oswald Wirt

Of the boolean operators the precedence, from weakest to strongest, is as follows:

布尔运算符的优先级,从最弱到最强,如下:

  1. or
  2. and
  3. not x
  4. is not; not in
  1. or
  2. and
  3. not x
  4. is not; not in

Where operators are of equal precedence evaluation proceeds from left to right.

如果运算符具有同等优先级,则从左到右进行评估。

回答by MarianD

There is no good reasonfor Python to have otherpriority sequence of those operators than well established onein (almost) all other programming languages, including C/C++.

除了在(几乎)所有其他编程语言(包括 C/C++)中已经确立的优先级序列之外,Python没有充分理由对这些运算符设置其他优先级序列。

You may find it in The Python Language Reference, part 6.16 - Operator precedence, downloadable (for the current version and packed with all other standard documentation) from https://docs.python.org/3/download.html, or read it online here: 6.16. Operator precedence.

您可以在Python 语言参考6.16 部分 - 运算符优先级中找到它,可从https://docs.python.org/3/download.html下载(对于当前版本并与所有其他标准文档打包在一起),或阅读它在线:6.16。运算符优先级

But there is still something in Python which can mislead you: The resultof andand oroperators may be differentfrom Trueor False- see 6.11 Boolean operationsin the same document.

但是,仍然有一些在Python可以误导你:该结果andor运营商可能是不同TrueFalse-见6.11布尔运算相同的文件内。

回答by nos

You can do the following test to figure out the precedence of andand or.

您可以执行以下测试来确定and和的优先级or

First, try 0 and 0 or 1in python console

首先,0 and 0 or 1在python控制台中尝试

If orbinds first, then we would expect 0as output.

如果or首先绑定,那么我们期望0作为输出。

In my console, 1is the output. It means andeither binds first or equal to or(maybe expressions are evaluated from left to right).

在我的控制台中,1是输出。这意味着and要么首先绑定要么等于or(也许表达式是从左到右计算的)。

Then try 1 or 0 and 0.

然后尝试1 or 0 and 0

If orand andbind equally with the built-in left to right evaluation order, then we should get 0as output.

如果orand绑定与内置的从左到右的评估顺序相等,那么我们应该得到0作为输出。

In my console, 1is the output. Then we can conclude that andhas higher priority than or.

在我的控制台中,1是输出。那么我们可以得出结论and比 具有更高的优先级or

回答by Koffer Shen

NOT AND OR is the correct answer.

NOT AND OR 是正确答案。

回答by Victoria Stuart

Some simple examples; note the operator precedence (not, and, or); parenthesize to assist human-interpretability.

一些简单的例子;注意运算符的优先级(not、and、or);括号以帮助人类可解释性。

a = 'apple'
b = 'banana'
c = 'carrots'

if c == 'carrots' and a == 'apple' and b == 'BELGIUM':
    print('True')
else:
    print('False')
# False

Similarly:

相似地:

if b == 'banana'
True

if c == 'CANADA' and a == 'apple'
False

if c == 'CANADA' or a == 'apple'
True

if c == 'carrots' and a == 'apple' or b == 'BELGIUM'
True

# Note this one, which might surprise you:
if c == 'CANADA' and a == 'apple' or b == 'banana'
True

# ... it is the same as:
if (c == 'CANADA' and a == 'apple') or b == 'banana':
True

if c == 'CANADA' and (a == 'apple' or b == 'banana'):
False

if c == 'CANADA' and a == 'apple' or b == 'BELGIUM'
False

if c == 'CANADA' or a == 'apple' and b == 'banana'
True

if c == 'CANADA' or (a == 'apple' and b == 'banana')
True

if (c == 'carrots' and a == 'apple') or b == 'BELGIUM'
True

if c == 'carrots' and (a == 'apple' or b == 'BELGIUM')
True

if a == 'apple' and b == 'banana' or c == 'CANADA'
True

if (a == 'apple' and b == 'banana') or c == 'CANADA'
True

if a == 'apple' and (b == 'banana' or c == 'CANADA')
True

if a == 'apple' and (b == 'banana' and c == 'CANADA')
False

if a == 'apple' or (b == 'banana' and c == 'CANADA')
True