bool() Builtin Function
Python - bool()
Python bool() builtin function is used to get the boolean value of a given object.
In this tutorial, you will learn the syntax of bool() function, and then its usage with the help of example programs.
Syntax
The syntax of bool()
function is
bool(x)
where
Parameter | Description |
---|---|
x | 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, the bool()
function returns True
.
Examples
1. 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}')
Output
x : 7
bool(x) : True
2. 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}')
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.