{"id":24903,"date":"2023-03-25T15:15:40","date_gmt":"2023-03-25T09:45:40","guid":{"rendered":"https:\/\/tocxten.com\/?page_id=24903"},"modified":"2023-03-25T15:50:13","modified_gmt":"2023-03-25T10:20:13","slug":"boolean-values-boolean-operators-and-boolean-expressions","status":"publish","type":"page","link":"https:\/\/tocxten.com\/index.php\/boolean-values-boolean-operators-and-boolean-expressions\/","title":{"rendered":"Boolean Values, Boolean Operators and Boolean Expressions"},"content":{"rendered":"\n<p class=\"has-background has-large-font-size\" style=\"background-color:#e6eef2\"><strong>Boolean Values:<\/strong>\u00a0<\/p>\n\n\n\n<p class=\"has-medium-font-size\">A Boolean value is either true or false. It is named after the British mathematician, George Boole, who first formulated Boolean algebra. In Python the two Boolean Values are True and False and the Python type is bool. Enter the following into the Python shell and observe the output.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-69.png\" alt=\"\" class=\"wp-image-24904\" width=\"661\" height=\"484\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-69.png 768w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-69-300x220.png 300w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-69-760x556.png 760w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-69-600x439.png 600w\" sizes=\"auto, (max-width: 661px) 100vw, 661px\" \/><\/figure>\n\n\n\n<p class=\"has-medium-font-size\">In Python, boolean values are used to represent logical values of <code>True<\/code> or <code>False<\/code>. These values are often used in conditional statements, loops, and other control structures to control the flow of the program. Here are some examples of using boolean values in Python:<\/p>\n\n\n\n<ol class=\"has-medium-font-size wp-block-list\"><li><strong>Boolean values in conditional statements:<\/strong><\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>age = 18\r\n\rif age >= 18:\r\n    print(\"You are an adult\")\r\nelse:\r\n    print(\"You are not an adult\")<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In the example above, the <code>age<\/code> variable is checked if it is greater than or equal to 18, and if it is, the program prints &#8220;You are an adult&#8221;, otherwise, it prints &#8220;You are not an adult&#8221;.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">2. <strong>Boolean values in loops:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fruits = &#91;\"apple\", \"banana\", \"cherry\", \"orange\"]\r\n\r\nfor fruit in fruits:\r\n    if fruit == \"banana\":\r\n        print(\"I found a banana!\")\r\n        break\r\n    else:\r\n        print(\"No banana found :(\")<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In the example above, the program loops through a list of fruits and checks if each fruit is a banana. If it finds a banana, it prints &#8220;I found a banana!&#8221; and exits the loop using the <code>break<\/code> statement.<\/p>\n\n\n\n<ol start=\"3\" class=\"has-medium-font-size wp-block-list\"><li><strong>Boolean values in functions:<\/strong><\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>def is_even(number):\r\n    return number % 2 == 0\r\n\r\nprint(is_even(4)) # Output: True\r\nprint(is_even(3)) # Output: False<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In the example above, the <code>is_even()<\/code> function checks if a given number is even or not by checking if the remainder of the number divided by 2 is equal to 0. It returns <code>True<\/code> if the number is even, and <code>False<\/code> otherwise. Boolean values are a fundamental part of Python programming and are used in a wide range of applications.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-background\" style=\"background-color:#dee7eb;font-size:30px\"><strong>Boolean Expressions <\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\">A Boolean expression is an expression that evaluated to produce a result which is a Boolean value. For example, the operator == tests if two values are equal. It produces (or yields) a Boolean value:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-70.png\" alt=\"\" class=\"wp-image-24910\" width=\"360\" height=\"257\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-70.png 530w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-70-300x215.png 300w\" sizes=\"auto, (max-width: 360px) 100vw, 360px\" \/><\/figure>\n\n\n\n<p class=\"has-medium-font-size\">In the first statement the two operands evaluate to equal values, so the expression evaluates to\u00a0<strong>True<\/strong>; in the second statement, 5 is not equal to 6 we get\u00a0<strong>False.<\/strong><\/p>\n\n\n\n<p class=\"has-background\" style=\"background-color:#dbe6eb;font-size:30px\"><strong>Boolean Operators <\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\">In Python, boolean operators and expressions are used to combine boolean values and perform logical operations on them. There are three boolean operators in Python: <code>and<\/code>, <code>or<\/code>, and <code>not<\/code>. Here are some examples of using boolean operators and expressions in Python:<\/p>\n\n\n\n<ol class=\"has-medium-font-size wp-block-list\"><li><strong><code>and<\/code> operator:<\/strong>   The <code>and<\/code> operator returns <code>True<\/code> if both operands are <code>True<\/code>, otherwise, it returns <code>False<\/code>.<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>x = 5\r\ny = 10\r\n\rif x > 0 and y > 0:\r\n    print(\"Both x and y are positive\")\r\nelse:\r\n    print(\"At least one of x and y is not positive\")<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In the example above, the <code>and<\/code> operator is used to check if both <code>x<\/code> and <code>y<\/code> are positive. Since both variables are greater than 0, the program prints &#8220;Both x and y are positive&#8221;.<\/p>\n\n\n\n<ol start=\"2\" class=\"has-medium-font-size wp-block-list\"><li><strong><code>or<\/code> operator:<\/strong><\/li><\/ol>\n\n\n\n<p class=\"has-medium-font-size\">The <code>or<\/code> operator returns <code>True<\/code> if at least one of the operands is <code>True<\/code>, otherwise, it returns <code>False<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x = 5\r\ny = -10\r\n\r\nif x > 0 or y > 0:\r\n    print(\"At least one of x and y is positive\")\r\nelse:\r\n    print(\"Neither x nor y is positive\")<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In the example above, the <code>or<\/code> operator is used to check if at least one of <code>x<\/code> and <code>y<\/code> is positive. Since <code>x<\/code> is positive, the program prints &#8220;At least one of x and y is positive&#8221;.<\/p>\n\n\n\n<ol start=\"3\" class=\"has-medium-font-size wp-block-list\"><li><strong><code>not<\/code> operator:<\/strong><\/li><\/ol>\n\n\n\n<p class=\"has-medium-font-size\">The <code>not<\/code> operator returns the opposite of a boolean value. If the value is <code>True<\/code>, it returns <code>False<\/code>, and vice versa.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x = 5\r\n\rif not x > 0:\r\n    print(\"x is not positive\")\r\nelse:\r\n    print(\"x is positive\")<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In the example above, the <code>not<\/code> operator is used to check if <code>x<\/code> is not positive. Since <code>x<\/code> is positive, the program prints &#8220;x is positive&#8221;.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Boolean expressions can also be used to create more complex boolean logic by combining multiple boolean values and operators. Here&#8217;s an example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x = 5\r\ny = 10\r\n\r\nif (x > 0 and y > 0) or (x &lt; 0 and y &lt; 0):\r\n    print(\"Both x and y have the same sign\")\r\nelse:\r\n    print(\"x and y have opposite signs\")<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In the example above, the program checks if <code>x<\/code> and <code>y<\/code> have the same sign by using the <code>and<\/code> and <code>or<\/code> operators to combine boolean expressions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Boolean Values:\u00a0 A Boolean value is either true or false. It is named after the British mathematician, George Boole, who first formulated Boolean algebra. In Python the two Boolean Values&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":"","_links_to":"","_links_to_target":""},"class_list":["post-24903","page","type-page","status-publish","hentry"],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/tocxten.com\/index.php\/wp-json\/wp\/v2\/pages\/24903","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tocxten.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/tocxten.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/tocxten.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tocxten.com\/index.php\/wp-json\/wp\/v2\/comments?post=24903"}],"version-history":[{"count":4,"href":"https:\/\/tocxten.com\/index.php\/wp-json\/wp\/v2\/pages\/24903\/revisions"}],"predecessor-version":[{"id":24912,"href":"https:\/\/tocxten.com\/index.php\/wp-json\/wp\/v2\/pages\/24903\/revisions\/24912"}],"wp:attachment":[{"href":"https:\/\/tocxten.com\/index.php\/wp-json\/wp\/v2\/media?parent=24903"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}