您好,欢迎访问一九零五行业门户网

Python内置bool函数详细介绍

英文文档:
class bool([x])
    return a boolean value, i.e. one of true or false. x is converted using the standard truth testing procedure. if x is false or omitted, this returns false; otherwise it returns true. the bool class is a subclass of int (see numeric types — int, float, complex). it cannot be subclassed further. its only instances are false and true (see boolean values).
说明:
    1. 返回值为true或者false的布尔值
    2. 参数如果缺省,则返回false
>>> bool() #未传入参数 false
3. 参数转换使用标准的逻辑测试表达式
3.1 传入布尔类型时,按原值返回
>>> bool(true) true >>> bool(false) false
3.2 传入字符串时,空字符串返回false,否则返回true
>>> bool('') false >>> bool('0') true
3.3 传入数值时,0值返回false,否则返回true
>>> bool(0) false >>> bool(1) true >>> bool(-1.0) true
3.4 传入元组、列表、字典等对象时,元素个数为空返回false,否则返回true
>>> bool(()) #空元组 false >>> bool((0,)) #非空元组 true >>> bool([]) #空列表 false >>> bool([0]) #非空列表 true >>> bool({}) #空字典 false >>> bool({'k':'v'}) #非空字典 true
以上就是python内置bool函数详细介绍的详细内容。
其它类似信息

推荐信息