Python的"和"运营商整数 [英] Python "and" operator with ints

查看:101
本文介绍了Python的"和"运营商整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

,这是什么行为在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 evaluates x; if x 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 evaluates x; if x is true, its value is returned; otherwise, y is evaluated and the resulting value is returned.

这篇关于Python的"和"运营商整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆