{"id":3121,"date":"2026-11-10T06:10:03","date_gmt":"2026-11-10T06:10:03","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=3121"},"modified":"2026-11-10T06:10:03","modified_gmt":"2026-11-10T06:10:03","slug":"python-comparison-operators","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/python-comparison-operators\/","title":{"rendered":"Python Comparison Operators with Examples"},"content":{"rendered":"<p>Comparison operators compare two values and always give back a Boolean: <code>True<\/code> or <code>False<\/code>. They are the backbone of every <code>if<\/code> statement and loop condition you will write, for example &#8220;is the marks value at least 33?&#8221; or &#8220;has the user typed the right PIN?&#8221;<\/p>\n<p>Python has six comparison operators (also called relational operators).<\/p>\n<h2>The Six Comparison Operators<\/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>==<\/code><\/td>\n<td>Equal to<\/td>\n<td><code>5 == 5<\/code><\/td>\n<td><code>True<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>!=<\/code><\/td>\n<td>Not equal to<\/td>\n<td><code>5 != 3<\/code><\/td>\n<td><code>True<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>&gt;<\/code><\/td>\n<td>Greater than<\/td>\n<td><code>7 &gt; 9<\/code><\/td>\n<td><code>False<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>&lt;<\/code><\/td>\n<td>Less than<\/td>\n<td><code>7 &lt; 9<\/code><\/td>\n<td><code>True<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>&gt;=<\/code><\/td>\n<td>Greater than or equal to<\/td>\n<td><code>10 &gt;= 10<\/code><\/td>\n<td><code>True<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>&lt;=<\/code><\/td>\n<td>Less than or equal to<\/td>\n<td><code>4 &lt;= 3<\/code><\/td>\n<td><code>False<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>Basic Example<\/h2>\n<pre class=\"wp-block-code\"><code>a = 15\nb = 10\nprint(a == b)\nprint(a != b)\nprint(a &gt; b)\nprint(a &lt; b)\nprint(a &gt;= 15)\nprint(b &lt;= 9)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>False\nTrue\nTrue\nFalse\nTrue\nFalse<\/code><\/pre>\n<h2>Using Comparisons in if Statements<\/h2>\n<p>This is where you will use comparison operators most. Python checks each condition from the top and runs the first block whose condition is True:<\/p>\n<pre class=\"wp-block-code\"><code>marks = 72\nif marks &gt;= 90:\n    print(\"Grade A\")\nelif marks &gt;= 60:\n    print(\"Grade B\")\nelse:\n    print(\"Grade C\")<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>Grade B<\/code><\/pre>\n<p>72 is not &gt;= 90, so the first block is skipped. 72 &gt;= 60 is True, so &#8220;Grade B&#8221; is printed and the rest is ignored.<\/p>\n<h2>Chaining Comparisons<\/h2>\n<p>Python lets you write a range check the way you would in maths. <code>1 &lt; x &lt; 10<\/code> means <code>1 &lt; x and x &lt; 10<\/code>.<\/p>\n<pre class=\"wp-block-code\"><code>x = 7\nprint(1 &lt; x &lt; 10)\nprint(1 &lt; x &gt; 10)\nprint(10 &gt; x &gt;= 7)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>True\nFalse\nTrue<\/code><\/pre>\n<p>The second line is read as <code>1 &lt; x and x &gt; 10<\/code>, and 7 is not greater than 10, so the answer is False. Chaining works with any comparison operators, but keep it readable; a chain like <code>1 &lt; x &gt; 10<\/code> confuses readers.<\/p>\n<h2>Comparing Strings<\/h2>\n<p>Strings are compared character by character using each character&#8217;s Unicode number (you can see it with <code>ord()<\/code>). This is why capital letters come before small letters.<\/p>\n<pre class=\"wp-block-code\"><code>print(\"apple\" == \"apple\")\nprint(\"Apple\" == \"apple\")\nprint(\"apple\" &lt; \"banana\")\nprint(\"Zebra\" &lt; \"apple\")\nprint(ord(\"Z\"), ord(\"a\"))\nprint(\"10\" &lt; \"9\")<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>True\nFalse\nTrue\nTrue\n90 97\nTrue<\/code><\/pre>\n<p>Two results surprise most beginners:<\/p>\n<ul>\n<li><code>\"Zebra\" &lt; \"apple\"<\/code> is True because &#8220;Z&#8221; is 90 and &#8220;a&#8221; is 97. To compare without caring about case, convert both first: <code>\"Zebra\".lower() &lt; \"apple\".lower()<\/code>.<\/li>\n<li><code>\"10\" &lt; \"9\"<\/code> is True because the strings are compared character by character, and &#8220;1&#8221; comes before &#8220;9&#8221;. Numbers typed by a user arrive as strings from <code>input()<\/code>, so convert them with <code>int()<\/code> or <code>float()<\/code> before comparing.<\/li>\n<\/ul>\n<h2>Comparing Lists and Tuples<\/h2>\n<p>Lists and tuples are compared item by item from the left. The first pair of items that differ decides the result.<\/p>\n<pre class=\"wp-block-code\"><code>print([1, 2, 3] &lt; [1, 2, 4])\nprint((2, 5) &gt; (2, 3, 9))\nprint([1, 2] == [1, 2])<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>True\nTrue\nTrue<\/code><\/pre>\n<p>In the second line the first items are equal (2 and 2), then 5 &gt; 3 decides the answer. The extra 9 is never looked at.<\/p>\n<h2>Comparing Different Types<\/h2>\n<pre class=\"wp-block-code\"><code>print(5 == 5.0)\nprint(5 == \"5\")\ntry:\n    print(5 &lt; \"5\")\nexcept TypeError as e:\n    print(\"TypeError:\", e)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>True\nFalse\nTypeError: '&lt;' not supported between instances of 'int' and 'str'<\/code><\/pre>\n<p>An integer and a float can be compared freely. <code>==<\/code> between unrelated types such as a number and a string simply returns False, but ordering operators like <code>&lt;<\/code> raise a <code>TypeError<\/code>.<\/p>\n<h2>The Floating-Point Trap<\/h2>\n<pre class=\"wp-block-code\"><code>print(0.1 + 0.2 == 0.3)\nprint(0.1 + 0.2)\n\nimport math\nprint(math.isclose(0.1 + 0.2, 0.3))<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>False\n0.30000000000000004\nTrue<\/code><\/pre>\n<p>Computers store most decimal numbers as close approximations, so a tiny error creeps in. Never use <code>==<\/code> to compare results of float arithmetic. Use <code>math.isclose()<\/code> instead. (For money, many programs store paise as whole numbers, or use the <code>decimal<\/code> module.)<\/p>\n<h2>== vs is<\/h2>\n<p><code>==<\/code> asks &#8220;do these have the same value?&#8221;, while <code>is<\/code> asks &#8220;are these the very same object?&#8221; Two separate lists with the same items are <code>==<\/code> but not <code>is<\/code>. Use <code>==<\/code> for comparing values, and <code>is<\/code> only for checks like <code>if result is None:<\/code>. See <a href=\"https:\/\/gyanipandit.com\/programming\/identity-operators-in-python\/\">identity operators in Python<\/a> for details.<\/p>\n<h2>Practice Questions<\/h2>\n<ol>\n<li><code>print(\"cat\" &gt; \"Cat\")<\/code><\/li>\n<li><code>print(3 == 3.0 == 3)<\/code><\/li>\n<li><code>print(2 &lt; 4 &lt; 3)<\/code><\/li>\n<\/ol>\n<p><strong>Answers:<\/strong> 1) <code>True<\/code>, because &#8220;c&#8221; (99) is greater than &#8220;C&#8221; (67). 2) <code>True<\/code>, because both comparisons in the chain are True. 3) <code>False<\/code>, because the chain means <code>2 &lt; 4 and 4 &lt; 3<\/code>, and 4 &lt; 3 is False.<\/p>\n<h2>Frequently Asked Questions<\/h2>\n<h3>How many comparison operators are there in Python?<\/h3>\n<p>Six: <code>==<\/code>, <code>!=<\/code>, <code>&gt;<\/code>, <code>&lt;<\/code>, <code>&gt;=<\/code> and <code>&lt;=<\/code>.<\/p>\n<h3>What is the difference between = and == in Python?<\/h3>\n<p><code>=<\/code> assigns a value to a variable. <code>==<\/code> compares two values and returns True or False.<\/p>\n<h3>Can I compare a string with a number?<\/h3>\n<p>With <code>==<\/code>, yes, but the result is always False. With <code>&lt;<\/code> or <code>&gt;<\/code>, Python raises a TypeError. Convert the string with <code>int()<\/code> or <code>float()<\/code> first.<\/p>\n<h3>Does Python support a &lt; b &lt; c?<\/h3>\n<p>Yes. Comparison chaining is built into Python, and <code>a &lt; b &lt; c<\/code> means <code>a &lt; b and b &lt; c<\/code>.<\/p>\n<p>Next: <a href=\"https:\/\/gyanipandit.com\/programming\/python-bitwise-operators\/\">Python bitwise operators<\/a> \u00b7 Previous: <a href=\"https:\/\/gyanipandit.com\/programming\/python-logical-operators\/\">Python logical operators<\/a> \u00b7 All operators: <a href=\"https:\/\/gyanipandit.com\/programming\/python-operators\/\">Python operators overview<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Comparison operators compare two values and always give back a Boolean: True or False. They are the backbone of every if statement and loop condition you will write, for example &#8220;is the marks value at least 33?&#8221; or &#8220;has the user typed the right PIN?&#8221; Python has six comparison operators (also called relational operators). The &#8230; <a title=\"Python Comparison Operators with Examples\" class=\"read-more\" href=\"https:\/\/gyanipandit.com\/programming\/python-comparison-operators\/\" aria-label=\"Read more about Python Comparison Operators 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-3121","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\/3121","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=3121"}],"version-history":[{"count":3,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3121\/revisions"}],"predecessor-version":[{"id":6128,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3121\/revisions\/6128"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=3121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=3121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=3121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}