{"id":156,"date":"2020-10-03T23:51:23","date_gmt":"2020-10-03T23:51:23","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=156"},"modified":"2025-03-26T06:39:24","modified_gmt":"2025-03-26T06:39:24","slug":"python-boolean","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-basics\/python-boolean\/","title":{"rendered":"Python Boolean"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about the Python boolean data type, falsy and truthy values.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-python-boolean-data-type'>Introduction to Python Boolean data type <a href=\"#introduction-to-python-boolean-data-type\" class=\"anchor\" id=\"introduction-to-python-boolean-data-type\" title=\"Anchor for Introduction to Python Boolean data type\">#<\/a><\/h2>\n\n\n\n<p>In programming, you often want to check if a condition is true or not and perform some actions based on the result.<\/p>\n\n\n\n<p>To represent true and false, Python provides you with the boolean data type. The boolean value has a technical name as <code><a href=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-bool\/\">bool<\/a><\/code>.<\/p>\n\n\n\n<p>The boolean data type has two values: <code>True<\/code> and <code>False<\/code>. <\/p>\n\n\n\n<p>Note that the boolean values <code>True<\/code> and <code>False<\/code> start with the capital letters (<code>T<\/code>) and (<code>F<\/code>). <\/p>\n\n\n\n<p>The following example defines two boolean <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-variables\/\">variables<\/a>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">is_active = <span class=\"hljs-literal\">True<\/span>\nis_admin = <span class=\"hljs-literal\">False<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>When you compare two <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-numbers\/\">numbers<\/a>, Python returns the result as a boolean value. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">x = <span class=\"hljs-number\">20<\/span>\ny = <span class=\"hljs-number\">10<\/span>\n\nresult = x &gt; y\nprint(result)\n\nresult = x &lt; y\nprint(result)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=eCA9IDIwCnkgPSAxMAoKcmVzdWx0ID0geCA%2BIHkKcHJpbnQocmVzdWx0KQoKcmVzdWx0ID0geCA8IHkKcHJpbnQocmVzdWx0KQ%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">True<\/span>\n<span class=\"hljs-keyword\">False<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Also, comparing two <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-string\/\">strings<\/a> results in a boolean value:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">x = <span class=\"hljs-string\">'a'<\/span>\ny = <span class=\"hljs-string\">'b'<\/span>\n\nresult = x &gt; y\nprint(result)\n\nresult = x &lt; y\nprint(result)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=eCA9ICdhJwp5ID0gJ2InCgpyZXN1bHQgPSB4ID4geQpwcmludChyZXN1bHQpCgpyZXN1bHQgPSB4IDwgeQpwcmludChyZXN1bHQp\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">False<\/span>\n<span class=\"hljs-keyword\">True<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='the-bool-function'>The bool() function <a href=\"#the-bool-function\" class=\"anchor\" id=\"the-bool-function\" title=\"Anchor for The bool() function\">#<\/a><\/h2>\n\n\n\n<p>To find out if a value is <code>True<\/code> or <code>False<\/code>, you use the <code>bool()<\/code> function. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">result = bool(<span class=\"hljs-string\">'Hi'<\/span>)\nprint(result)\n\nresult = bool(<span class=\"hljs-number\">100<\/span>)\nprint(result)\n\nresult = bool(<span class=\"hljs-number\">0<\/span>)\nprint(result)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=cmVzdWx0ID0gYm9vbCgnSGknKQpwcmludChyZXN1bHQpCgpyZXN1bHQgPSBib29sKDEwMCkKcHJpbnQocmVzdWx0KQoKcmVzdWx0ID0gYm9vbCgwKQpwcmludChyZXN1bHQp\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">True<\/span>\n<span class=\"hljs-keyword\">True<\/span>\n<span class=\"hljs-keyword\">False<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The output indicates some values evaluate to <code>True<\/code> and the others evaluate to <code>False<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='falsy-and-truthy-values'>Falsy and Truthy values <a href=\"#falsy-and-truthy-values\" class=\"anchor\" id=\"falsy-and-truthy-values\" title=\"Anchor for Falsy and Truthy values\">#<\/a><\/h2>\n\n\n\n<p>When a value evaluates to <code>True<\/code>, it&#8217;s truthy. If a value evaluates to <code>False<\/code>, it&#8217;s falsy.<\/p>\n\n\n\n<p>The following are falsy values in Python:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The number zero (<code>0<\/code>)<\/li>\n\n\n\n<li>An empty string <code>''<\/code><\/li>\n\n\n\n<li><code>False<\/code><\/li>\n\n\n\n<li><code>None<\/code><\/li>\n\n\n\n<li>An empty <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-list\/\">list<\/a> <code>[]<\/code><\/li>\n\n\n\n<li>An empty <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-tuples\/\">tuple<\/a> <code>()<\/code><\/li>\n\n\n\n<li>An empty <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-dictionary\/\">dictionary <\/a><code>{}<\/code><\/li>\n<\/ul>\n\n\n\n<p>The truthy values are the other values that aren&#8217;t falsy.<\/p>\n\n\n\n<p class=\"note\">Note that you&#8217;ll learn more about the <code><a href=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-none\/\">None<\/a><\/code>, <code><a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-list\/\">list<\/a><\/code>, <code><a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-tuples\/\">tuple<\/a><\/code>, and <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-dictionary\/\"><code>dictionary<\/code> <\/a>in the upcoming tutorials.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Python boolean data type has two values: <code>True<\/code> and <code>False<\/code>.<\/li>\n\n\n\n<li>Use the <code>bool()<\/code> function to test if a value is <code>True<\/code> or <code>False<\/code>.<\/li>\n\n\n\n<li>Falsy values evaluate to <code>False<\/code> whereas truthy values evaluate to <code>True<\/code>.<\/li>\n\n\n\n<li>Flasy values are the number zero, an empty string, <code>False<\/code>, <code>None<\/code>, an empty list <code>[]<\/code>, an empty tuple <code>()<\/code>, and an empty dictionary <code>{}<\/code>. <\/li>\n\n\n\n<li>Truthy values are the values that are not Falsy.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=boolean\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\n\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"156\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-boolean\/\"\n\t\t\t\tdata-post-title=\"Python Boolean\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"156\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-boolean\/\"\n\t\t\t\tdata-post-title=\"Python Boolean\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you&#8217;ll learn about the Python boolean data type, falsy and truthy values. Introduction to Python Boolean data type # In programming, you often want to check if a condition is true or not and perform some actions based on the result. To represent true and false, Python provides you with the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":37,"menu_order":4,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-156","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/156","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=156"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/156\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/37"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}