{"id":75110,"date":"2020-01-11T10:43:33","date_gmt":"2020-01-11T05:13:33","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=75110"},"modified":"2020-01-11T10:43:33","modified_gmt":"2020-01-11T05:13:33","slug":"python-booleans","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/python-booleans\/","title":{"rendered":"Python Booleans &#8211; A data type to find two possible outcomes"},"content":{"rendered":"<p>In the world of computer science, Boolean is a <strong>data type<\/strong> that can only have two <strong>possible values<\/strong> either <strong>True<\/strong> or <strong>False<\/strong>.<\/p>\n<p>In this article, we are going to look at the Python Booleans, we will understand how to<strong> declare a boolean<\/strong> and the <strong>bool() function<\/strong>. We will also learn the operations you can perform on booleans.<\/p>\n<h3>What are Python Booleans?<\/h3>\n<p>The Python Booleans is a basic <strong>data structure<\/strong> which holds either <strong>False<\/strong> or <strong>True<\/strong> <strong>values<\/strong>.<\/p>\n<p>A lot of functions only require a <strong>boolean<\/strong> value to <strong>return<\/strong>, for example, we used the <strong>isdisjoint()<\/strong>,<strong> issuperset()<\/strong> and <strong>issubset()<\/strong> functions. The result is only supposed to be <strong>True<\/strong> or <strong>False<\/strong>.<\/p>\n<h3>Declaring a Boolean in Python<\/h3>\n<p>Booleans can be declared just like an <strong>integer<\/strong>. We assign either True or False to an <strong>identifier<\/strong> and we <strong>don\u2019t use<\/strong> <strong>quotes<\/strong> like in a <strong>string<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">Start = False\nRun = True\nprint( type(Start) )<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">&lt;class \u2018bool\u2019&gt;<\/div>\n<h3>Python bool() function<\/h3>\n<p>The <strong>bool()<\/strong> function is used to <strong>convert<\/strong> a <strong>value<\/strong> into a <strong>boolean value<\/strong> ( True or False). We can convert <strong>integer<\/strong>, <strong>strings<\/strong>, <strong>list<\/strong>, <strong>map<\/strong> into <strong>boolean<\/strong> <strong>values<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">print( bool(0) )\nprint( bool(10) )\nprint( bool(\u2018\u2019))\nprint( bool(\u2018hello\u2019))\nprint( bool({1,2,3}))\nprint( bool(()))<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">False<br \/>\nTrue<br \/>\nFalse<br \/>\nTrue<br \/>\nTrue<br \/>\nFalse<\/div>\n<h3>Operations on Python Booleans<\/h3>\n<p><strong>False<\/strong> is equivalent to <strong>zero<\/strong> and <strong>True<\/strong> is equivalent to <strong>1<\/strong> so we can perform various operations on <strong>booleans<\/strong>.<\/p>\n<h4>1. Arithmetic Operators in Python<\/h4>\n<p><strong>1.1 Addition<\/strong><\/p>\n<p>Let\u2019s <strong>add<\/strong> two boolean values.<\/p>\n<table class=\"tv-table-center\">\n<tbody>\n<tr>\n<td><b>True + True<\/b><\/td>\n<td><b>2<\/b><\/td>\n<\/tr>\n<tr>\n<td><b>False + True<\/b><\/td>\n<td><b>1<\/b><\/td>\n<\/tr>\n<tr>\n<td><b>True + False<\/b><\/td>\n<td><b>1<\/b><\/td>\n<\/tr>\n<tr>\n<td><b>False + False<\/b><\/td>\n<td><b>0<\/b><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>1.2. Subtraction<\/strong><\/p>\n<table class=\"tv-table-center\">\n<tbody>\n<tr>\n<td><b>True &#8211; True<\/b><\/td>\n<td><b>0<\/b><\/td>\n<\/tr>\n<tr>\n<td><b>False &#8211; True<\/b><\/td>\n<td><b>-1<\/b><\/td>\n<\/tr>\n<tr>\n<td><b>True &#8211; False<\/b><\/td>\n<td><b>1<\/b><\/td>\n<\/tr>\n<tr>\n<td><b>False &#8211; False<\/b><\/td>\n<td><b>0<\/b><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>1.3. Multiplication<\/strong><\/p>\n<table class=\"tv-table-center\">\n<tbody>\n<tr>\n<td><b>True * True<\/b><\/td>\n<td><b>1<\/b><\/td>\n<\/tr>\n<tr>\n<td><b>False * True<\/b><\/td>\n<td><b>0<\/b><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<table class=\"tv-table-center\">\n<tbody>\n<tr>\n<td><b>True * False<\/b><\/td>\n<td><b>0<\/b><\/td>\n<\/tr>\n<tr>\n<td><b>False * False<\/b><\/td>\n<td><b>0<\/b><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>1.4. Division<\/strong><\/p>\n<p>As we know, <strong>division<\/strong> by <strong>zero <\/strong>gives us an <strong>error <\/strong>so dividing anything by <strong>False<\/strong> will also give us an <strong>exception<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">print(True\/True)\nprint( True\/False)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">1.0<br \/>\nTraceback (most recent call last):<br \/>\n<b>\u00a0\u00a0<\/b>File &#8220;&lt;stdin&gt;&#8221;, line 1, in &lt;module&gt;<br \/>\nZeroDivisionError: division by zero<\/div>\n<p><strong>1.5. Modulus, exponentiation and Floor division<\/strong><\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">print( False%True)\nprint( True**False)\nprint( True\/\/True)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">0<br \/>\n1<br \/>\n1<\/div>\n<h4>2. Relational Operators in Python<\/h4>\n<p>All the <strong>relational operators (&lt;,&gt;, &lt;=, &gt;=, !=, ==)<\/strong> <strong>return boolean<\/strong> values after <strong>comparing<\/strong>.<\/p>\n<p>Try playing with relational operators on your own and see what output you will get.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">print( True&gt;=True or False&gt;True )\nprint( False&lt;True and False!=True and False==True)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">True<br \/>\nFalse<\/div>\n<h4>3. Bitwise Operators in Python<\/h4>\n<p>Bitwise operations are performed <strong>bit by bit<\/strong> on <strong>binary values<\/strong>. In the case of boolean values, they are <strong>treated<\/strong> as a <strong>single bit 1<\/strong> as <strong>True<\/strong> and <strong>0<\/strong> as<strong> False<\/strong>.<\/p>\n<p>Bitwise operators include <strong><em>&amp;, |, ^, ~, &lt;&lt;, &gt;&gt;<\/em><\/strong>.<\/p>\n<p>Let\u2019s see some <strong>bitwise operations<\/strong> performed on booleans:<\/p>\n<table class=\"tv-table-center\">\n<tbody>\n<tr>\n<td><span style=\"font-weight: 400\">True &amp; False<\/span><\/td>\n<td><span style=\"font-weight: 400\">False<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">True | False<\/span><\/td>\n<td><span style=\"font-weight: 400\">True<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">True ^ False<\/span><\/td>\n<td><span style=\"font-weight: 400\">True<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">~True<\/span><\/td>\n<td><span style=\"font-weight: 400\">-2<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">~False<\/span><\/td>\n<td><span style=\"font-weight: 400\">-1<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">True&lt;&lt;True<\/span><\/td>\n<td><span style=\"font-weight: 400\">2<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>4. Logical Operators in Python<\/h4>\n<p>Logical operators<strong> (\u2018and\u2019 and \u2018or\u2019)<\/strong> also work the same on boolean values.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">print(False or True)\nprint(False and False)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">True<br \/>\nFalse<\/div>\n<h4>5. Identity Operators in Python<\/h4>\n<p>At last, we will apply the boolean operations on <strong>identity operators \u2018is\u2019<\/strong> and<strong> \u2018is not\u2019<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">True is True\nFalse is 0<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">True<br \/>\nFalse<\/div>\n<h3>Summary<\/h3>\n<p>In this article, we talked about the concept of Python booleans. We saw how you can <strong>create<\/strong> the <strong>boolean data type<\/strong>, the bool() <strong>function<\/strong> and all the <strong>operations<\/strong> you can perform with them.<\/p>\n<p>So this was all about TechVidvan&#8217;s Python booleans article.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the world of computer science, Boolean is a data type that can only have two possible values either True or False. In this article, we are going to look at the Python Booleans,&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":75389,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1053],"tags":[1285,1286,1287,1288,1289,1290,1291,1292],"class_list":["post-75110","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-arithmetic-operators-in-python","tag-bitwise-operators-in-python","tag-booleans-in-python","tag-declaring-a-boolean-in-python","tag-operations-on-python-booleans","tag-python-bool-function","tag-python-booleans","tag-python-relational-operators"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Booleans - A data type to find two possible outcomes - TechVidvan<\/title>\n<meta name=\"description\" content=\"Python Booleans - Learn the concept of booleans which is a basic data structure in Python. Also, learn about Python bool() function and its operations.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/python-booleans\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Booleans - A data type to find two possible outcomes - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Python Booleans - Learn the concept of booleans which is a basic data structure in Python. Also, learn about Python bool() function and its operations.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/python-booleans\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-01-11T05:13:33+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/python-booleans.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Booleans - A data type to find two possible outcomes - TechVidvan","description":"Python Booleans - Learn the concept of booleans which is a basic data structure in Python. Also, learn about Python bool() function and its operations.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/python-booleans\/","og_locale":"en_US","og_type":"article","og_title":"Python Booleans - A data type to find two possible outcomes - TechVidvan","og_description":"Python Booleans - Learn the concept of booleans which is a basic data structure in Python. Also, learn about Python bool() function and its operations.","og_url":"https:\/\/techvidvan.com\/tutorials\/python-booleans\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2020-01-11T05:13:33+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/python-booleans.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/python-booleans\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-booleans\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Python Booleans &#8211; A data type to find two possible outcomes","datePublished":"2020-01-11T05:13:33+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-booleans\/"},"wordCount":497,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-booleans\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/python-booleans.jpg","keywords":["arithmetic operators in python","bitwise operators in python","Booleans in Python","Declaring a Boolean in Python","Operations on Python Booleans","Python bool() function","Python Booleans","python relational operators"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/python-booleans\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/python-booleans\/","url":"https:\/\/techvidvan.com\/tutorials\/python-booleans\/","name":"Python Booleans - A data type to find two possible outcomes - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-booleans\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-booleans\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/python-booleans.jpg","datePublished":"2020-01-11T05:13:33+00:00","description":"Python Booleans - Learn the concept of booleans which is a basic data structure in Python. Also, learn about Python bool() function and its operations.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-booleans\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/python-booleans\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/python-booleans\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/python-booleans.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/python-booleans.jpg","width":802,"height":420,"caption":"booleans in python"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/python-booleans\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Python Booleans &#8211; A data type to find two possible outcomes"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/75110","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=75110"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/75110\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/75389"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=75110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=75110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=75110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}