Python’s built-in bool(x)
function converts value x
to a Boolean value True
or False
. It uses implicit Boolean conversion on the input argument x
. Any Python object has an associated truth value. The bool(x)
function takes only one argument, the object for which a Boolean value is desired.
Argument | x | A Python object for which a Boolean value should be determined. Any Python object has an associated Boolean defined by the method object.__bool__() . |
Return Value | True, False | Returns a Boolean value associated to the argument x . The object will always return True , unless:⭐ The object is empty, like [] , () , {} ⭐The object is False ⭐The object is 0 or 0.0 ⭐The object is None |
Input :bool(1)
Output :True
Input :bool(0)
Output :False
Input :bool(True)
Output :True
Input :bool([1, 2, 3])
Output :True
Input :bool([])
Output :False
Check out my new Python book Python One-Liners (Amazon Link).
If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python!
The book was released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).
Publisher Link: https://nostarch.com/pythononeliners
Examples bool() Functions
The following code shows you how to use the bool(x)
function on different input arguments that all lead to True
results.
##################### # True Boolean Values ##################### # All integers except 0 print(bool(1)) print(bool(2)) print(bool(42)) print(bool(-1)) # All collections except empty ones # (lists, tuples, sets) print(bool([1, 2])) print(bool([-1])) print(bool((-1, -2))) print(bool({1, 2, 3})) # All floats except 0.0 print(bool(0.1)) print(bool(0.0000001)) print(bool(3.4)) # Output is True for all previous examples
The following list of executions of the function bool(x)
all result in Boolean values of False
.
##################### # False Boolean Values ##################### # Integer 0 print(bool(0)) # Empty collections # (lists, tuples, sets) print(bool([])) print(bool({})) print(bool(())) # Float 0.0 print(bool(0.0)) # Output is False for all previous examples
You can observe multiple properties of the bool()
function:
- You can pass any object into it and it will always return a Boolean value because all Python objects implement the
__bool__()
method and have an associated implicit Boolean value. You can use them to test a condition:0 if x else 1
(example ternary operator). - The vast majority of objects are converted to
True
. Semantically, this means that they’re non-empty or whole. - A minority of objects convert to
False
. These are the “empty” values—for example, empty lists, empty sets, empty tuples, or an empty number 0.
👉 Recommended Tutorial: Python Function Returns Boolean
Summary
Python’s built-in bool(x)
function converts value x
to a Boolean value True
or False
.
It uses implicit Boolean conversion on the input argument x
.
Any Python object has an associated truth value.
The bool(x)
function takes only one argument, the object for which a Boolean value is desired.
Where to Go From Here?
Enough theory. Let’s get some practice!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.