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:
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Python String capitalize()
Python del Statement
Deep Learning with Applications Using Python - Navin Kumar Manaswi
Python sorted()
Python isinstance()
Python Program to Add Two Matrices
Python Program to Make a Simple Calculator
Python Program to Find the Factors of a Number
Python Program to Convert String to Datetime
Python String endswith()
Python classmethod()
Python Iterators
Python map()
Node.js vs Python for Backend Development
Machine Learning with Python for everyone - Mark E.Fenner
Python Set discard()
Python zip()
Deep Learning with Python - Francois Chollet
Python complex()
Python any()
Python compile()
Python Program to Compute all the Permutation of the String
Python Program to Iterate Through Two Lists in Parallel
Python hash()
Python divmod()
Python delattr()
Python Set difference()
Python print()
Python String isdecimal()
Python Program to Check the File Size
Python setattr()