Python的"和"运营商整数 [英] Python "and" operator with ints
问题描述
,这是什么行为在Python?
What is the explanation for this behavior in Python?
a = 10
b = 20
a and b # 20
b and a # 10
a和b
的计算结果为20,而 B和
的计算结果为10相当于真阳性整数?为什么它评估为第二个值?因为是第二次?
a and b
evaluates to 20, while b and a
evaluates to 10. Are positive ints equivalent to True? Why does it evaluate to the second value? Because it is second?
推荐答案
The documentation explains this quite well:
这位前pression x和y
首先评估 X
;如果 X
是假的,它的值返回;否则,是
评估并返回结果值。
The expression
x and y
first evaluatesx
; ifx
is false, its value is returned; otherwise,y
is evaluated and the resulting value is returned.
和同样为或
这将可能是你的嘴唇上的下一个问题。
And similarly for or
which will probably be the next question on your lips.
这位前pression x或y
首先评估 X
;如果 X
是真实的,它的值返回;否则,是
评估并返回结果值。
The expression
x or y
first evaluatesx
; ifx
is true, its value is returned; otherwise,y
is evaluated and the resulting value is returned.
这篇关于Python的"和"运营商整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!