问题描述
在 Python 中这种行为的解释是什么?
What is the explanation for this behavior in Python?
a = 10
b = 20
a and b # 20
b and a # 10
a and b 计算结果为 20,而 b and a 计算结果为 10.正整数是否等同于 True?为什么它评估为第二个值?因为它是第二?
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?
推荐答案
文档解释这很好:
表达式x and y首先计算x;如果 x 为 false,则返回其值;否则,评估 y 并返回结果值.
The expression
x and yfirst evaluatesx; ifxis false, its value is returned; otherwise,yis evaluated and the resulting value is returned.
or 也是如此,这可能是你的下一个问题.
And similarly for or which will probably be the next question on your lips.
表达式x or y首先计算x;如果 x 为真,则返回其值;否则,评估 y 并返回结果值.
The expression
x or yfirst evaluatesx; ifxis true, its value is returned; otherwise,yis evaluated and the resulting value is returned.
这篇关于Python“和"带整数的运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)