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.
Python bool() Method
The syntax of bool() method is:
1
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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.
1
2
3
4
5
6
[] 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.
This post is licensed under CC BY 4.0 by the author.