The bool() function converts a value to Boolean (True or False) using the standard truth testing procedure.
The syntax of bool()
is:
bool([value])
1. bool() parameters
It’s not mandatory to pass a value to bool()
. If you do not pass a value, bool()
returns False
.
In general use, bool()
takes a single parameter value
.
2. Return Value from bool()
bool()
returns:
False
if the value is omitted or falseTrue
if the value is true
The following values are considered false in Python:
None
False
- Zero of any numeric type. For example,
0
,0.0
,0j
- Empty sequence. For example,
()
,[]
,''
. - Empty mapping. For example,
{}
- objects of Classes which has
__bool__()
or__len()__
method which returns0
orFalse
All other values except these values are considered true.
3. Example: How bool() works?
test = [] print(test,'is',bool(test)) test = [0] print(test,'is',bool(test)) test = 0.0 print(test,'is',bool(test)) test = None print(test,'is',bool(test)) test = True print(test,'is',bool(test)) test = 'Easy string' print(test,'is',bool(test))
Output
[] is False [0] is True 0.0 is False None is False True is True Easy string is True
Related posts:
Python Program to Convert Two Lists Into a Dictionary
Python Program to Represent enum
Python List index()
Python Program to Extract Extension From the File Name
Python Program to Display Fibonacci Sequence Using Recursion
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Python eval()
Python String capitalize()
Python format()
Python Program to Find Numbers Divisible by Another Number
Python Program to Check If a List is Empty
Python dict()
Python Program to Calculate the Area of a Triangle
Python Program to Access Index of a List Using for Loop
How to Get Started With Python?
Python bin()
Python Program to Shuffle Deck of Cards
Python Dictionary clear()
Python pow()
Python String center()
Python Closures
Python reversed()
Python Decorators
Python range()
Deep Learning with Python - Francois Cholletf
Python Machine Learning Eqution Reference - Sebastian Raschka
Python Program to Convert Kilometers to Miles
Python Program to Convert Decimal to Binary Using Recursion
Python String isalnum()
Python Dictionary setdefault()
Python Program to Check Leap Year
Python slice()