在Python中,13和9=什么? 13 和 19 = 什么?

问题描述 投票:0回答:2
如果两边都是

True

,则布尔运算符 'and' 返回 
True
。 0 以外的任何数字都是 True。所以 python 应该返回 
True
for

13 and 19
但是为什么它返回19?

Agan,python 应该返回 True

13 and 9
但是为什么 python 返回 9?

python boolean operator-keyword
2个回答
0
投票
表达式 x 和 y 首先计算 x;如果 x 为 false,则返回其值;否则,计算 y 并返回结果值。

https://docs.python.org/3/reference/expressions.html#boolean-operations


0
投票
如果两边都是
and

,则
布尔运算符
True
返回
True

如果双方字面上都是

True

,则情况就是这样,正如毫不奇怪地,
True and True
True
。对于其他值,它们是否真实,在这种情况下,链中的最后一个值是传播的值。
这是因为您并不总是需要布尔值。如果它返回一个布尔值并“吃掉”你的字符串,test = x and "works!" or "nope"

会很烦人。

© www.soinside.com 2019 - 2024. All rights reserved.