The bool()
method returns either True
or False
, based on the specified object. If no object is passed, it returns False
by default.
The bool()
method is declared as follows:
Only one parameter can go inside the bool()
method, be it a set, integer, string, list, dictionary, or character.
If no parameter goes inside the bool()
method, it returns a False
value.
The bool()
method returns True
if one of the following parameters is passed in it:
True
value.The bool()
method returns False
in the following cases:
False
value.In the example below, the bool()
method will return False
.
# Default case: no parameter passed to bool() methodprint "Default case:",bool()# Integer 0 goes inside bool() methodx = 0print "Integer 0:",bool(x)# Empty string goes inside bool() methody = ""print "Empty string:",bool(y)# Empty set goes inside bool() methodi = set()print "Empty set:",bool(i)# Empty list goes inside bool() methodj = []print "Empty list:",bool(j)# Empty dictionary goes inside bool() methodk = {}print "Empty dictionary:",bool(k)
The example below shows the instances where the bool()
method returns True
.
# A true value goes inside bool() methodcheck = Trueprint "The check is",bool(check)# Positive integer goes inside bool() methoda = 1print "Positive integer:",bool(a)# Negative integer goes inside bool() methodb = -6print "Negative Integer:",bool(b)# Non-empty string goes inside bool() methodc = "Hello"print "Non-empty string:",bool(c)# Non-empty list goes inside bool() methodg = [1,2,3]print "Non-empty list:",bool(g)# Non-empty set goes inside bool() methodh = {1, 2, 3, 4}print "Non-empty set:",bool(h)# Non-empty dictionary goes inside bool() methodi = {"Language": "Python"}print "Non-empty dictionary:",bool(i)