The bool() function is one of the built in methods in python which outputs the Boolean value depending on the input parameters. The Boolean values True or False are returned from the single argument. The python bool() method converts value to boolean (True or False) using the standard truth testing procedure.
Python bool() Function Syntax
It has the following syntax:
bool([value])
Parameters
It is not mandatory to pass value to bool(). If you do not pass the value, bool() returns False.
In general , bool() takes a single parameter value.
Return
The bool() returns:
- Returns False if the value is omitted or false and the condition is not met.
- Returns True if the value is true and condition is met.
Python bool() Function Example 1
Let us take an example to demonstrate the bool() function in Python.
# Python program example for bool() Function
# Creating an empty list
test = []
# Printing the value of the list and its boolean value
print(test, 'is', bool(test))
# Creating a list with one item (0)
test = [0]
# Printing the value of the list and its boolean value
print(test, 'is', bool(test))
# Creating a float value of 0.0
test = 0.0
# Printing the value of the float and its boolean value
print(test, 'is', bool(test))
# Creating a None value
test = None
# Printing the value of None and its boolean value
print(test, 'is', bool(test))
# Creating a boolean value of True
test = True
# Printing the value of True and its boolean value
print(test, 'is', bool(test))
# Creating a string value
test = 'Easy string'
# Printing the value of the string and its boolean value
print(test, 'is', bool(test))
Output:
[] is False
[] is False
[0] is True
0.0 is False
None is False
True is True
Easy string is True
Python bool() Function Example 2
Let’s take anothe example to demonstrate the bool() function with if-else statements in Python.
num = 12
if num > 15:
print(" number is greater than 15")
else:
print(" number is less than or equal to 15")
Output:
number is less than or equal to 15
Explanation:
In the above code, If the condition is true or false, the respective block is executed.
Now, lets see the explicit conversion of value into Boolean value in the below example.
# Using bool() function to convert the condition to a boolean value
Example:
num = 12
if bool(num > 15):
print(" number is greater than 15")
else:
print(" number is less than or equal to 15")
Output:
number is less than or equal to 15
Python bool() Function Example 3
1. Using bool() function to check if a value is True or False
Example:
val = " Javatpoint "
if bool(val):
print("True")
else:
print("False")
Output:
True
2. To Check if an empty string is True or False
Example:
emp_str = ""
if bool(emp_str):
print("True")
else:
print("False")
Output:
False
3. To Check if a zero value is True or False
Example:
zero_val = 0
if bool(zero_val):
print("True")
else:
print("False")
Output:
False
Conclusion:
All in all, the bool() function is a useful asset in Python for working with boolean values. It very well may be utilized to switch any value over completely to a boolean value expressly, and it can likewise be utilized to check in the event that a value is true or false.
Leave a Reply