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:
Python Program to Find the Factors of a Number
Python Program to Get the Full Path of the Current Working Directory
Python Objects and Classes
Python Program to Shuffle Deck of Cards
Python Inheritance
Python Set pop()
Python time Module
Python String count()
Python next()
Python Program to Find the Size (Resolution) of a Image
Python Program to Remove Punctuations From a String
Python Program to Find HCF or GCD
Python Program to Check Whether a String is Palindrome or Not
Python Strings
Python String lower()
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python zip()
Python isinstance()
Python Program to Copy a File
Python Operator Overloading
Python Matrices and NumPy Arrays
Python List copy()
Python Program to Get the Class Name of an Instance
Python format()
Python id()
Python Machine Learning Second Edition - Sebastian Raschka & Vahid Mirjalili
Python Set difference()
Python String endswith()
Python del Statement
Python vars()
Python Dictionary copy()
Python Program to Compute all the Permutation of the String