The bool function in python takes a value as input and returns its boolean value ie; either True or False as per boolen convention.
bool([value]) #where value any object,like String,List,Number etc.
The bool function takes only a parameter. If a value is passed as a parameter, its equivalent boolean value will be returned as either True or False.
| Parameter | Description | Required / Optional |
|---|---|---|
| Value or empty | Bool takes only a parameter if no parameter passed return False | Optional |
On passing any other inputs rather than the ones mentioned in the table, returns True.
| Input | Output |
|---|---|
| None | False |
| False | False |
| Numeric having value equivalent to zero | False |
| Empty iterables like (),{},[] etc | False |
| Classes having __len__ or __bool__ methods returning 0/False | False |
eg_string = ‘0’
print(bool(eg_string))
Output:
True
iterable = [False]
print(bool(iterable))
Output:
True
empty_iterable = []
print(bool(empty_iterable))
Output:
False