bool() is a built-in function that converts a value to a Boolean (True or False). The Boolean data type is fundamental in programming and is commonly used in conditional statements, loops and logical operations. The bool() function evaluates the truthiness or falseness of a value and returns either True or False. Understanding bool() is essential for writing clean and efficient Python code.
x = bool(1)
print(x)
y = bool()
print(y)
Output
True False
Syntax:
bool([x])
Parameters:
- x: represents the value that we want to convert to a Boolean. If no argument is provided,Β bool()Β returnsΒ FalseΒ by default.
Return Value:
- 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()
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.
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 ifΒvalueΒ is truth or false. - SinceΒ
valueΒ isΒ10, which is a non-zero number,Βbool(value)Β returnsΒTrue. - Therefore, theΒ
ifΒ block is executed, and the message "The value is truth." is printed.
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Β gfg with anΒ __init__Β method and aΒ __bool__Β method.
- TheΒ __bool__Β method is a special method in Python that defines the truthiness of an object whenΒ bool()Β is called on it.
- WhenΒ bool(obj1)Β is called, theΒ __bool__Β method ofΒ obj1Β is invoked, which returnsΒ bool(self.value). SinceΒ self.valueΒ isΒ 0,Β bool(obj1)Β returnsΒ False.
- Similarly,Β bool(obj2)Β returnsΒ TrueΒ becauseΒ self.valueΒ isΒ 42, 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".