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
But before we move on, I’m excited to present you my brand-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).
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.
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!
To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And 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?
Practice projects is how you sharpen your saw in coding!
Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?
Then become 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.
Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.