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:
Falseif the value is omitted or falseTrueif the value is true
The following values are considered false in Python:
NoneFalse- 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 returns0orFalse
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:
How to Get Started With Python?
Python del Statement
Python Dictionary pop()
Python Program to Create Pyramid Patterns
Python Program to Iterate Over Dictionaries Using for Loop
Python Tuple count()
Python String rindex()
Python Dictionary
Python Program to Get the File Name From the File Path
Python String strip()
Python Exception Handling Using try, except and finally statement
Python Program to Extract Extension From the File Name
Learning scikit-learn Machine Learning in Python - Raul Garreta & Guillermo Moncecchi
Python Program to Get Line Count of a File
Deep Learning with Python - Francois Chollet
Python String isspace()
Python Program to Count the Occurrence of an Item in a List
Python round()
Python Program to Remove Punctuations From a String
Python Program to Split a List Into Evenly Sized Chunks
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
Introduction to Machine Learning with Python - Andreas C.Muller & Sarah Guido
Python ascii()
Python datetime
Python range()
Python Program to Display Fibonacci Sequence Using Recursion
Python print()
Python Get Current time
Python Artificial Intelligence Project for Beginners - Joshua Eckroth
Python String find()
Python abs()
Python Program to Print Colored Text to the Terminal