python - 逻辑 `and` 运算符如何处理整数?

标签 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 工作?

最佳答案

来自 Python 文档:

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.

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

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

关于python - 逻辑 `and` 运算符如何处理整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49658308/

相关文章:

python - 从 Sublime Text 2 运行 .py 文件

python - NLTK ViterbiParser 无法解析不在 PCFG 规则中的单词

python - 将每个项目与一行中的最后一个非空值配对

Python CFFI - 无法在函数调用中使用格式化的 Python 字符串作为字节数组

python - Phonon 可以播放 FLAC 文件吗?

python - 如何用Python3中的logging包把自己的日志文件放到正确的地方?

python - 是否可以更改 Kivy 应用程序任务栏图标?

python - 在 Python 中绘制每次迭代的 KMeans 聚类中心

python - 在文本文件中找到最大的数字并写下它的行

python - 确定条件是否适用于列表的所有成员