Python bool() 使用方法及示例

您所在的位置:网站首页 bool函数例子 Python bool() 使用方法及示例

Python bool() 使用方法及示例

2024-04-10 05:26| 来源: 网络整理| 查看: 265

Python 字符串方法Python 内置函数 Python bool() 使用方法及示例

Python 内置函数

bool()方法使用标准真值测试过程将值转换为布尔值(True或False)。

bool的语法是:

bool([value])bool()参数

将值传递给bool()不是必需的。如果不传递值,则bool()返回False。

通常,bool()使用单个参数值。

bool()返回值

bool()返回:

False 如果值被省略或为false

True 如果值为true

以下值在Python中被视为false:

None

False

任何数字类型的零。例如,0,0.0,0j

空序列。例如(),[],''。

空映射。例如,{}

具有__bool__()或__len()__方法返回0或False

除这些值以外的所有其他值均视为“ true”。

示例:bool()如何工作?test = [] print(test,'is',bool(test)) test = [0] print(test,'is',bool(test)) test = 0.0 print(test,'is',bool(test)) test = None print(test,'is',bool(test)) test = True print(test,'is',bool(test)) test = 'Easy string' print(test,'is',bool(test))

运行该程序时,输出为:

[] is False [0] is True 0.0 is False None is False True is True Easy string is True

Python 内置函数

Python 字符串方法Python 内置函数


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3