Python 和/或运算符返回值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4477850/
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
and / or operators return value
提问by atp
I was watching a 2007 video on Advanced Python or Understanding Python, and at 18'27" the speaker claims "As some may know in Python andand orreturn one of the two values, whereas notreturns always a boolean." When has this been the case?
我正在观看2007 年关于 Advanced Python 或Understanding Python 的视频,并且在 18'27" 演讲者声称“正如某些人在 Python 中可能知道的那样and并or返回两个值之一,而not返回始终是一个布尔值。”何时是这种情况?
As far as I can tell, andand orreturn booleans, too.
据我所知,and也or返回布尔值。
采纳答案by Frédéric Hamidi
The andand oroperators do return one of their operands, not a pure boolean value like Trueor False:
在and和or运营商做他们的操作数,而不是一个纯粹的布尔值一样返回一个True或False:
>>> 0 or 42
42
>>> 0 and 42
0
Whereas notalways returns a pure boolean value:
而not总是返回一个纯布尔值:
>>> not 0
True
>>> not 42
False
回答by mgalgs
回答by Reut Sharabani
from Python docs:
来自Python 文档:
The operator not yields True if its argument is false, False otherwise.
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.
The expression x or y first evaluates x; if x is true, its value is returned; otherwise, y is evaluated and the resulting value is returned.
如果其参数为假,则运算符不产生 True,否则产生 False。
表达式 x 和 y 首先计算 x;如果 x 为假,则返回其值;否则,计算 y 并返回结果值。
表达式 x 或 y 首先计算 x;如果 x 为真,则返回其值;否则,计算 y 并返回结果值。
Python's oroperator returns the first Truth-y value, or the last value, and stops. This is very useful for common programming assignments that need fallback values.
Python 的or运算符返回第一个 Truth-y 值或最后一个值,然后停止。这对于需要回退值的常见编程分配非常有用。
Like this simple one:
像这个简单的:
print my_list or "no values"
This will print my_list, if it has anything in it. Otherwise, it will print no values(if list is empty, or it is None...).
这将打印my_list,如果它有任何东西。否则,它将打印no values(如果列表为空,或者它是None...)。
A simple example:
一个简单的例子:
>>> my_list = []
>>> print my_list or 'no values'
no values
>>> my_list.append(1)
>>> print my_list or 'no values'
[1]
The compliment by using and, which returns the first False-y value, or the last value, and stops, is used when you want a guard rather than a fallback.
and当你想要一个守卫而不是一个后备时,使用 using 的恭维,它返回第一个 False-y 值,或最后一个值,并停止。
Like this one:
像这个:
my_list and my_list.pop()
This is useful since you can't use list.popon None, or [], which are common prior values to lists.
这很有用,因为您不能使用list.poponNone或[],它们是列表的常见先验值。
A simple example:
一个简单的例子:
>>> my_list = None
>>> print my_list and my_list.pop()
None
>>> my_list = [1]
>>> print my_list and my_list.pop()
1
In both cases non-boolean values were returned and no exceptions were raised.
在这两种情况下,都返回了非布尔值并且没有引发异常。
回答by dulaj sanjaya
Need to add some points to the @Frédéric 's answer.
需要在@Frédéric 的回答中添加一些要点。
do return one of their operands???
做返回他们的操作数之一???
It is True but that is not the logic behind this. In python a number except 0 is considered as True and number 0 is considered as False.
这是真的,但这不是这背后的逻辑。在 python 中,除 0 之外的数字被视为 True,数字 0 被视为 False。
(0 and 42 -> False and True) = False.
(0 和 42 -> 假和真)= 假。
That's why it returns 0.
这就是它返回 0 的原因。
(0 or 42-> false or True) = 42
(0 或 42-> 假或真)= 42
In that case the statement will be True because of the operand 42. so python returns the operand which causes the statement to be true, in that case.
在这种情况下,由于操作数 42,语句将为 True。因此,在这种情况下,python 返回导致语句为真的操作数。


