12-06-2020, 02:45 AM
Python bool() Function
Python’s built-in function converts value to a Boolean value or . It uses implicit Boolean conversion on the input argument . Any Python object has an associated truth value. The function takes only one argument, the object for which a Boolean value is desired.
Argument |
|
A Python object for which a Boolean value should be determined. Any Python object has an associated Boolean defined by the method.
Return Value |
|
Returns a Boolean value associated to the argument. The object will always return , unless:
The object is empty, like , ,
The object is
The object is or
The object is
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!
![[Image: image-1.png]](https://blog.finxter.com/wp-content/uploads/2020/02/image-1.png)
The book is 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 function on different input arguments that all lead to results.
The following list of executions of the function all result in Boolean values of .
You can observe multiple properties of the function:
Summary
Python’s built-in function converts value to a Boolean value or .
It uses implicit Boolean conversion on the input argument.
Any Python object has an associated truth value.
The 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.
Join the free webinar now!
The post Python bool() Function first appeared on Finxter.
Python’s built-in
Code:
bool(x)Code:
xCode:
TrueCode:
FalseCode:
xCode:
bool(x)Argument |
Code:
xA Python object for which a Boolean value should be determined. Any Python object has an associated Boolean defined by the method
Code:
object.__bool__()Return Value |
Code:
True, FalseReturns a Boolean value associated to the argument
Code:
xCode:
True
The object is empty, like Code:
[]Code:
()Code:
{}
The object is Code:
False
The object is or Code:
0.0
The object is Code:
NoneCode:
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 : FalseBut 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!
![[Image: image-1.png]](https://blog.finxter.com/wp-content/uploads/2020/02/image-1.png)
The book is 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
Code:
bool(x)Code:
TrueCode:
#####################
# 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 examplesThe following list of executions of the function
Code:
bool(x)Code:
FalseCode:
#####################
# 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 examplesYou can observe multiple properties of the
Code:
bool()- You can pass any object into it and it will always return a Boolean value because all Python objects implement the method and have an associated implicit Boolean value. You can use them to test a condition:Code:
__bool__()(example ternary operator).Code:0 if x else 1
- The vast majority of objects are converted to . Semantically, this means that they’re non-empty or whole.Code:
True
- A minority of objects convert to . These are the “empty” values—for example, empty lists, empty sets, empty tuples, or an empty number 0.Code:
False
Summary
Python’s built-in
Code:
bool(x)Code:
xCode:
TrueCode:
FalseIt uses implicit Boolean conversion on the input argument
Code:
xAny Python object has an associated truth value.
The
Code:
bool(x)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.
Join the free webinar now!
The post Python bool() Function first appeared on Finxter.

