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()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
