bool(): converts a given value to a Boolean (either True or False)
bool(object)
object :input any object
Returns boolean vlaue ( True or False )
Examples
x=10
y=bool(x)
print(y) # True
x='12'
y=bool(x)
print(y) # True
x='' # empty string
y=bool(x)
print(y) # False
x=0
y=bool(x)
print(y) # False
x=1
y=bool(x)
print(y) # True
x=[] # empty list
y=bool(x)
print(y) # False
x=() # empty tuple
y=bool(x)
print(y) # False
x={} # empty set
y=bool(x)
print(y) # False
x=None
y=bool(x)
print(y) # False
Bool data type
We can use type() to know more about boolean data type.
x=4
y=bool(x)
print(type(y)) # <class 'bool'>
bool() will return False for following inputs
empty string
empty list
any empty iterable object
None
0
False
«All Built in Functions in Python
« max()
Bitwise operators using bin() »
any() »
← Subscribe to our YouTube Channel here
This article is written by plus2net.com team.
https://www.plus2net.com
plus2net.com