![]() |
|
[Tut] Python bool() Function - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] Python bool() Function (/thread-98615.html) |
[Tut] Python bool() Function - xSicKxBot - 12-06-2020 Python bool() Function 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! ![]() 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()
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. |