Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么10和5在python中返回5和5,10在python中返回10?基本上,它返回and运算符之后的第二个int,而与值无关_Python_Operator Keyword_And Operator - Fatal编程技术网

为什么10和5在python中返回5和5,10在python中返回10?基本上,它返回and运算符之后的第二个int,而与值无关

为什么10和5在python中返回5和5,10在python中返回10?基本上,它返回and运算符之后的第二个int,而与值无关,python,operator-keyword,and-operator,Python,Operator Keyword,And Operator,我在研究python“and”运算符时发现: >>> 10 and 5 5 >>> 5 and 10 10 考虑语句逻辑以及哪个操作数决定整个表达式的值: 和: 第一个falsy操作数使表达式为False,而不管后面是什么 如果只有truthy操作数,则最后一个操作数将解决该问题 示例: >>> 5 and 0 0 >>> 0 and 5 0 >>> 5 and 0 and 3 0 >>

我在研究python“and”运算符时发现:

>>> 10 and 5
5
>>> 5 and 10
10

考虑语句逻辑以及哪个操作数决定整个表达式的值:

  • 第一个falsy操作数使表达式
    为False
    ,而不管后面是什么
  • 如果只有truthy操作数,则最后一个操作数将解决该问题
示例:

>>> 5 and 0
0
>>> 0 and 5
0 
>>> 5 and 0 and 3
0 
>>> 10 and 5
5
>>> 5 and 10
10
>>> 5 or 0
5
>>> 0 or 5
5 
>>> 5 or 0 or 3
5 
>>> 10 or 5
10
>>> 5 or 10
5
  • 第一个truthy操作数使表达式
    为True
    ,而不管后面是什么
  • 如果只有错误的操作数,则最后一个操作数将解决该问题
示例:

>>> 5 and 0
0
>>> 0 and 5
0 
>>> 5 and 0 and 3
0 
>>> 10 and 5
5
>>> 5 and 10
10
>>> 5 or 0
5
>>> 0 or 5
5 
>>> 5 or 0 or 3
5 
>>> 10 or 5
10
>>> 5 or 10
5
放炮电路行为: 请注意,表达式的其余部分甚至没有计算,如果您链接(例如)函数调用或具有副作用的其他表达式,则这是相关的:

>>> 4/0 or 5
ZeroDivisionError
>>> 5 or 4/0
5
>>> func1() or func2() and func3()
# func2 and func3 might never be called
左右结合性 这是短路行为的直接要求,但并不完全直观,尤其是与Python的算术运算符相比:

a or b and c or d == (a or (b and (c or d)))