bool() 函数使用标准真值测试程序将值转换为布尔值(真或假)。
用法:
bool([value])
参数:
将值传递给 bool() 不是强制性的。如果不传递值,bool() 将返回 False。
在一般使用中,bool() 采用单个参数 value 。
返回:
bool() 返回:
False如果value被省略或为假True如果value为真
以下值在 Python 中被认为是错误的:
NoneFalse- 任何数字类型的零。例如,
0,0.0,0j - 空序列。例如,
(),[],''。 - 空映射。例如,
{} - 具有
__bool__()或__len()__方法的类的对象,该方法返回0或False
除这些值之外的所有其他值都被视为真。
示例: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 bool()用法及代码示例
- Python bokeh.plotting.figure.asterisk()用法及代码示例
- Python bokeh.plotting.figure.annular_wedge()用法及代码示例
- Python bokeh.plotting.figure.circle()用法及代码示例
- Python bokeh.plotting.figure.circle_cross()用法及代码示例
- Python bokeh.plotting.figure.bezier()用法及代码示例
- Python bokeh.plotting.figure.diamond()用法及代码示例
- Python bokeh.plotting.figure.step()用法及代码示例
- Python bokeh.plotting.figure.diamond_cross()用法及代码示例
- Python bokeh.plotting.figure.arc()用法及代码示例
- Python bokeh.plotting.figure.circle_x()用法及代码示例
- Python bokeh.plotting.figure.annulus()用法及代码示例
- Python bokeh.plotting.figure.dash()用法及代码示例
- Python bokeh.plotting.figure.cross()用法及代码示例
- Python base64.b64decode()用法及代码示例
- Python binary转string用法及代码示例
- Python base64.b32decode()用法及代码示例
- Python bytearray()用法及代码示例
- Python base64.b85encode()用法及代码示例
- Python bz2.decompress(s)用法及代码示例
注:本文由纯净天空筛选整理自 Python bool()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
