In this tutorial, we will learn about the python bool() method and its uses.

Python bool() Method

What is Python bool() Method?

The bool() is a built-in Python method that returns the boolean(True or False)  value of a specified given object using python’s standard truth testing procedure.

The syntax of bool() method is:

bool(value)

Python bool() parameters

The bool()  has no specified parameter; it is not mandatory to pass a value to the bool() method. 

Let see an example of the bool() method.

Example 1: Using bool() method.

my_bool = []
print(my_bool,'is',bool(my_bool))

my_bool = [0]
print(my_bool,'is',bool(my_bool))

my_bool = 0.0
print(my_bool,'is',bool(my_bool))

my_bool = None
print(my_bool,'is',bool(my_bool))

my_bool = True
print(my_bool,'is',bool(my_bool))

my_bool = 'Easy string'
print(my_bool,'is',bool(my_bool))

The output will be as follows.

[] is False
[0] is True
0.0 is False
None is False
True is True
Easy string is True

The following values are always considered false in Python:

  • None will return False as it is a Null value.
  • False will also return False as output.
  • Empty Sequence  and mapping like (),[],’’ and {} will return False.
  • objects of Classes which has __bool__() or __len()__ method which returns 0 or False

All other values except these values are considered true.

Rules of bool() method.

  • If the value is not empty and has any true value, it will return True.
  • If the value is empty and has no true value, it will return False.

 

Was this article helpful?
Share on: