BlogsDope image BlogsDope

Python bool() Function

Jan. 23, 2021 FUNCTION PYTHON 278

Before learning about the bool() method in Python, let's see what are Python Booleans.

What is Boolean?

In computer science, a boolean or bool is a data type with two possible values:
  1. True
  2. False
In a programming language, we often need to know if an expression is True or False.

Python Boolean Type

The Python Boolean type is one of Python’s built-in data types.
The type() of both True and False is bool.
>>> type(False)
<class 'bool'>
>>> type(True)
<class 'bool'>

Boolean Operator

A boolean operator, or logical operator, consists of operators such as AND, OR, NOT, NOR, NAND, and XOR. These operators are used with conditional statements in programming, search engines, algorithms, and formulas. When you compare two values, the expression is evaluated and Python returns the Boolean answer.
Let's see some examples for better understanding.
print(18 > 9)
print(18 == 9)
print(18 < 9)

True 

False 

False


When we run a condition in an 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?

The bool() method is a built-in method in Python that allows us to evaluate an expression or value and return True or False.
The 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 and True 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

NoteFalse and True are keywords in Python.

Example 2:

The 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:

The 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:

The 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:

The 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:

The 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))
​False
Let's write a program to find out a number is even or odd using the 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.


Liked the post?
Rarely seen, always noticed.
Editor's Picks
0 COMMENT

Please login to view or add comment(s).