和/或运算符返回值 [英] and / or operators return value

查看:125
本文介绍了和/或运算符返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在观看 2007年有关高级Python或了解Python的视频,并在说话者说18'27"有些人可能在Python中知道andor返回两个值之一,而not总是返回布尔值."

据我所知,andor也会返回布尔值.

解决方案

andor运算符确实返回其操作数之一,而不是像TrueFalse这样的纯布尔值:

>>> 0 or 42
42
>>> 0 and 42
0

not始终返回纯布尔值:

>>> not 0
True
>>> not 42
False

I was watching a 2007 video on Advanced Python or Understanding Python, and at 18'27" the speaker claims "As some may know in Python and and or return one of the two values, whereas not returns always a boolean." When has this been the case?

As far as I can tell, and and or return booleans, too.

解决方案

The and and or operators do return one of their operands, not a pure boolean value like True or False:

>>> 0 or 42
42
>>> 0 and 42
0

Whereas not always returns a pure boolean value:

>>> not 0
True
>>> not 42
False

这篇关于和/或运算符返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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