Python 的逻辑运算符 AND

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18195322/
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-19 10:09:46  来源:igfitidea点击:

Python's Logical Operator AND

pythonoperator-keyword

提问by BubbleMonster

I'm a little confused with the results I'm getting with the logical operators in Python. I'm a beginner and studying with the use of a few books, but they don't explain in as much detail as I'd like.

我对使用 Python 中的逻辑运算符得到的结果有些困惑。我是初学者,正在使用几本书进行学习,但它们并没有像我想要的那样详细地解释。

here is my own code:

这是我自己的代码:

five = 5
two = 2

print five and two

>> 2

It seems to be just outputting the two variable.

它似乎只是输出两个变量。

five = 5
two = 2
zero = 0

print five and two and zero

So, I added another variable integer. Then I printed and got the following output:

所以,我添加了另一个变量整数。然后我打印并得到以下输出:

>> 0

What is going on with Python in the background? Why isn't the output something like 7 or 5, 2.

Python 在后台发生了什么?为什么输出不是 7 或 5、2 之类的东西。

采纳答案by tdelaney

Python Boolean operators return the last value evaluated, not True/False. The docshave a good explanation of this:

Python 布尔运算符返回最后评估的值,而不是 True/False。该文档有一个很好的解释:

The expression x and yfirst evaluates x; if xis false, its value is returned; otherwise, yis evaluated and the resulting value is returned.

表达式x and y首先计算x;如果xfalse,则返回其值;否则,y计算并返回结果值。

回答by Ali Gajani

This ANDin Python is an equivalent of the &&in Java for instance. This doesn't mean the and in the English language. The ANDis a logical operator. Assume five holds 5 and two holds 2. From Python documentation: The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned. Basically, it evaluates the last integer in your case which is true.

例如AND,Python 中的 this 相当于&&Java 中的 。这并不意味着英语中的 和 。该AND是一个逻辑运算符。假设五个包含 5,两个包含 2。来自 Python 文档:表达式 x 和 y 首先计算 x;如果 x 为假,则返回其值;否则,计算 y 并返回结果值。基本上,它评估您的情况下的最后一个整数,这是真的。

if (five and two):
...     print "True"
... else:
...     print "False"

The AND is a logical operator, to test the logic for a specific case, not an arithmetic operator. If you want to get results like 7 for five and two, you should rather use "+" which means adding two integers. See below:

AND 是逻辑运算符,用于测试特定情况的逻辑,而不是算术运算符。如果你想得到像 7 的 5 和 2 这样的结果,你应该使用“+”,这意味着添加两个整数。见下文:

>>> five = 5
>>> two = 2
>>> print five + two
7

回答by aChipmunk

As a bit of a side note: (i don't have enough rep for a comment) The AND operator is not needed for printing multiple variables. You can simply separate variable names with commas such as print five, twoinstead of print five AND two. You can also use escapes to add variables to a print line such as print "the var five is equal to: %s" %five. More on that here: http://docs.python.org/2/library/re.html#simulating-scanf

作为一个旁注:(我没有足够的代表发表评论)打印多个变量不需要 AND 运算符。您可以简单地用逗号分隔变量名称,例如print five, two代替print five AND two。您还可以使用转义符将变量添加到打印行,例如print "the var five is equal to: %s" %five. 更多关于这里:http: //docs.python.org/2/library/re.html#simulating-scanf

Like others have said AND is a logical operator and used to string together multiple conditions, such as

就像其他人所说的,AND 是一个逻辑运算符,用于将多个条件串在一起,例如

if (five == 5) AND (two == 2):
    print five, two

回答by deadboy

Boolean And operators will return the first value 5if the expression evaluated is false, and the second value 2if the expression evaluated is true. Because 5and 2are both real, non-false, and non-null values, the expression is evaluated to true.

布尔和运算符将返回的第一个值5,如果计算的表达式是false,第二值2,如果计算的表达式是true。因为5and2都是实值、非假值和非空值,所以表达式的计算结果为真。

If you wanted to print both variables you could concatenate them to a String and print that.

如果你想打印这两个变量,你可以将它们连接到一个字符串并打印出来。

five = 5
two = 2
print five + " and " + two

Or to print their sum you could use

或者打印他们可以使用的总和

print five + two

This documentexplains how to use the logical Boolean operators.

文档解释了如何使用逻辑布尔运算符。

回答by bobalukalallu

Try 0and 9.

尝试09

The result is 0because the value of 0is false. The operand on the left of the andoperator is False so the whole expression is False and returns 0

结果是0因为 的值为0假。运算符左侧的and操作数为 False,因此整个表达式为 False 并返回0

回答by Steven William Bausch

In Python any non-zero integer value is true; zero is false.

在 Python 中,任何非零整数值都为真;零是假的。

The OP's values are both non-zero.

OP 的值都是非零的。

The AND operator tests from left to right,

AND 运算符从左到右进行测试,

with and, if all values are True, returns the last evaluated value. If any value is false, returns the first one.

with and,如果所有值都为 True,则返回最后评估的值。如果任何值为 false,则返回第一个。

Since both are non-zero, both are true, so the last value is returned

由于两者都非零,因此两者都为真,因此返回最后一个值