小编典典

逻辑`和`运算符如何在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

最初,我认为这与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中工作?


阅读 152

收藏
2020-12-20

共1个答案

小编典典

从Python文档中:

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

这正是您的实验显示正在发生的情况。您所有的x值都是真实的,因此将y返回该值。

https://docs.python.org/3/reference/expressions.html#and

2020-12-20