'and' 和 'or' 如何处理 Python 中的非布尔值? [英] How do 'and' and 'or' act with non-boolean values in Python?

查看:39
本文介绍了'and' 和 'or' 如何处理 Python 中的非布尔值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

22 and 333/12 or 1

我遇到了上面的代码行.结果是 27,但我不太明白在这种情况下 andor 是什么意思.有人可以向我解释,最好有例子.提前致谢!!

I have encountered the above line of code. The result is 27, but I could not quite understand what does and and or mean in this case. Can somebody explain to me, preferred with examples. Thanks in advance!!

推荐答案

这是和-或技巧" -- andor 实际上并不返回布尔值;相反,它们返回其输入参数之一.人们过去常常将其用于控制​​流.

This is the "and-or trick" -- and and or don't actually return boolean values; instead, they return one of their input arguments. People used to use this for control-flow.

从 python 2.5 开始,不再需要,因为引入了条件表达式.

As of python 2.5, it's not necessary anymore, because conditional expressions were introduced.

22 and 333/12 or 1

相当于

333/12 if 22 else 1

这篇关于'and' 和 'or' 如何处理 Python 中的非布尔值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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