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 Check If Two Strings are Anagram
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python Program to Add Two Numbers
Introduction to Machine Learning with Python - Andreas C.Muller & Sarah Guido
Python timestamp to datetime and vice-versa
Python hash()
Python List index()
Python String rsplit()
Python Objects and Classes
Python Program to Shuffle Deck of Cards
Python Tuple index()
Python File I/O Operation
Python Program to Add Two Matrices
Python slice()
Python Program to Reverse a Number
Python Sets
Python Data Types
Intelligent Projects Using Python - Santanu Pattanayak
Python String rindex()
Python Global Keyword
Python Program to Get the Last Element of the List
Python for Programmers with introductory AI case studies - Paul Deitel & Harvey Deitel
Python Functions
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
Python hasattr()
Python String center()
How to Get Started With Python?
Python Set issuperset()
Python Artificial Intelligence Project for Beginners - Joshua Eckroth
Python Program to Get the File Name From the File Path
Python Program to Make a Simple Calculator
Python Program to Count the Number of Occurrence of a Character in String