在Python中,逻辑`和`运算符如何处理整数?
所以,我在玩翻译,输入了以下内容:在Python中,逻辑`和`运算符如何处理整数?,python,Python,所以,我在玩翻译,输入了以下内容: In [95]: 1 and 2 Out[95]: 2 In [96]: 1 and 5 Out[96]: 5 In [97]: 234324 and 2 Out[97]: 2 In [98]: 234324 and 22343243242 Out[98]: 22343243242L In [99]: 1 or 2 and 9 Out[99]: 1 起初我认为这与假值和真值有关,因为: In [101]: True + True Out[101]:
In [95]: 1 and 2
Out[95]: 2
In [96]: 1 and 5
Out[96]: 5
In [97]: 234324 and 2
Out[97]: 2
In [98]: 234324 and 22343243242
Out[98]: 22343243242L
In [99]: 1 or 2 and 9
Out[99]: 1
起初我认为这与假值和真值有关,因为:
In [101]: True + True
Out[101]: 2
In [102]: True * 5
Out[102]: 5
但这似乎并不相关,因为False总是0,从上面的试验来看,它似乎不是输出的最大值
老实说,我在这里看不到模式,在文档中也找不到任何东西(老实说,我真的不知道如何有效地查找它)
那么,你是怎么做的
int(x) [logical operation] int(y)
用Python工作 来自Python文档: 表达式
x和y
首先计算x
;如果x
为false,则其
返回值;否则,将计算y
,并计算结果值
他回来了
这正是你的实验所显示的。所有x
值均为真,因此返回y
值
它适用于Python中的每一项,它不依赖于整数
not x Returns True if x is False, False otherwise
x and y Returns x if x is False, y otherwise
x or y Returns y if x is False, x otherwise
1为真,因此它将返回2我发现不是x