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 Set difference_update()
Python reversed()
Python Program to Find ASCII Value of Character
Python Program to Count the Number of Digits Present In a Number
Python __import__()
Python Program to Convert Decimal to Binary Using Recursion
Python property()
Python zip()
Python Function Arguments
Python Dictionary popitem()
Python id()
Python Program to Concatenate Two Lists
Statistical Methods for Machine Learning - Disconver how to Transform data into Knowledge with Pytho...
Python compile()
Python Object Oriented Programming
Intelligent Projects Using Python - Santanu Pattanayak
Python Program to Sort a Dictionary by Value
Introduction to Scientific Programming with Python - Joakim Sundnes
Python Program to Check If a List is Empty
Python Custom Exceptions
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Python List pop()
Python Tuple count()
Python Program to Convert String to Datetime
Python Namespace and Scope
Python String format_map()
Python sleep()
Python Set copy()
Python str()
Python Variables, Constants and Literals
Python strftime()
Python Objects and Classes