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 文档

你可能感兴趣的:(Python内置函数,python,内置函数,bool,布尔型,逻辑真假)