Python内置函数 — bool

1、bool

Python内置函数, bool() 函数可被用来将任意值转换为布尔值。

源码注释:

class bool(int):
    """
    bool(x) -> bool
    
    Returns True when the argument x is true, False otherwise.
    The builtins True and False are the only two instances of the class bool.
    The class bool is a subclass of the class int, and cannot be subclassed.
    """

 语法格式:

class bool([x])

接收一个任意参数,返回一个布尔值,True 或者 False。 如果 x 是假值或者被省略,返回 False;否则返回 True。

bool类型是int类型的子类,True和False是bool类型仅有的2个实例。

参数:

  •  x,可选参数,可以是任意对象

返回值:

  • 返回一个布尔值,True 或者 False。

用法实例:

print(bool())
print(bool(False))
print(bool(0))
print(bool(None))
print(bool(1 > 2))

print(bool(1))
print(bool(True))
---------------------------------------------------------------------------------------
False
False
False
False
False
True
True

 

2、逻辑真假判断

(1)假值 False

  • 被定义为假值的常量:None 和 False。
  • 任何数值类型的零:0, 0.0, 0j, Decimal(0), Fraction(0, 1)
  • 空的序列和多项集::'', (), [], {}, set(), range(0)

(2)真值 True

  • 除了假值以外的所有对象,如True,1


reference:

内置函数 — Python 3.8.16 文档 

内置类型 — Python 3.8.16 文档

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值