bool() in Python
In Python, bool() is a built-in function that is used to convert a value to a Boolean (i.e., True or False). The Boolean data type represents truth values and is a fundamental concept in programming, often used in conditional statements, loops and logical operations.
bool() function evaluates the truthness or falseness of a given value and returns either True or False. Understanding how bool() works is crucial for writing effective and efficient Python code.
Example:
x = bool(1)
print(x)
y = bool()
print(y)
Output
True False
Syntax of bool() function
bool([x])
Parameters
- x: represents the value that we want to convert to a Boolean. If no argument is provided,
bool()
returnsFalse
by default.
Return Value of bool()
The bool() function returns:
- True if the value x is considered “truth”
- False if the value x is considered “false.”
Truth and False Values in Python
In Python, certain values are considered “truth” or “false” based on their inherent properties. Here’s a breakdown:
False Values:
- False: The Boolean value False.
- None: The None keyword, which represents the absence of a value.
- 0: The integer zero.
- 0.0: The floating-point zero.
- “”: An empty string.
- []: An empty list.
- (): An empty tuple.
- {}: An empty dictionary.
- set(): An empty set.
- range(0): An empty range.
Truth Values:
- Non-zero numbers (e.g.,
1
,-1
,3.14
). - Non-empty strings (e.g.,
"hello"
). - Non-empty collections (e.g.,
[1, 2, 3]
,{"key": "value"}
). - Objects (e.g., instances of classes).
Examples of bool()
Example 1: Basic usage
print(bool(True))
print(bool(False))
print(bool(0))
print(bool(1))
print(bool(-1))
print(bool(0.0))
print(bool(3.14))
print(bool(""))
print(bool("Hello"))
print(bool([]))
print(bool([1, 2]))
print(bool({}))
print(bool({"a": 1}))
print(bool(None))
Output
True False False True True False True False True False True False True False
Explanation:
- bool(True) returns True because True is already a Boolean value.
- bool(False) returns False because False is already a Boolean value.
- bool(0) returns False because 0 is a false value.
- bool(1) returns True because 1 is a truth value.
- bool(-1) returns True because any non-zero number is truth.
- bool(0.0) returns False because 0.0 is a false value.
- bool(3.14) returns True because 3.14 is a non-zero number.
- bool(“”) returns False because an empty string is false.
- bool(“Hello”) returns True because a non-empty string is truth.
- bool([]) returns False because an empty list is false.
- bool([1, 2]) returns True because a non-empty list is truth.
- bool({}) returns False because an empty dictionary is false.
- bool({“a”: 1}) returns True because a non-empty dictionary is truth.
- bool(None) returns False because None is a false value.
Example 2: Using bool() in conditional statement
value = 10
if bool(value):
print("The value is truth.")
else:
print("The value is false.")
Output
The value is truth.
Explanation:
- The
bool(value)
function is used to check ifvalue
is truth or false. - Since
value
is10
, which is a non-zero number,bool(value)
returnsTrue
. - Therefore, the
if
block is executed, and the message “The value is truth.” is printed.
Example 3: Using bool() with custom objects
class gfg:
def __init__(self, value):
self.value = value
def __bool__(self):
return bool(self.value)
obj1 = gfg(0)
obj2 = gfg(42)
print(bool(obj1))
print(bool(obj2))
Output
False True
Explanation:
- In this example, we define a custom class
g
fg with an__init__
method and a__bool__
method. - The
__bool__
method is a special method in Python that defines the truthiness of an object whenbool()
is called on it. - When
bool(obj1)
is called, the__bool__
method ofobj1
is invoked, which returnsbool(self.value)
. Sinceself.value
is0
,bool(obj1)
returnsFalse
. - Similarly,
bool(obj2)
returnsTrue
becauseself.value
is42
, which is a non-zero number.
Python bool() function to check odd and even number
Here is a program to find out even and odd by the use of the bool() method. You may use other inputs and check out the results.
def check_even(num):
return bool(num % 2 == 0)
num = 8
if check_even(num):
print("Even")
else:
print("Odd")
Output
Even
Explanation:
- num % 2 == 0 this checks if the number is divisible by 2.
- bool(num % 2 == 0) converts the result into True (even) or False (odd).
- conditional check if True, prints “Even” otherwise “Odd”.