{"id":16216,"date":"2022-03-23T16:42:34","date_gmt":"2022-03-23T16:42:34","guid":{"rendered":"https:\/\/linuxways.net\/?p=16216"},"modified":"2022-03-23T16:42:34","modified_gmt":"2022-03-23T16:42:34","slug":"python-logical-operators","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/scripting\/python-logical-operators\/","title":{"rendered":"Python Logical Operators"},"content":{"rendered":"<p>Logical operators can be used to perform different logical operations, which can be in the form of true or false. These operators are divided into three categories: logical AND, logical OR, and logical NOT, and are represented by different symbols and keywords in the Python programming language. The operand refers to the value on which the operator conducts the computation. In this post, we&#8217;ll go through the logical operator&#8217;s execution and discuss how you can implement them in Python.<\/p>\n<h2><strong>Logical Operators in Python with Examples<\/strong><\/h2>\n<p>In this section, we are going to discuss the logical operators in detail with examples for a better understanding.<\/p>\n<h3><strong>Logical AND operator <\/strong><\/h3>\n<p>The first one is the logical AND operator, which we are going to apply between two different operands. If the condition for the operands is true, then their overall result will also be true. Otherwise, it will be considered false if any of the two conditions is false. For better understanding, we have explained the logical and operator in the form of the truth table.<\/p>\n<table>\n<tbody>\n<tr>\n<td>\n<pre>x<\/pre>\n<\/td>\n<td>\n<pre>y<\/pre>\n<\/td>\n<td>\n<pre>x and y<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<pre>True<\/pre>\n<\/td>\n<td>\n<pre>True<\/pre>\n<\/td>\n<td>\n<pre>True<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<pre>True<\/pre>\n<\/td>\n<td>\n<pre>False<\/pre>\n<\/td>\n<td>\n<pre>False<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<pre>False<\/pre>\n<\/td>\n<td>\n<pre>False<\/pre>\n<\/td>\n<td>\n<pre>False<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<pre>False<\/pre>\n<\/td>\n<td>\n<pre>True<\/pre>\n<\/td>\n<td>\n<pre>False<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>As shown in the table, the conditions x and y only return true if both conditions evaluate to be true.<\/p>\n<p><strong>Note:<\/strong> When using an operator, if the first expression is evaluated as false, and the subsequent expressions are not evaluated.<\/p>\n<p>The implementation of the basic example in Python is shown below.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<table>\n<tbody>\n<tr>\n<td>\n<pre><strong>x = 10<\/strong>\r\n\r\n<strong>y = 20<\/strong>\r\n\r\n<strong>x &gt; 0 and y &gt; 0<\/strong>\r\n\r\n<strong>Output:\u00a0<\/strong>\r\n\r\n<strong>True<\/strong><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>In the above example, you can see that we have used two different variables with the names of &#8220;x&#8221; and &#8220;y&#8221; and the assigned values for both these variables are greater than 0. That&#8217;s why their overall result is also true.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"859\" height=\"183\" class=\"wp-image-16217\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/03\/shape-description-automatically-generated-with-me-6.png\" alt=\"Shape Description automatically generated with medium confidence\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/03\/shape-description-automatically-generated-with-me-6.png 859w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/03\/shape-description-automatically-generated-with-me-6-300x64.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/03\/shape-description-automatically-generated-with-me-6-768x164.png 768w\" sizes=\"auto, (max-width: 859px) 100vw, 859px\" \/><\/p>\n<p><strong>Example 2:<\/strong><\/p>\n<table>\n<tbody>\n<tr>\n<td>\n<pre><strong>x = 10<\/strong>\r\n\r\n<strong>y = 20<\/strong>\r\n\r\n<strong>x &gt; 0 and y &lt; 0<\/strong>\r\n\r\n<strong>Output:\u00a0<\/strong>\r\n\r\n<strong>False<\/strong><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"918\" height=\"163\" class=\"wp-image-16218\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/03\/shape-description-automatically-generated-with-me-7.png\" alt=\"Shape Description automatically generated with medium confidence\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/03\/shape-description-automatically-generated-with-me-7.png 918w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/03\/shape-description-automatically-generated-with-me-7-300x53.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/03\/shape-description-automatically-generated-with-me-7-768x136.png 768w\" sizes=\"auto, (max-width: 918px) 100vw, 918px\" \/><\/p>\n<p>Now in this example, you can see that we have given the same values again to variables x and y, but this time we are testing if the value of the variable y is less than 0. As this condition is false, their overall result will also be false, as can be seen in the image shown above.<\/p>\n<h3><strong>Logical OR operator <\/strong><\/h3>\n<p>The logical OR operator will also be used to compare the values of two different variables just like the AND operator, but the difference is that the overall result will be true if any or both of the conditions are true. To give you a better picture, let&#8217;s discuss this operator in more detail in the truth table as shown below.<\/p>\n<table>\n<tbody>\n<tr>\n<td>\n<pre>x<\/pre>\n<\/td>\n<td>\n<pre>y<\/pre>\n<\/td>\n<td>\n<pre>x or y<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<pre>True<\/pre>\n<\/td>\n<td>\n<pre>True<\/pre>\n<\/td>\n<td>\n<pre>True<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<pre>True<\/pre>\n<\/td>\n<td>\n<pre>False<\/pre>\n<\/td>\n<td>\n<pre>True<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<pre>False<\/pre>\n<\/td>\n<td>\n<pre>True<\/pre>\n<\/td>\n<td>\n<pre>True<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<pre>False<\/pre>\n<\/td>\n<td>\n<pre>False<\/pre>\n<\/td>\n<td>\n<pre>False<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The\u00a0OR operator returns false only when both conditions are false.<\/p>\n<p><strong>Note:<\/strong> When employing the OR operator, if the initial expression evaluates to true, then the subsequent expressions are not evaluated.<\/p>\n<p>Now let\u2019s discuss the first example:<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<table>\n<tbody>\n<tr>\n<td>\n<pre><strong>x = 10<\/strong>\r\n\r\n<strong>y = 20<\/strong>\r\n\r\n<strong>x &gt; 0 or y &gt; 0<\/strong>\r\n\r\n<strong>Output:\u00a0<\/strong>\r\n\r\n<strong>True<\/strong><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"859\" height=\"160\" class=\"wp-image-16219\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/03\/shape-description-automatically-generated-with-me-8.png\" alt=\"Shape Description automatically generated with medium confidence\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/03\/shape-description-automatically-generated-with-me-8.png 859w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/03\/shape-description-automatically-generated-with-me-8-300x56.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/03\/shape-description-automatically-generated-with-me-8-768x143.png 768w\" sizes=\"auto, (max-width: 859px) 100vw, 859px\" \/><\/p>\n<p>As you can see in the above image, both the variables x and y have a value greater than 0. That&#8217;s why their overall result will also be true.<\/p>\n<p><strong>Example 2:<\/strong><\/p>\n<table>\n<tbody>\n<tr>\n<td>\n<pre><strong>x = 10<\/strong>\r\n\r\n<strong>y = 20<\/strong>\r\n\r\n<strong>x &lt; 0 or y &lt; 0<\/strong>\r\n\r\n<strong>Output:\u00a0<\/strong>\r\n\r\n<strong>False<\/strong><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"888\" height=\"165\" class=\"wp-image-16220\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/03\/shape-description-automatically-generated-with-me-9.png\" alt=\"Shape Description automatically generated with medium confidence\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/03\/shape-description-automatically-generated-with-me-9.png 888w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/03\/shape-description-automatically-generated-with-me-9-300x56.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/03\/shape-description-automatically-generated-with-me-9-768x143.png 768w\" sizes=\"auto, (max-width: 888px) 100vw, 888px\" \/><\/p>\n<p>In this example, you can see that the values for the variable are greater than 0, but we are testing for values that are less than 0. That&#8217;s why their overall result is also false. Till now, it behaves just like the logical AND operator, so the clear difference can be seen in the next example mentioned below.<\/p>\n<p><strong>Example 3:<\/strong><\/p>\n<table>\n<tbody>\n<tr>\n<td>\n<pre><strong>x = 10<\/strong>\r\n\r\n<strong>y = 20<\/strong>\r\n\r\n<strong>x &gt; 0 or y &lt; 0<\/strong>\r\n\r\n<strong>Output:\u00a0<\/strong>\r\n\r\n<strong>False<\/strong><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"538\" height=\"108\" class=\"wp-image-16221\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/03\/shape-description-automatically-generated-with-me-10.png\" alt=\"Shape Description automatically generated with medium confidence\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/03\/shape-description-automatically-generated-with-me-10.png 538w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/03\/shape-description-automatically-generated-with-me-10-300x60.png 300w\" sizes=\"auto, (max-width: 538px) 100vw, 538px\" \/><\/p>\n<p>Now, we are testing if the value of variable x is greater than 0, which is true, and if the value of variable y is less than 0. As you can see, one of the two conditions is true, which is why their overall result will also be true.<\/p>\n<h3><strong>Logical NOT operator <\/strong><\/h3>\n<p>The logical not operator will take a single value as an input and will show you the opposite result of whatever was previously stored in it. For example, if the condition is true, then it will show you that the condition is false. This can also be seen in the below-mentioned truth table.<\/p>\n<table>\n<tbody>\n<tr>\n<td>\n<pre>a<\/pre>\n<\/td>\n<td>\n<pre>not a<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<pre>True<\/pre>\n<\/td>\n<td>\n<pre>False<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<pre>False<\/pre>\n<\/td>\n<td>\n<pre>True<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Let&#8217;s discuss some examples for better understanding.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<table>\n<tbody>\n<tr>\n<td>\n<pre><strong>x = 10<\/strong>\r\n\r\n<strong>y = 20<\/strong>\r\n\r\n<strong>&gt;&gt;&gt; x &lt; y<\/strong>\r\n\r\n<strong>True<\/strong>\r\n\r\n<strong>&gt;&gt;&gt; not x &gt; y<\/strong>\r\n\r\n<strong>False<\/strong><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"979\" height=\"229\" class=\"wp-image-16222\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/03\/shape-description-automatically-generated-with-me-11.png\" alt=\"Shape Description automatically generated with medium confidence\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/03\/shape-description-automatically-generated-with-me-11.png 979w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/03\/shape-description-automatically-generated-with-me-11-300x70.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/03\/shape-description-automatically-generated-with-me-11-768x180.png 768w\" sizes=\"auto, (max-width: 979px) 100vw, 979px\" \/><\/p>\n<p>In the above example, you can see that the value of x is less than y so the condition is true, but after applying the logical and operator, it reverses the result, showing you the output as false.<\/p>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p>Logical operators are used to calculate the results based on logical operations. It compares the values of the two operands and will provide you with the result in the form of true or false. There are three different logical operators that we have discussed in this article, which are logical and operator, logical or operator, and logical not operator.<\/p>","protected":false},"excerpt":{"rendered":"<p>Logical operators can be used to perform different logical operations, which can be in the form of true or false. These operators are divided into three categories: logical&hellip;<\/p>","protected":false},"author":1,"featured_media":16252,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[168],"tags":[940,10],"class_list":["post-16216","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-operators","tag-python"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/16216","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=16216"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/16216\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/16252"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=16216"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=16216"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=16216"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}