bool() returns the boolean value of a specified object.
Example:
result = 1
print(result,'is',bool(result))
"""
Output:
1 is True
"""
Syntax
bool(object)
Parameters
bool() takes a single parameter; an object, i.e. string, list, number, etc.
Return Value
- True – If the object is any non-zero number, non-empty iterable, True or a string
- False – If the object is empty iterable (list, tuple, etc.), 0, False, or None.
Example
Use of bool()
# bool() with non-zero integer number
t1 = 155
print(t1,'is',bool(t1))
# bool() with zero
t2 = 0
print(t2,'is',bool(t2))
# bool() with floating-point number
t3 = 3.25
print(t3,'is',bool(t3))
# bool() with string
t4 = 'CodingDots'
print(t4,'is',bool(t4))
# bool() with empty string
t5 = ''
print('Empty string',t5,'is',bool(t5))
# bool() with Boolean - True
t6 = True
print(t6,'is',bool(t6))
# bool() with Boolean - False
t7 = False
print(t7,'is',bool(t7))
# bool() with None
t8 = None
print(t8,'is',bool(t8))
# bool() with list (iterable)
t9 = [1,2,3,4]
print(t9,'is',bool(t9))
# bool() with empty list (iterable)
t10 = []
print(t10,'is',bool(t10))
Output
155 is True
0 is False
3.25 is True
CodingDots is True
Empty string is False
True is True
False is False
None is False
[1, 2, 3, 4] is True
[] is False