English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python basic tutorial

Python flow control

Fonctions en Python

Types de données en Python

Python file operations

Python objects and classes

Python date and time

Advanced knowledge of Python

Python reference manual

Python bool() usage and examples

Python built-in functions

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()不是必需的。如果不传递值,则bool()返回False。

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

bool() returns value

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'.

Example: how does bool() work?

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

Python built-in functions