{"id":121627,"date":"2020-12-05T18:22:36","date_gmt":"2020-12-05T18:22:36","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=17841"},"modified":"2020-12-05T18:22:36","modified_gmt":"2020-12-05T18:22:36","slug":"python-bool-function","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/12\/05\/python-bool-function\/","title":{"rendered":"Python bool() Function"},"content":{"rendered":"<p class=\"has-pale-cyan-blue-background-color has-background\">Python&#8217;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>\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\">\n<div class=\"wp-block-embed__wrapper\">\n<div class=\"ast-oembed-container\"><iframe loading=\"lazy\" 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>\n<\/div>\n<\/figure>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><strong>Argument<\/strong><\/td>\n<td><code>x<\/code><\/td>\n<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>\n<\/tr>\n<tr>\n<td><strong>Return Value<\/strong><\/td>\n<td><code>True, False<\/code><\/td>\n<td>Returns a Boolean value associated to the argument <code>x<\/code>. The object will always return <code>True<\/code>, unless:<br \/><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/13.0.0\/72x72\/2b50.png\" alt=\"\u2b50\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/>\u00a0The object is empty, like <code>[]<\/code>, <code>()<\/code>, <code>{}<\/code><br \/><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/13.0.0\/72x72\/2b50.png\" alt=\"\u2b50\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/>The object is <code>False<\/code><br \/><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/13.0.0\/72x72\/2b50.png\" alt=\"\u2b50\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/>The object is <code>0<\/code> or <code>0.0<\/code><br \/><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/13.0.0\/72x72\/2b50.png\" alt=\"\u2b50\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/>The object is <code>None<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<pre class=\"wp-block-preformatted\"><strong>Input <\/strong>: <code>bool(1)<\/code>\n<strong>Output <\/strong>: <code>True<\/code> <strong>Input <\/strong>: <code>bool(0)<\/code>\n<strong>Output <\/strong>: <code>False<\/code> <strong>Input <\/strong>: <code>bool(True)<\/code>\n<strong>Output <\/strong>: <code>True<\/code> <strong>Input <\/strong>: <code>bool([1, 2, 3])<\/code>\n<strong>Output <\/strong>: <code>True<\/code> <strong>Input <\/strong>: <code>bool([])<\/code>\n<strong>Output <\/strong>: <code>False<\/code><\/pre>\n<hr class=\"wp-block-separator\"\/>\n<p><strong>But before we move on, I&#8217;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>\n<p>If you like one-liners, you&#8217;ll LOVE the book. It&#8217;ll teach you everything there is to know about a <strong>single line of Python code.<\/strong> But it&#8217;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>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/amzn.to\/2WAYeJE\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" 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>\n<\/div>\n<p>The book is released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco). <\/p>\n<p>Link: <a href=\"https:\/\/nostarch.com\/pythononeliners\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/nostarch.com\/pythononeliners<\/a><\/p>\n<h2>Examples bool() Functions<\/h2>\n<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>\n<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=\"\">#####################\n# True Boolean Values\n##################### # All integers except 0\nprint(bool(1))\nprint(bool(2))\nprint(bool(42))\nprint(bool(-1)) # All collections except empty ones\n# (lists, tuples, sets)\nprint(bool([1, 2]))\nprint(bool([-1]))\nprint(bool((-1, -2)))\nprint(bool({1, 2, 3})) # All floats except 0.0\nprint(bool(0.1))\nprint(bool(0.0000001))\nprint(bool(3.4)) # Output is True for all previous examples<\/pre>\n<p>The following list of executions of the function <code>bool(x)<\/code> all result in Boolean values of <code>False<\/code>. <\/p>\n<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=\"\">#####################\n# False Boolean Values\n##################### # Integer 0\nprint(bool(0)) # Empty collections\n# (lists, tuples, sets)\nprint(bool([]))\nprint(bool({}))\nprint(bool(())) # Float 0.0\nprint(bool(0.0)) # Output is False for all previous examples<\/pre>\n<p>You can observe multiple properties of the <code>bool()<\/code> function:<\/p>\n<ul>\n<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>\n<li>The vast majority of objects are converted to <code>True<\/code>. Semantically, this means that they&#8217;re non-empty or whole. <\/li>\n<li>A minority of objects convert to <code>False<\/code>. These are the &#8220;empty&#8221; values&#8212;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 \u2013 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>\n<\/ul>\n<h2>Summary<\/h2>\n<p>Python&#8217;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>\n<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>\n<p>Any Python object has an associated truth value. <\/p>\n<p>The <code>bool(x)<\/code> function takes only one argument, the object for which a Boolean value is desired.<\/p>\n<h2>Where to Go From Here?<\/h2>\n<p>Enough theory, let\u2019s get some practice!<\/p>\n<p>To become successful in coding, you need to get out there and solve real problems for real people. That\u2019s how you can become a six-figure earner easily. And that\u2019s how you polish the skills you really need in practice. After all, what\u2019s the use of learning theory that nobody ever needs?<\/p>\n<p><strong>Practice projects is how you sharpen your saw in coding!<\/strong><\/p>\n<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>\n<p>Then become a Python freelance developer! It\u2019s the best way of approaching the task of improving your Python skills\u2014even if you are a complete beginner.<\/p>\n<p>Join my free webinar <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\">\u201cHow to Build Your High-Income Skill Python\u201d<\/a> and watch how I grew my coding business online and how you can, too\u2014from the comfort of your own home.<\/p>\n<p><a href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\" rel=\"noreferrer noopener\">Join the free webinar now!<\/a><\/p>\n<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>\n","protected":false},"excerpt":{"rendered":"<p>Python&#8217;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 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-121627","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/121627","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=121627"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/121627\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=121627"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=121627"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=121627"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}