Home »
Python
bool() function with example in Python
Python bool() function: Here, we are going to learn about the bool() function in Python with example.
Submitted by IncludeHelp, on April 01, 2019
Python bool() function
bool() function is used to convert a given value to the Boolean value (True or False) as per the standard truth testing procedures. It accepts a value (like an integer, list, maps, etc) and converts it into a Boolean value.
Some of the examples:
- None – converts to False
- False – converts to False
- Zeros (0, 0.0, 0j) – converts to False
- Empty sequences like, (), [], ' ' – converts to False
Syntax:
bool([value])
Parameter: value – a value to be converted to the Boolean value, it’s an optional parameter, if we do not pass any parameter – it returns False.
Return value: bool – a Boolean value
Example:
Input:
val = False
print("val = ", bool(val))
val = True
print("val = ", bool(val))
val = 10
print("val = ", bool(val))
val = 0
print("val = ", bool(val))
Output:
val = False
val = True
val = True
val = False
Python code to convert value to the Boolean value
# python code to demonstrate example
# of bool() function
val = False
print("val = ", bool(val))
val = True
print("val = ", bool(val))
val = 10
print("val = ", bool(val))
val = 0
print("val = ", bool(val))
val = 10.23
print("val = ", bool(val))
val = 0.0
print("val = ", bool(val))
val = "Hello"
print("val = ", bool(val))
val = []
print("val = ", bool(val))
Output
val = False
val = True
val = True
val = False
val = True
val = False
val = True
val = False
ADVERTISEMENT
ADVERTISEMENT