Python 内建类型 - 真值测试

Python中的任何对象都可以进行真值测试,如在if或while条件中。None、False、任何数值类型的零值、空序列和空映射被视为假。其他值通常视为真。布尔运算如or和and会返回其操作数之一。
摘要由CSDN通过智能技术生成

Manual

Any object can be tested for truth value, for use in an if or while condition or as operand of the Boolean operations below. The following values are considered false:
- None
- False
- zero of any numeric type, for example,0,0.0, 0j.
- any empty sequence, for example, ”,(), [].
- any empty mapping, for example, {}.
- instances of user-defined classes, if the class defines a bool() or len() method, when that method returns the integer zero orbool valueFalse.

All other values are considered true — so objects of many types are always true.
Operations and built-in functions that have a Boolean result always return0 orFalse for false and1 orTrue for true, unless otherwise stated. (Important exception: the Boolean operationsor andand always return one of their operands.)

直译

任意对象可以通过‘使用 if 或 while条件’或‘在布尔预算中作为操作数’来检测真值。以下值被认为是否:
- None
- False
- 任意数值类型的0值,例如:0, 0.0, 0j
- 任意空序列,例如:‘’,[],()
- 任意空映射,例如:{}
- 自定义类的实例,如果类定义了bool()或len()方法,当方法返回整数0或布尔值False

除此之外,所有其他值被认为是真,所以通常很多类型的对象为真。运算和内建函数的布尔运算结果,往往为否返回0或False,为真返回1或True(除非有其他规定)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值