{"id":854,"date":"2019-12-11T03:50:47","date_gmt":"2019-12-11T03:50:47","guid":{"rendered":"https:\/\/www.askpython.com\/?p=854"},"modified":"2023-05-05T11:31:51","modified_gmt":"2023-05-05T11:31:51","slug":"python-comparison-operators","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/python-comparison-operators","title":{"rendered":"Python Comparison Operators"},"content":{"rendered":"\n<p>Operators are symbols in programming languages that perform the operation on the operands and decide the operation type. For instance, the equal to ( == ) operator checks for equal values and returns True if they are equal, and False otherwise, whereas the not equal ( !=) operator does the exact opposite.<\/p>\n\n\n\n<p>In Python, <a href=\"https:\/\/www.askpython.com\/python\/python-operators\" target=\"_blank\" data-type=\"URL\" data-id=\"https:\/\/www.askpython.com\/python\/python-operators\" rel=\"noreferrer noopener\">operators<\/a> can be divided into several categories based on their functionality such as Arithmetic Operators to perform mathematical operations, Comparison Operators to compare two values, Logical Operators to combine multiple conditions and evaluate whether they are True or False, <a href=\"https:\/\/www.askpython.com\/python\/python-bitwise-operators\" target=\"_blank\" data-type=\"URL\" data-id=\"https:\/\/www.askpython.com\/python\/python-bitwise-operators\" rel=\"noreferrer noopener\">Bitwise Operators <\/a>to perform operations on the individual bits of a binary number, etc.&nbsp;<\/p>\n\n\n\n<p>In this tutorial, we will learn about all types of Comparison Operators and demonstrate them with examples to understand their working.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Comparison Operators<\/h2>\n\n\n\n<p>Python comparison operators, also known as relational operators, are used in comparing two values and applying conditions respectively.&nbsp;<\/p>\n\n\n\n<p>Here there can be two possible outputs, either boolean <code>True<\/code> or <code>False<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"382\" height=\"182\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/12\/Python-Comparison-Operators-Flowchart-1.png\" alt=\"Python Comparison Operators Flowchart\" class=\"wp-image-860\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/12\/Python-Comparison-Operators-Flowchart-1.png 382w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/12\/Python-Comparison-Operators-Flowchart-1-300x143.png 300w\" sizes=\"auto, (max-width: 382px) 100vw, 382px\" \/><figcaption class=\"wp-element-caption\">Python Comparison Operators Flowchart<\/figcaption><\/figure>\n\n\n\n<p>There are several types of comparison operators, each performing a unique operation, let&#8217;s look at them one by one.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Types of Comparison Operators in Python<\/h2>\n\n\n\n<p>There are 6 types of comparison operators in Python:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Less Than ( &lt; )<\/strong><\/li>\n\n\n\n<li><strong>Greater Than ( &gt; )<\/strong><\/li>\n\n\n\n<li><strong>Equal To ( == )<\/strong><\/li>\n\n\n\n<li><strong>Not Equal ( != ) <\/strong><\/li>\n\n\n\n<li><strong>Less Than or Equal To ( &lt;= )<\/strong><\/li>\n\n\n\n<li><strong>Greater Than or Equal To ( &gt;= )<\/strong><\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"467\" height=\"232\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/12\/Python-Comparison-Operators.png\" alt=\"Python Comparison Operators\" class=\"wp-image-861\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/12\/Python-Comparison-Operators.png 467w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/12\/Python-Comparison-Operators-300x149.png 300w\" sizes=\"auto, (max-width: 467px) 100vw, 467px\" \/><figcaption class=\"wp-element-caption\">Python Comparison Operators<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">1. Less Than ( &lt; ) <\/h3>\n\n\n\n<p>It is used to check for the smaller value or variable containing a smaller value as compared with the other number or variable. It will return True if the provided number or a variable is smaller than the given number or variable, otherwise, it will return False.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\na = 10\nif (a &lt; 10):\n print(&quot;True&quot;)\nelse:\n  print(&quot;False&quot;)\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>: <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nFalse\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">2. Greater Than ( &gt; ) <\/h3>\n\n\n\n<p>Python Greater Than operator is used to check for the greater value or variable containing greater value as compared with the other number or variable. If the provided number or a variable is greater than the given number or variable then it will return True, else, it will return false.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\na = 10\nif (a &gt; 10):\n print(&quot;True&quot;)\nelse:\n  print(&quot;False&quot;)\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>: <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nFalse\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">3. Equal To ( == ) <\/h3>\n\n\n\n<p>Python Equal To operator checks for equal values. It compares elements and if they are equal then it will return True else False.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\na = 10\nb = 20\nif (a == b):\n print(&quot;True&quot;)\nelse:\n print(&quot;False&quot;)\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>: <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nFalse\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">4. Not Equal ( != ) <\/h3>\n\n\n\n<p>Python <a href=\"https:\/\/www.askpython.com\/python\/python-not-equal-operator\" target=\"_blank\" data-type=\"URL\" data-id=\"https:\/\/www.askpython.com\/python\/python-not-equal-operator\" rel=\"noreferrer noopener\">Not Equal<\/a> operator is denoted by <strong><code>!=<\/code><\/strong>, this does the exact opposite of the equal to operator. It returns True if the values on either side of the operator are unequal.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(3!=3.0)\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>: <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nFalse\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">5. Less than or Equal to (&lt;=)<\/h3>\n\n\n\n<p> This operator executes to <code>True<\/code> only if the value on the left is less than or equal to that on the right. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\na = 15\nb = 5\nif(b &lt;= a):\n print(&quot;b is either less or equal to a&quot;)\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>: <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nb is either less or equal to a\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">6. Greater than or Equal to (&gt;=)<\/h3>\n\n\n\n<p> This operator executes to <code>True<\/code> only if the value on the left is greater than or equal to that on the right. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\na = 5\nb = 15\nif(b &gt;= a):\n print(&quot;b is either greater or equal to a&quot;)\n<\/pre><\/div>\n\n\n<p> <strong>Output<\/strong>: <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nb is either greater or equal to a\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Python Comparison Operators Example<\/h2>\n\n\n\n<p>Let&#8217;s write the code to demonstrate each comparison operator we have seen earlier.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\na = 10\nb = 5\nc = 0\n \nif a == b:\n   print(&quot;a is equal to b&quot;)\nelse:\n   print(&quot;a is not equal to b&quot;)\n \nif a != b:\n   print(&quot;a is not equal to b&quot;)\nelse:\n   print(&quot;a is equal to b&quot;)\n \nif a &lt; b:\n   print(&quot;a is less than b&quot;)\nelse:\n   print(&quot;a is not less than b&quot;)\n \nif a &gt; b:\n   print(&quot;a is greater than b&quot;)\nelse:\n   print(&quot;a is not greater than b&quot;)\n \na = 6;\nb = 15;\nif a &lt;= b:\n   print(&quot;a is either less than or equal to b&quot;)\nelse:\n   print(&quot;a is neither less than nor equal to b&quot;)\n \nif b &gt;= a:\n   print(&quot;b is either greater than or equal to a&quot;)\nelse:\n   print(&quot;b is neither greater than nor equal to a&quot;)\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">a is not equal to b\na is not equal to b\na is not equal to b\na is not less than b\na is greater than b\na is either less than or equal to b\nb is either greater than or equal to b<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>During this tutorial, we have discussed 6 operators that programmers used to compare values, also called Python comparison operators, let us summarize them one by one.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Less than ( &lt; )<\/strong> returns True if the value of the left operand is lesser than the value of the right operand.<\/li>\n\n\n\n<li><strong>Greater than ( &gt; )<\/strong>&nbsp; returns True if the value of the left operand is greater than the value of the right operand.<\/li>\n\n\n\n<li><strong>Equal to ( == )<\/strong> returns True if the value of two operands is equal.&nbsp;<\/li>\n\n\n\n<li><strong>Not equal ( != )<\/strong>\u00a0 is the exact opposite of the equal to operator.<\/li>\n\n\n\n<li><strong>Less than or equal To (&lt;=)<\/strong> returns True if the value on the left is less or equal to that on the right.<\/li>\n\n\n\n<li><strong>Greater than or equal to (&gt;=)<\/strong> returns True if the value on the left is greater than or equal to that on the right.<\/li>\n<\/ol>\n\n\n\n<p>Hope you now have a clear understanding of Python comparison operators.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Reference<\/h2>\n\n\n\n<p><a href=\"https:\/\/docs.python.org\/3\/reference\/expressions.html#comparisons\" target=\"_blank\" rel=\"noopener\">https:\/\/docs.python.org\/3\/reference\/expressions.html#comparisons<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Operators are symbols in programming languages that perform the operation on the operands and decide the operation type. For instance, the equal to ( == ) operator checks for equal values and returns True if they are equal, and False otherwise, whereas the not equal ( !=) operator does the exact opposite. In Python, operators [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":50213,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-854","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/854","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=854"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/854\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/50213"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=854"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=854"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}