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 Custom Exceptions
Python Program to Find Numbers Divisible by Another Number
Python Set add()
Python Recursion
Python Machine Learning - Sebastian Raschka
Python Set update()
Python Anonymous / Lambda Function
Python frozenset()
Python Program to Count the Occurrence of an Item in a List
Python Operator Overloading
Python String isalpha()
Python time Module
Python Program to Return Multiple Values From a Function
APIs in Node.js vs Python - A Comparison
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Python complex()
Python property()
Python Program to Find the Square Root
Deep Learning in Python - LazyProgrammer
Python Program to Shuffle Deck of Cards
Deep Learning with Python - Francois Chollet
Python Program to Get a Substring of a String
Python Program to Create Pyramid Patterns
Python List pop()
Python Program to Get Line Count of a File
Python globals()
Python Program to Check If Two Strings are Anagram
Python Program to Find HCF or GCD
Python Directory and Files Management
Python Program to Check Prime Number
Python max()