bool() Builtin Function

Python – bool()

Python bool() builtin function returns the boolean value of given object.

Syntax

The syntax of bool() function is

bool(x)

where x is an object.

bool() returns a boolean value of False for the following values of x.

  • Empty objects like [], (), {}, ''.
  • 0
  • None
  • False

For any other values of the argument to bool(), it returns True.

Examples

Boolean of a non-zero value

In the following program, we take an integer (non-zero) value in x and find its boolean value using bool() function.

Python Program

x = 7
output = bool(x)
print(f'x       : {x}')
print(f'bool(x) : {output}')
Run

Output

x       : 7
bool(x) : True

Boolean of an empty string

In the following program, we take an empty string value in x and find its boolean value using bool() function.

Python Program

x = ""
output = bool(x)
print(f'x       : {x}')
print(f'bool(x) : {output}')
Run

Output

x       : 
bool(x) : False

Summary

In this tutorial of Python Examples, we learned the syntax of bool() function, and how to find the boolean value of a given object using bool() with examples.