89DEVs

Python bool() function

The bool() function checks if a value is True or False, and returns the Boolean.

bool() on empty list

print(bool([])) In this example we test an empty list, and boolean() returns False. False

bool() on integer 0

print(bool(0)) In this example we test the integer 0, and boolean() returns False. False

bool() on float 0.0

print(bool(0.0)) In this example we test the float 0.0, and boolean() returns False. False

bool() on non-empty string

print(bool('test string')) In this example we test a string, and boolean() returns True. True

bool() on non-empty list

print(bool([0])) In this example we test a non-empty list, and boolean() returns True. True

bool on complex number

print(bool(0j)) In this example we test the complex number 0j, and boolean() returns False. False

bool() on None

print(bool(None)) In this example we test None, and boolean returns False.

bool() syntax

The syntax of bool() is: bool(value)

bool() arguments

The bool() function accepts at most 1 argument. If more than one argument is passed, an TypeError exception is raised. If no argument is passed, it returns False.

bool() return values

The bool() function returns True, if the value is considered True. And it returns False, if the value is considered False. See this table for the most common return values:
condition return value
True True
non-empty string True
non-zero integer True
non-zero float True
non-zero complex number True
non-empty list True
non-empty dictionary True
False False
None False
0 False
0.0 False
0j False
() False
empty list [] False
empty string '' False
empty dictionary {} False

                
        

Summary


Click to jump to section