因此,我正在与解释器一起玩,并输入以下内容:
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
最初,我认为这与False和True值有关,因为:
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 and y首先计算x; 如果x为假,则返回其值;否则,y将求值并返回结果值。
x and y
x
y
这正是您的实验显示正在发生的情况。您所有的x值都是真实的,因此将y返回该值。
https://docs.python.org/3/reference/expressions.html#and