English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The bool() method uses the standard truth testing process to convert values to boolean values (True or False).
The syntax of bool() is:
bool([value])
传递值给bool()不是必需的。如果不传递值,则bool()返回False。
通常,bool()使用单个参数值。
bool() returns:
False ifValueomitted or false
True ifValuetrue
The following values are considered false in Python:
None
False
Zero of any numeric type. For example, 0,0.0,0j
Empty sequence. For example (),[],''.
Empty mapping. For example, {}
With __bool__() or __len__() methods returning 0 or False
All other values except these are considered 'true'.
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))
When running the program, the output is:
[] is False [0] is True 0.0 is False None is False True is True Easy string is True