Create an account


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

#1
Python bool() Function

<div><p class="has-pale-cyan-blue-background-color has-background">Python’s built-in <code>bool(x)</code> function converts value <code>x</code> to a Boolean value <code>True</code> or <code>False</code>. It uses implicit Boolean conversion on the input argument <code>x</code>. Any Python object has an associated truth value. The <code>bool(x)</code> function takes only one argument, the object for which a Boolean value is desired.</p>
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio">
<div class="wp-block-embed__wrapper">
<div class="ast-oembed-container"><iframe title="Python bool() - Everything You Need to Know and More" width="1400" height="788" src="https://www.youtube.com/embed/qGg_z33J_b4?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></div>
</div>
</figure>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><strong>Argument</strong></td>
<td><code>x</code></td>
<td>A Python object for which a Boolean value should be determined. Any Python object has an associated Boolean defined by the method <code>object.__bool__()</code>. </td>
</tr>
<tr>
<td><strong>Return Value</strong></td>
<td><code>True, False</code></td>
<td>Returns a Boolean value associated to the argument <code>x</code>. The object will always return <code>True</code>, unless:<br /><img src="https://s.w.org/images/core/emoji/13.0.0/72x72/2b50.png" alt="⭐" class="wp-smiley" style="height: 1em; max-height: 1em;" /> The object is empty, like <code>[]</code>, <code>()</code>, <code>{}</code><br /><img src="https://s.w.org/images/core/emoji/13.0.0/72x72/2b50.png" alt="⭐" class="wp-smiley" style="height: 1em; max-height: 1em;" />The object is <code>False</code><br /><img src="https://s.w.org/images/core/emoji/13.0.0/72x72/2b50.png" alt="⭐" class="wp-smiley" style="height: 1em; max-height: 1em;" />The object is <code>0</code> or <code>0.0</code><br /><img src="https://s.w.org/images/core/emoji/13.0.0/72x72/2b50.png" alt="⭐" class="wp-smiley" style="height: 1em; max-height: 1em;" />The object is <code>None</code></td>
</tr>
</tbody>
</table>
</figure>
<pre class="wp-block-preformatted"><strong>Input </strong>: <code>bool(1)</code>
<strong>Output </strong>: <code>True</code> <strong>Input </strong>: <code>bool(0)</code>
<strong>Output </strong>: <code>False</code> <strong>Input </strong>: <code>bool(True)</code>
<strong>Output </strong>: <code>True</code> <strong>Input </strong>: <code>bool([1, 2, 3])</code>
<strong>Output </strong>: <code>True</code> <strong>Input </strong>: <code>bool([])</code>
<strong>Output </strong>: <code>False</code></pre>
<hr class="wp-block-separator"/>
<p><strong>But before we move on, I’m excited to present you my brand-new Python book <a rel="noreferrer noopener" href="https://amzn.to/2WAYeJE" target="_blank" title="https://amzn.to/2WAYeJE">Python One-Liners</a></strong> (Amazon Link).</p>
<p>If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a <strong>single line of Python code.</strong> But it’s also an <strong>introduction to computer science</strong>, data science, machine learning, and algorithms. <strong><em>The universe in a single line of Python!</em></strong></p>
<div class="wp-block-image">
<figure class="aligncenter"><a href="https://amzn.to/2WAYeJE" target="_blank" rel="noopener noreferrer"><img loading="lazy" width="215" height="283" src="https://blog.finxter.com/wp-content/uploads/2020/02/image-1.png" alt="" class="wp-image-5969"/></a></figure>
</div>
<p>The book is released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco). </p>
<p>Link: <a href="https://nostarch.com/pythononeliners" target="_blank" rel="noreferrer noopener">https://nostarch.com/pythononeliners</a></p>
<h2>Examples bool() Functions</h2>
<p>The following code shows you how to use the <code>bool(x)</code> function on different input arguments that all lead to<code> True</code> results. </p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#####################
# 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</pre>
<p>The following list of executions of the function <code>bool(x)</code> all result in Boolean values of <code>False</code>. </p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#####################
# 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</pre>
<p>You can observe multiple properties of the <code>bool()</code> function:</p>
<ul>
<li>You can pass any object into it and it will always return a Boolean value because all Python objects implement the <code>__bool__()</code> method and have an associated implicit Boolean value. You can use them to test a condition: <code>0 if x else 1</code> (example <a href="https://blog.finxter.com/python-one-line-ternary/" target="_blank" rel="noreferrer noopener" title="Python One Line Ternary">ternary operator</a>). </li>
<li>The vast majority of objects are converted to <code>True</code>. Semantically, this means that they’re non-empty or whole. </li>
<li>A minority of objects convert to <code>False</code>. These are the “empty” values—for example, empty <a href="https://blog.finxter.com/python-lists/" target="_blank" rel="noreferrer noopener" title="The Ultimate Guide to Python Lists">lists</a>, empty <a href="https://blog.finxter.com/sets-in-python/" target="_blank" rel="noreferrer noopener" title="The Ultimate Guide to Python Sets – with Harry Potter Examples">sets</a>, empty <a href="https://blog.finxter.com/the-ultimate-guide-to-python-tuples/" title="The Ultimate Guide To Python Tuples">tuples</a>, or an empty number 0. </li>
</ul>
<h2>Summary</h2>
<p>Python’s built-in <code>bool(x)</code> function converts value <code>x</code> to a Boolean value <code>True</code> or <code>False</code>. </p>
<p>It uses <a href="https://blog.finxter.com/how-to-convert-a-string-to-a-boolean-in-python/" title="How to Convert a String to a Boolean in Python?" target="_blank" rel="noreferrer noopener">implicit Boolean conversion</a> on the input argument <code>x</code>.</p>
<p>Any Python object has an associated truth value. </p>
<p>The <code>bool(x)</code> function takes only one argument, the object for which a Boolean value is desired.</p>
<h2>Where to Go From Here?</h2>
<p>Enough theory, let’s get some practice!</p>
<p>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?</p>
<p><strong>Practice projects is how you sharpen your saw in coding!</strong></p>
<p>Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?</p>
<p>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.</p>
<p>Join my free webinar <a rel="noreferrer noopener" href="https://blog.finxter.com/webinar-freelancer/" target="_blank">“How to Build Your High-Income Skill Python”</a> and watch how I grew my coding business online and how you can, too—from the comfort of your own home.</p>
<p><a href="https://blog.finxter.com/webinar-freelancer/" target="_blank" rel="noreferrer noopener">Join the free webinar now!</a></p>
<p>The post <a href="https://blog.finxter.com/python-bool/" target="_blank" rel="noopener noreferrer">Python bool() Function</a> first appeared on <a href="https://blog.finxter.com/" target="_blank" rel="noopener noreferrer">Finxter</a>.</p>
</div>


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



Forum Jump:


Users browsing this thread:
3 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016