Before learning about the bool() method in Python, let's see what are Python Booleans.
What is Boolean?
True
False
Python Boolean Type
type()
of both True
and False
is bool.>>> type(False)
<class 'bool'>
>>> type(True)
<class 'bool'>
Boolean Operator
print(18 > 9)
print(18 == 9)
print(18 < 9)
True
False
False
if
statement, Python returns True
or False
:a = 120
b = 50
if b > a:
print("b is greater than a")
else:
print("b is not greater than a")
b is not greater than a
Here, the if
condition evaluates to False
, hence, the else
condition's statement is printed.
After learning about Booleans in Python, let us see the bool() method in Python.
What is the bool() method in Python?
bool()
method is a built-in method in Python that allows us to evaluate an expression or value and return True
or False
.bool()
method is used to return or convert a value to a Boolean value(True or False).Syntax of bool()
bool(object)
The bool()
method takes 0 or 1 argument. In general, it accepts one object as a parameter like List, String, Number, etc.
Note: If no parameter is passed, the bool()
method returns False
.
How bool() works?
The following are the few cases in which Python’s bool() method returns False
:
- If None is passed.
- If
False
value is passed. (False
andTrue
are keywords in Python) - If 0(zero) is passed in any numeric type, such as 0, 0.0, etc.
- If an empty object is passed such as (), [], {}, "", etc.
- If Objects of Classes having __bool__() or __len()__ method, returning 0 or
False
.
Other than the above-mentioned cases, the bool()
method always returns True
.
Let's look at some examples of the bool()
method.
Example 1:
# Returns False as x is False
x = False
print(bool(x))
# Returns True as x is True
x = True
print(bool(x))
# Returns False as x is None
x = None
print(bool(x))
False
True
False
Note: False
and True
are keywords in Python.
Example 2:
bool()
method returns True
for any number except 0.print(bool(1))
print(bool(0))
print(bool(0.00))
print(bool(18))
print(bool(-101))
True
False
False
True
True
Example 3:
bool()
method return True
for any string except an empty string.x = 'CosedDope'
print(bool(x))
x = 'Python is Interesting'
print(bool(x))
x = ''
print(bool(x))
x = ' '
print(bool(x))
True
True
False
True
Note: In the above example, the last print statement prints True
as the string is not empty. The length of the string is 1 as space is counted while calculating the length of a string.
Example 4:
bool()
method returns True
for any List except an empty List.x = ["air", "water", "fire"]
print(bool(x))
x = []
print(bool(x))
x = [1, 2]
print(bool(x))
True
False
True
Example 5:
bool()
method returns True
for any Tuple except an empty Tuple.x = ("cookies", "chocolates", "wafers")
print(bool(x))
x = ()
print(bool(x))
True
False
Example 6:
bool()
method returns False
if you have an object that is made from a class with a __len__
function that returns 0 or False
.class Person:
def __len__(self):
return 0
obj = Person()
print(bool(obj))
bool()
method in Python.def check_num(x):
return (bool(x % 2 == 0))
# Driver Code
x = 101
if (check_num(x)):
print("The number is Even")
else:
print("The number is Odd")
The number is Odd
Here, the check_num()
function evaluates if the number passed as a parameter is even or odd and returns True
or False
accordingly with the help of bool() method. If the bool() method returns True
, the if
condition's statement is printed and if the bool() method returns False
, the else
statement is printed.