bool() in Python

PythonServer Side ProgrammingProgramming

The bool() in python returns a boolean value of the parameter supplied to it. The parameter can be any of the following and the results are as per the below conditions. Except the values mentioned here the remaining values return True.

False is returned when the parameter value passed is as below −

  • None

  • A False condition

  • Zero of any numeric type

  • Empty sequence (), [] etc.

  • Empty mapping like {}

  • objects of Classes which has __bool__() or __len()__ method which returns 0 or False


In the below program, we illustrate all such example scenarios.

print("None gives : ",bool(None))
print("True gives : ",bool(True))
print("Zero gives: ",bool(0))
# Expression evaluating to true
print("Expression evaluating to True: ",bool(0 == (18/3)))
# Expression evaluating to false
print("Expression evaluating to False: ",bool(0 == (18%3)))
s = ()
print("An mpty sequence: ",bool(s))
m = {}
print("An emty mapping: ",bool(m))
t = 'Tutoriaslpoint'
print("A non empty string: ",bool(t))


Running the above code gives us the following result −

None gives : False
True gives : True
Zero gives: False
Expression evaluating to True: False
Expression evaluating to False: True
An mpty sequence: False
An emty mapping: False
A non empty string: True
Updated on 07-Aug-2019 07:59:15