{"id":31,"date":"2024-01-24T13:20:40","date_gmt":"2024-01-24T13:20:40","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=31"},"modified":"2024-01-24T13:20:42","modified_gmt":"2024-01-24T13:20:42","slug":"python-logical-operators","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/24\/python-logical-operators\/","title":{"rendered":"Python Logical Operators"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn about Python logical operators and how to use them to combine multiple conditions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to Python logical operators<\/h2>\n\n\n\n<p>Sometimes, you may want to check multiple conditions at the same time. To do so, you use logical operators.<\/p>\n\n\n\n<p>Python has three logical operators:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>and<\/code><\/li>\n\n\n\n<li><code>or<\/code><\/li>\n\n\n\n<li><code>not<\/code><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">The&nbsp;<code>and<\/code>&nbsp;operator<\/h3>\n\n\n\n<p>The\u00a0<code>and<\/code>\u00a0operator checks whether two conditions are both\u00a0<code>True<\/code>\u00a0simultaneously:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>a and b<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>It returns&nbsp;<code>True<\/code>&nbsp;if both conditions are&nbsp;<code>True<\/code>. And it returns&nbsp;<code>False<\/code>&nbsp;if either the condition&nbsp;<code>a<\/code>&nbsp;or&nbsp;<code>b<\/code>&nbsp;is&nbsp;<code>False<\/code>.<\/p>\n\n\n\n<p>The following example uses the\u00a0<code>and<\/code>\u00a0operator to combine two conditions that compare the\u00a0<code>price<\/code>\u00a0with numbers:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>>>> price = 9.99 >>> price > 9 and price &lt; 10 True<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The result is&nbsp;<code>True<\/code>&nbsp;because the&nbsp;<code>price<\/code>&nbsp;is greater than 9 and less than 10.<\/p>\n\n\n\n<p>The following example returns\u00a0<code>False<\/code>\u00a0because the\u00a0<code>price<\/code>\u00a0isn\u2019t greater than 10:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>>>> price > 10 and price &lt; 20 False<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>In this example, the condition&nbsp;<code>price &gt; 10<\/code>&nbsp;returns&nbsp;<code>False<\/code>&nbsp;while the second condition&nbsp;<code>price &lt; 20<\/code>&nbsp;returns&nbsp;<code>True<\/code>.<\/p>\n\n\n\n<p>The following table illustrates the result of the&nbsp;<code>and<\/code>&nbsp;operator when combining two conditions:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>a<\/td><td>b<\/td><td>a and b<\/td><\/tr><tr><td>True<\/td><td>True<\/td><td>True<\/td><\/tr><tr><td>True<\/td><td>False<\/td><td>False<\/td><\/tr><tr><td>False<\/td><td>False<\/td><td>False<\/td><\/tr><tr><td>False<\/td><td>True<\/td><td>False<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>As you can see from the table, the condition&nbsp;<code>a<\/code>&nbsp;and&nbsp;<code>b<\/code>&nbsp;only returns&nbsp;<code>True<\/code>&nbsp;if both conditions evaluate to&nbsp;<code>True<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The or operator<\/h2>\n\n\n\n<p>Similar to the\u00a0<code>and<\/code>\u00a0operator, the\u00a0<code>or<\/code>\u00a0operator checks multiple conditions. But it returns\u00a0<code>True<\/code>\u00a0when either or both individual conditions are\u00a0<code>True<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>a or b<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The following table illustrates the result of the&nbsp;<code>or<\/code>&nbsp;operator when combining two conditions:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>a<\/td><td>b<\/td><td>a or b<\/td><\/tr><tr><td>True<\/td><td>True<\/td><td>True<\/td><\/tr><tr><td>True<\/td><td>False<\/td><td>True<\/td><\/tr><tr><td>False<\/td><td>True<\/td><td>True<\/td><\/tr><tr><td>False<\/td><td>False<\/td><td>False<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The&nbsp;<code>or<\/code>&nbsp;operator returns&nbsp;<code>False<\/code>&nbsp;only when both conditions are&nbsp;<code>False<\/code>.<\/p>\n\n\n\n<p>The following example shows how to use the\u00a0<code>or<\/code>\u00a0operator:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>>>> price = 9.99 >>> price > 10 or price &lt; 20 >>> True<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>In this example, the&nbsp;<code>price &lt; 20<\/code>&nbsp;returns&nbsp;<code>True<\/code>, therefore, the whole expression returns&nbsp;<code>True<\/code>.<\/p>\n\n\n\n<p>The following example returns\u00a0<code>False<\/code>\u00a0because both conditions evaluate to\u00a0<code>False<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>>>> price = 9.99 >>> price > 10 or price &lt; 5 False<\/code><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">The not operator<\/h2>\n\n\n\n<p>The\u00a0<code>not<\/code>\u00a0operator applies to one condition. And it reverses the result of that condition,\u00a0<code>True<\/code>\u00a0becomes\u00a0<code>False<\/code>\u00a0and\u00a0<code>False<\/code>\u00a0becomes\u00a0<code>True<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>not a<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>If the condition is&nbsp;<code>True<\/code>, the&nbsp;<code>not<\/code>&nbsp;operator returns&nbsp;<code>False<\/code>&nbsp;and vice versa.<\/p>\n\n\n\n<p>The following table illustrates the result of the&nbsp;<code>not<\/code>&nbsp;operator:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>a<\/td><td>not a<\/td><\/tr><tr><td>True<\/td><td>False<\/td><\/tr><tr><td>False<\/td><td>True<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The following example uses the\u00a0<code>not<\/code>\u00a0operator. Since the\u00a0<code>price > 10<\/code>\u00a0returns\u00a0<code>False<\/code>, the\u00a0<code>not price > 10<\/code>\u00a0returns\u00a0<code>True<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>>>> price = 9.99 >>> not price > 10 True<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Here is another example that combines the\u00a0<code>not<\/code>\u00a0and the\u00a0<code>and<\/code>\u00a0operators:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>>>> not (price > 5 and price &lt; 10) False<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>In this example, Python evaluates the conditions based on the following order:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First,&nbsp;<code>(price &gt; 5 and price &lt; 10)<\/code>&nbsp;evaluates to&nbsp;<code>True<\/code>.<\/li>\n\n\n\n<li>Second,&nbsp;<code>not True<\/code>&nbsp;evaluates to&nbsp;<code>False<\/code>.<\/li>\n<\/ul>\n\n\n\n<p>This leads to an important concept called precedence of logical operators.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Precedence of Logical Operators<\/h3>\n\n\n\n<p>When you mix the logical operators in an expression, Python will evaluate them in the order which is called the operator precedence.<\/p>\n\n\n\n<p>The following shows the precedence of the&nbsp;<code>not<\/code>,&nbsp;<code>and<\/code>, and&nbsp;<code>or<\/code>&nbsp;operators:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th>Operator<\/th><th>Precedence<\/th><\/tr><tr><td>not<\/td><td>High<\/td><\/tr><tr><td>and<\/td><td>Medium<\/td><\/tr><tr><td>or<\/td><td>Low<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Based on these precedences, Python will group the operands for the operator with the highest precedence first, then group the operands for the operator with the lower precedence, and so on.<\/p>\n\n\n\n<p>In case an expression has several logical operators with the same precedence, Python will evaluate them from the left to right:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><code>a or b and c<\/code><\/td><td>means<\/td><td><code>a or (b and c)<\/code><\/td><\/tr><tr><td><code>a and b or c and d<\/code><\/td><td>means<\/td><td><code>(a and b) or (c and d)<\/code><\/td><\/tr><tr><td><code>a and b and c or d<\/code><\/td><td>means<\/td><td><code>((a and b) and c) or d<\/code><\/td><\/tr><tr><td><code>not a and b or c<\/code><\/td><td>means<\/td><td><code>((not a) and b) or c<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn about Python logical operators and how to use them to combine multiple conditions. Introduction to Python logical operators Sometimes, you may want to check multiple conditions at the same time. To do so, you use logical operators. Python has three logical operators: The&nbsp;and&nbsp;operator The\u00a0and\u00a0operator checks whether two conditions are [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-31","post","type-post","status-publish","format-standard","hentry","category-2-operators"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/31","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=31"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/31\/revisions"}],"predecessor-version":[{"id":32,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/31\/revisions\/32"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=31"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=31"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=31"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}