{"id":3118,"date":"2026-11-09T11:40:03","date_gmt":"2026-11-09T11:40:03","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=3118"},"modified":"2026-11-09T11:40:03","modified_gmt":"2026-11-09T11:40:03","slug":"python-logical-operators","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/python-logical-operators\/","title":{"rendered":"Python Logical Operators (and, or, not) with Examples"},"content":{"rendered":"<p>Logical operators let you combine conditions. Python has three of them: <code>and<\/code>, <code>or<\/code> and <code>not<\/code>. You will use them all the time inside <code>if<\/code> statements and <code>while<\/code> loops, for example to check that a user is over 18 <em>and<\/em> has an ID, or that a number is divisible by 3 <em>or<\/em> by 5.<\/p>\n<p>Unlike C or Java, Python writes these operators as plain English words. <code>&amp;&amp;<\/code>, <code>||<\/code> and <code>!<\/code> do not work in Python.<\/p>\n<h2>Python Logical Operators at a Glance<\/h2>\n<table>\n<thead>\n<tr>\n<th>Operator<\/th>\n<th>Meaning<\/th>\n<th>Example<\/th>\n<th>Result<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>and<\/code><\/td>\n<td>True only if <strong>both<\/strong> conditions are True<\/td>\n<td><code>10 &lt; 20 and 4 &gt; 0<\/code><\/td>\n<td><code>True<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>or<\/code><\/td>\n<td>True if <strong>at least one<\/strong> condition is True<\/td>\n<td><code>4 &gt; 1 or 5 &gt; 10<\/code><\/td>\n<td><code>True<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>not<\/code><\/td>\n<td>Reverses the result<\/td>\n<td><code>not 5 &gt; 3<\/code><\/td>\n<td><code>False<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>Truth Table for and, or<\/h2>\n<p>This small program prints every combination of two Boolean values:<\/p>\n<pre class=\"wp-block-code\"><code>for a in (True, False):\n    for b in (True, False):\n        print(a, b, a and b, a or b)<\/code><\/pre>\n<p><strong>Output<\/strong> (columns: a, b, a and b, a or b):<\/p>\n<pre class=\"wp-block-code\"><code>True True True True\nTrue False False True\nFalse True False True\nFalse False False False<\/code><\/pre>\n<p>Notice the pattern: <code>and<\/code> gives True in only one row, while <code>or<\/code> gives False in only one row. <code>not<\/code> needs just one value: <code>not True<\/code> is <code>False<\/code> and <code>not False<\/code> is <code>True<\/code>.<\/p>\n<h2>Example: Checking a Number<\/h2>\n<pre class=\"wp-block-code\"><code>number = 26\nprint(number % 2 == 0 and number &lt; 100)\nprint(number % 2 == 0 or number % 5 == 0)\nprint(not (number % 2 == 0))<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>True\nTrue\nFalse<\/code><\/pre>\n<p>26 is even and less than 100, so the first line is True. For <code>or<\/code>, one True side is enough. 26 is even, so the last line is <code>not True<\/code>, which is False. You can also read the number with <code>number = int(input(\"Enter a number: \"))<\/code> and try different values.<\/p>\n<h2>Example: A Simple Login Check<\/h2>\n<pre class=\"wp-block-code\"><code>username = \"riya\"\npassword = \"abc123\"\n\nif username == \"riya\" and password == \"abc123\":\n    print(\"Login successful\")\nelse:\n    print(\"Wrong username or password\")<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>Login successful<\/code><\/pre>\n<p>Both checks must pass, so <code>and<\/code> is the right operator here. If either one is wrong, the <code>else<\/code> part runs. (Real apps never store passwords like this; the example only shows how <code>and<\/code> works.)<\/p>\n<h2>Example: Leap Year with and \/ or<\/h2>\n<p>A year is a leap year if it is divisible by 4 but not by 100, <em>or<\/em> if it is divisible by 400. That rule needs both <code>and<\/code> and <code>or<\/code>:<\/p>\n<pre class=\"wp-block-code\"><code>for year in (1900, 2000, 2024, 2026):\n    leap = (year % 4 == 0 and year % 100 != 0) or year % 400 == 0\n    print(year, leap)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>1900 False\n2000 True\n2024 True\n2026 False<\/code><\/pre>\n<h2>Short-Circuit Evaluation<\/h2>\n<p>Python stops checking as soon as it knows the answer:<\/p>\n<ul>\n<li>With <code>and<\/code>, if the left side is False, the whole thing must be False, so the right side is never run.<\/li>\n<li>With <code>or<\/code>, if the left side is True, the whole thing must be True, so the right side is skipped.<\/li>\n<\/ul>\n<pre class=\"wp-block-code\"><code>def check():\n    print(\"check() was called\")\n    return True\n\nprint(False and check())\nprint(True or check())\nprint(True and check())<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>False\nTrue\ncheck() was called\nTrue<\/code><\/pre>\n<p><code>check()<\/code> runs only in the third line. This is useful for safe checks such as <code>if count != 0 and total \/ count &gt; 50:<\/code>. If <code>count<\/code> is 0, the division never runs, so there is no ZeroDivisionError.<\/p>\n<h2>and \/ or Return Values, Not Just True or False<\/h2>\n<p>This surprises many beginners. <code>and<\/code> and <code>or<\/code> return one of the actual values they were given:<\/p>\n<ul>\n<li><code>a and b<\/code> returns <code>a<\/code> if <code>a<\/code> is falsy, otherwise <code>b<\/code>.<\/li>\n<li><code>a or b<\/code> returns <code>a<\/code> if <code>a<\/code> is truthy, otherwise <code>b<\/code>.<\/li>\n<\/ul>\n<pre class=\"wp-block-code\"><code>print(0 and 10)\nprint(7 and 10)\nprint(0 or 10)\nprint(7 or 10)\n\nname = \"\"\nprint(\"Hello,\", name or \"Guest\")\nprint(not 0, not \"hello\")<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>0\n10\n10\n7\nHello, Guest\nTrue False<\/code><\/pre>\n<p>In Python, <code>0<\/code>, <code>0.0<\/code>, an empty string <code>\"\"<\/code>, an empty list <code>[]<\/code>, an empty dictionary <code>{}<\/code> and <code>None<\/code> count as False. Almost everything else counts as True. That is why <code>name or \"Guest\"<\/code> is a common way to give a default value. <code>not<\/code> is different: it always returns <code>True<\/code> or <code>False<\/code>.<\/p>\n<h2>Precedence: not, then and, then or<\/h2>\n<p>When you mix them without brackets, Python runs <code>not<\/code> first, then <code>and<\/code>, then <code>or<\/code>. Comparisons such as <code>==<\/code> run before all three.<\/p>\n<pre class=\"wp-block-code\"><code>print(True or False and False)\nprint((True or False) and False)\nprint(not True == False)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>True\nFalse\nTrue<\/code><\/pre>\n<p>The first line is read as <code>True or (False and False)<\/code>. The last line is read as <code>not (True == False)<\/code>, because <code>==<\/code> runs before <code>not<\/code>. Brackets make your intention clear, so use them whenever you mix operators.<\/p>\n<h2>Common Mistake: x == 1 or 2<\/h2>\n<pre class=\"wp-block-code\"><code>choice = 3\nprint(choice == 1 or 2)\nprint(choice == 1 or choice == 2)\nprint(choice in (1, 2))<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>2\nFalse\nFalse<\/code><\/pre>\n<p><code>choice == 1 or 2<\/code> does not mean &#8220;choice is 1 or 2&#8221;. Python reads it as <code>(choice == 1) or 2<\/code>, which gives <code>2<\/code>, a truthy value. So an <code>if<\/code> using it would always run. Write the full comparison on both sides, or use the <a href=\"https:\/\/gyanipandit.com\/programming\/membership-operators-in-python\/\">membership operator<\/a> <code>in<\/code>.<\/p>\n<h2>Practice Questions<\/h2>\n<ol>\n<li><code>print(not (3 &gt; 2) or 5 == 5)<\/code><\/li>\n<li><code>print(4 &gt; 2 and 0)<\/code><\/li>\n<li><code>print([] or [0])<\/code><\/li>\n<\/ol>\n<p><strong>Answers:<\/strong> 1) <code>True<\/code>. The left side is False but <code>5 == 5<\/code> is True. 2) <code>0<\/code>. The left side is True, so <code>and<\/code> returns the right side, which is 0. 3) <code>[0]<\/code>. The empty list is falsy, so <code>or<\/code> returns the right side. A list containing 0 is not empty, so it is truthy.<\/p>\n<h2>Frequently Asked Questions<\/h2>\n<h3>What are the three logical operators in Python?<\/h3>\n<p><code>and<\/code>, <code>or<\/code> and <code>not<\/code>.<\/p>\n<h3>Can I use &amp;&amp; and || in Python?<\/h3>\n<p>No. They are a syntax error in Python. <code>&amp;<\/code> and <code>|<\/code> do exist, but they are <a href=\"https:\/\/gyanipandit.com\/programming\/python-bitwise-operators\/\">bitwise operators<\/a>, not logical ones.<\/p>\n<h3>What is short-circuit evaluation?<\/h3>\n<p>Python stops evaluating an <code>and<\/code>\/<code>or<\/code> expression as soon as the result is known. The right side is skipped when the left side already decides the answer.<\/p>\n<h3>Which runs first, and or or?<\/h3>\n<p><code>and<\/code> runs before <code>or<\/code>, and <code>not<\/code> runs before both.<\/p>\n<p>Next: <a href=\"https:\/\/gyanipandit.com\/programming\/python-comparison-operators\/\">Python comparison operators<\/a> \u00b7 All operators: <a href=\"https:\/\/gyanipandit.com\/programming\/python-operators\/\">Python operators overview<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Logical operators let you combine conditions. Python has three of them: and, or and not. You will use them all the time inside if statements and while loops, for example to check that a user is over 18 and has an ID, or that a number is divisible by 3 or by 5. Unlike C &#8230; <a title=\"Python Logical Operators (and, or, not) with Examples\" class=\"read-more\" href=\"https:\/\/gyanipandit.com\/programming\/python-logical-operators\/\" aria-label=\"Read more about Python Logical Operators (and, or, not) with Examples\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[26,27],"class_list":["post-3118","post","type-post","status-publish","format-standard","hentry","category-python-tutorial","tag-python","tag-python-tutorial"],"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3118","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=3118"}],"version-history":[{"count":3,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3118\/revisions"}],"predecessor-version":[{"id":6127,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3118\/revisions\/6127"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=3118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=3118"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=3118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}