Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
bool
communitycreator

What is the bool() method in Python?

Mahnoor Zaheer

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The bool() method returns either True or False, based on the specified object. If no object is passed, it returns False by default.

Syntax

The bool() method is declared as follows:

The syntax of bool() method in Python

Parameter

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.

Return value

True

The bool() method returns True if one of the following parameters is passed in it:

  • a negative or positive integer.
  • a non-empty string, list, dictionary, or set.
  • a True value.

False

The bool() method returns False in the following cases:

  • 0 (zero)
  • an empty string, list, dictionary, or set.
  • a False value.
  • no parameter is passed.

Code

Example 1

In the example below, the bool() method will return False.

# Default case: no parameter passed to bool() method
print "Default case:",bool()
# Integer 0 goes inside bool() method
x = 0
print "Integer 0:",bool(x)
# Empty string goes inside bool() method
y = ""
print "Empty string:",bool(y)
# Empty set goes inside bool() method
i = set()
print "Empty set:",bool(i)
# Empty list goes inside bool() method
j = []
print "Empty list:",bool(j)
# Empty dictionary goes inside bool() method
k = {}
print "Empty dictionary:",bool(k)

Example 2

The example below shows the instances where the bool() method returns True.

# A true value goes inside bool() method
check = True
print "The check is",bool(check)
# Positive integer goes inside bool() method
a = 1
print "Positive integer:",bool(a)
# Negative integer goes inside bool() method
b = -6
print "Negative Integer:",bool(b)
# Non-empty string goes inside bool() method
c = "Hello"
print "Non-empty string:",bool(c)
# Non-empty list goes inside bool() method
g = [1,2,3]
print "Non-empty list:",bool(g)
# Non-empty set goes inside bool() method
h = {1, 2, 3, 4}
print "Non-empty set:",bool(h)
# Non-empty dictionary goes inside bool() method
i = {"Language": "Python"}
print "Non-empty dictionary:",bool(i)

RELATED TAGS

python
bool
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring