Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python bool() Function

#1
Python bool() Function

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 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 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.

Join the free webinar now!

The post Python bool() Function first appeared on Finxter.



https://www.sickgaming.net/blog/2020/12/...-function/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] Python Async Function xSicKxBot 0 1,278 05-08-2023, 04:28 AM
Last Post: xSicKxBot
  [Tut] How to Return a File From a Function in Python? xSicKxBot 0 1,332 10-21-2022, 09:47 AM
Last Post: xSicKxBot
  [Tut] Python – Return NumPy Array From Function xSicKxBot 0 1,191 10-16-2022, 03:49 AM
Last Post: xSicKxBot
  [Tut] Python Return String From Function xSicKxBot 0 1,203 10-14-2022, 01:13 PM
Last Post: xSicKxBot
  [Tut] How to Convert Bool (True/False) to a String in Python? xSicKxBot 0 1,308 10-04-2022, 11:37 AM
Last Post: xSicKxBot
  [Tut] (Fixed) Python TypeError ‘bool’ object is not subscriptable xSicKxBot 0 1,442 10-02-2022, 12:23 PM
Last Post: xSicKxBot
  [Tut] How to Apply a Function to a List xSicKxBot 0 1,249 09-13-2022, 07:21 PM
Last Post: xSicKxBot
  [Tut] How to Apply a Function to Each Cell in a Pandas DataFrame? xSicKxBot 0 1,076 08-23-2022, 05:25 PM
Last Post: xSicKxBot
  [Tut] How to Calculate a Logistic Sigmoid Function in Python? xSicKxBot 0 1,253 05-31-2022, 01:27 PM
Last Post: xSicKxBot
  [Tut] A Guide to Python’s pow() Function xSicKxBot 0 1,315 12-22-2020, 11:24 AM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016