{"id":6966,"date":"2024-04-11T12:00:25","date_gmt":"2024-04-11T12:00:25","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=6966"},"modified":"2024-04-11T12:00:25","modified_gmt":"2024-04-11T12:00:25","slug":"python-comparison-operators","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/python-comparison-operators\/","title":{"rendered":"Python Comparison Operators"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#what-is-a-python-comparison-operator\">What is a Python  Comparison Operator<\/a><\/li><li ><a href=\"#comparison-of-float-number\">Comparison of Float number<\/a><\/li><li ><a href=\"#what-is-the-comparison-of-complex-numbers\">What is the Comparison of Complex numbers?<\/a><\/li><li ><a href=\"#comparison-of-booleans\">Comparison of Booleans<\/a><\/li><li ><a href=\"#what-is-the-comparison-of-sequence-types\">What is the Comparison of Sequence Types?<\/a><\/li><li ><a href=\"#what-is-the-comparison-of-dictionary-objects\">What is the Comparison of Dictionary objects?<\/a><\/li><li ><a href=\"#conclusion\">Conclusion<\/a><\/li><li ><a href=\"#python-comparison-operators-fa-qs\">Python Comparison Operators- FAQs<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<p>Python comparison operators will compare two values.  == or equal to,  !=&nbsp;or not equal to,  &gt; or greater than, &gt;= or greater than or equal to, &lt; or less than, and  &lt;= or less than or equal to are the six Python comparison operators. Read this article to learn more about Python Comparison Operators.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-a-python-comparison-operator\">What is a Python  Comparison Operator<\/h2>\n\n\n\n<p>Comparison Operators in Python play an essential role in Python&#8217;s conditional statements (if, else, and elif) and <a href=\"https:\/\/www.skillvertex.com\/blog\/do-while-loop-in-c\/\" data-type=\"post\" data-id=\"2291\">looping statements<\/a> (while loops). These comparison operators are also known as relational operators. Hence, the known operators will stands for &lt; and &gt; will be for greater operators.<\/p>\n\n\n\n<p>Furthermore,<a href=\"https:\/\/www.skillvertex.com\/blog\/python-literals\/\" data-type=\"post\" data-id=\"6953\"> python<\/a> will use two operators by adding the  =  symbol with these two. The &lt;= symbol will be less than or equal to the operator and these &gt;= symbols will stand for greater than or equal to the operator.<\/p>\n\n\n\n<p>It is known that Python has two operators that are &#8221;==&#8221; and &#8221; !=&#8221;. The &lt;= symbol will stand for less than or equal to the operator, whereas the &#8221;&gt;=&#8221; is referred to as the greater than or equal to the operator.<\/p>\n\n\n\n<p>Look at the table below to learn more about six comparison operators in Python.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>&lt;<\/td><td>Less than<\/td><td>a&lt;b<\/td><\/tr><tr><td>&gt;<\/td><td>Greater than<\/td><td>a&gt;b<\/td><\/tr><tr><td>&lt;=<\/td><td>Less than or equal to<\/td><td>a&lt;=b<\/td><\/tr><tr><td>&gt;=<\/td><td>Greater than or equal to<\/td><td>a&gt;=b<\/td><\/tr><tr><td>==<\/td><td>Is equal to<\/td><td>a==b<\/td><\/tr><tr><td>!=<\/td><td>Is not equal to<\/td><td>a!=b<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Comparison <a href=\"https:\/\/www.skillvertex.com\/blog\/python-operators\/\" data-type=\"post\" data-id=\"6937\">operators<\/a> are mostly <a href=\"https:\/\/www.skillvertex.com\/blog\/difference-between-binary-search-and-linear-search\/\" data-type=\"post\" data-id=\"520\">binar<\/a>y and need two operands. An expression that has a comparison operator is known as a Boolean expression. This will either come as true or false.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a=5\nb=7\nprint (a&gt;b)\nprint (a&lt;b)<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>False\nTrue<\/code><\/pre>\n\n\n\n<p>Both the operands can either be <a href=\"https:\/\/www.skillvertex.com\/blog\/python-literals\/\" data-type=\"post\" data-id=\"6953\">Python literals<\/a>,<a href=\"https:\/\/www.skillvertex.com\/blog\/python-variables\/\" data-type=\"post\" data-id=\"6932\"> variables<\/a> or expressions. As Python has mixed <a href=\"https:\/\/www.skillvertex.com\/blog\/python-arithmetic-operators\/\" data-type=\"post\" data-id=\"6958\">arithmetic<\/a> it is possible to have any number-type operands.<\/p>\n\n\n\n<p>The code provided below  will show the use of Python comparison operators with<a href=\"https:\/\/www.skillvertex.com\/blog\/integer-promotions-in-c\/\" data-type=\"post\" data-id=\"1968\"> integer<\/a> numbers<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print (\"Both operands are integer\")\na=5\nb=7\nprint (\"a=\",a, \"b=\",b, \"a&gt;b is\", a&gt;b)\nprint (\"a=\",a, \"b=\",b,\"a&lt;b is\",a&lt;b)\nprint (\"a=\",a, \"b=\",b,\"a==b is\",a==b)\nprint (\"a=\",a, \"b=\",b,\"a!=b is\",a!=b)<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Both operands are integer\na= 5 b= 7 a&gt;b is False\na= 5 b= 7 a&lt;b is True\na= 5 b= 7 a==b is False\na= 5 b= 7 a!=b is True<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"comparison-of-float-number\">Comparison of Float number<\/h2>\n\n\n\n<p>This example will show the comparison of an integer and float operand.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print (\"comparison of int and float\")\na=10\nb=10.0\nprint (\"a=\",a, \"b=\",b, \"a&gt;b is\", a&gt;b)\nprint (\"a=\",a, \"b=\",b,\"a&lt;b is\",a&lt;b)\nprint (\"a=\",a, \"b=\",b,\"a==b is\",a==b)\nprint (\"a=\",a, \"b=\",b,\"a!=b is\",a!=b)<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>comparison of int and float\na= 10 b= 10.0 a&gt;b is False\na= 10 b= 10.0 a&lt;b is False\na= 10 b= 10.0 a==b is True\na= 10 b= 10.0 a!=b is False<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-the-comparison-of-complex-numbers\">What is the Comparison of Complex numbers?<\/h2>\n\n\n\n<p>A comple<a href=\"https:\/\/www.skillvertex.com\/blog\/which-is-not-an-ado-net-dataadapter-object\/\" data-type=\"post\" data-id=\"2225\">x object<\/a> is considered as the number data type in Python. It will work differently from others in behavior.  Python won&#8217;t support &lt; and&gt; operators as it cannot support the equality  ( ==) and inequality (!=)  operators.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print (\"comparison of complex numbers\")\na=10+1j\nb=10.-1j\nprint (\"a=\",a, \"b=\",b,\"a==b is\",a==b)\nprint (\"a=\",a, \"b=\",b,\"a!=b is\",a!=b)\n\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>comparison of complex numbers\na= (10+1j) b= (10-1j) a==b is False\na= (10+1j) b= (10-1j) a!=b is True<\/code><\/pre>\n\n\n\n<p>Here, it is possible to get a type error that is less than or greater than the operators.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print (\"comparison of complex numbers\")\na=10+1j\nb=10.-1j\nprint (\"a=\",a, \"b=\",b,\"a&lt;b is\",a&lt;b)\nprint (\"a=\",a, \"b=\",b,\"a&gt;b is\",a&gt;b)<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>comparison of complex numbers\nTraceback (most recent call last):\n  File \"C:\\Users\\mlath\\examples\\example.py\", line 5, in &lt;module&gt;\n    print (\"a=\",a, \"b=\",b,\"a&lt;b is\",a&lt;b)\n                                      ^^^\nTypeError: '&lt;' not supported between instances of 'complex' and\n'complex<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"comparison-of-booleans\">Comparison of Booleans<\/h2>\n\n\n\n<p>Boolean objects are referred to as real integers such as true is  1 and false is 0.  However, python will consider any non-zero numbers as true=.  Hence, it is allowed to compare the boolean objects. False&lt; True is True.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print (\"comparison of Booleans\")\na=True\nb=False\nprint (\"a=\",a, \"b=\",b,\"a&lt;b is\",a&lt;b)\nprint (\"a=\",a, \"b=\",b,\"a&gt;b is\",a&gt;b)\nprint (\"a=\",a, \"b=\",b,\"a==b is\",a==b)\nprint (\"a=\",a, \"b=\",b,\"a!=b is\",a!=b)<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>comparison of Booleans\na= True b= False a&lt;b is False\na= True b= False a&gt;b is True\na= True b= False a==b is False\na= True b= False a!=b is True<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-the-comparison-of-sequence-types\">What is the Comparison of Sequence Types?<\/h2>\n\n\n\n<p>The comparison of one similar sequence object will be operated in Python. A string object can be compared with another string only.  A list can&#8217;t be compared with a tuple even if it holds the same items.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print (\"comparison of different sequence types\")\na=(1,2,3)\nb=&#91;1,2,3]\nprint (\"a=\",a, \"b=\",b,\"a&lt;b is\",a&lt;b)<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>comparison of different sequence types\nTraceback (most recent call last):\n  File \"C:\\Users\\mlath\\examples\\example.py\", line 5, in &lt;module&gt;\n    print (\"a=\",a, \"b=\",b,\"a&lt;b is\",a&lt;b)\n                                       ^^^\nTypeError: '&lt;' not supported between instances of 'tuple' and 'list'<\/code><\/pre>\n\n\n\n<p>From the example provided above, it is understood that the sequence objects will be compared with the help of a lexicographical ordering mechanism. This comparison will begin with the item at the 0th index. Hence, if they come as equal, this comparison will move to the next index until they get the items at the certain index unequal or the sequence is exhausted.<\/p>\n\n\n\n<p>However, if one sequence is considered as the initial sub-sequence of the other, then the shorter sequence will be the lesser one.<\/p>\n\n\n\n<p>The operators will be considered greater and that will depend on the difference in the values of items when they are not equal. An example of this is BAT&#8217;&gt;&#8217;BAR&#8217; is True, if the T comes after R in Unicode order.<\/p>\n\n\n\n<p>All the items in the two sequences will be compared for equity. Thus, the sequence will be equal while comparison.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print (\"comparison of strings\")\na='BAT'\nb='BALL'\nprint (\"a=\",a, \"b=\",b,\"a&lt;b is\",a&lt;b)\nprint (\"a=\",a, \"b=\",b,\"a&gt;b is\",a&gt;b)\nprint (\"a=\",a, \"b=\",b,\"a==b is\",a==b)\nprint (\"a=\",a, \"b=\",b,\"a!=b is\",a!=b)<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>comparison of strings\na= BAT b= BALL a&lt;b is False\na= BAT b= BALL a&gt;b is True\na= BAT b= BALL a==b is False\na= BAT b= BALL a!=b is True<\/code><\/pre>\n\n\n\n<p>Another example given below illustrates the two tuple objects.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print (\"comparison of tuples\")\na=(1,2,4)\nb=(1,2,3)\nprint (\"a=\",a, \"b=\",b,\"a&lt;b is\",a&lt;b)\nprint (\"a=\",a, \"b=\",b,\"a&gt;b is\",a&gt;b)\nprint (\"a=\",a, \"b=\",b,\"a==b is\",a==b)\nprint (\"a=\",a, \"b=\",b,\"a!=b is\",a!=b)<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a= (1, 2, 4) b= (1, 2, 3) a&lt;b is False\na= (1, 2, 4) b= (1, 2, 3) a&gt;b is True\na= (1, 2, 4) b= (1, 2, 3) a==b is False\na= (1, 2, 4) b= (1, 2, 3) a!=b is True<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-the-comparison-of-dictionary-objects\">What is the Comparison of Dictionary objects?<\/h2>\n\n\n\n<p>While comparing the dictionary objects, &#8220;&lt; &#8221; and &#8220;&gt;&#8221; operators are used in the Python dictionary and they are not defined. Whereas, in operands, this type error &#8220;&lt;&#8221; cannot be supported in between the instances of &#8216;dict&#8217; and  &#8216;dict&#8217; will be reported.<\/p>\n\n\n\n<p>Whereas. the equality comparison will monitor if the length of both the dict items is the same. The length of the dictionary will be considered as the number of key-value pairs in it. <\/p>\n\n\n\n<p> However,<a href=\"https:\/\/www.skillvertex.com\/blog\/python-syntax\/\" data-type=\"post\" data-id=\"6924\"> python dictionaries<\/a> will be compared with the length. The dictionaries that have fewer elements won&#8217;t be referred to as dictionaries. Since dictionaries require more <a href=\"https:\/\/www.skillvertex.com\/blog\/which-one-is-not-an-element-of-iot\/\" data-type=\"post\" data-id=\"3089\">elements.<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print (\"comparison of dictionary objects\")\na={1:1,2:2}\nb={2:2, 1:1, 3:3}\nprint (\"a=\",a, \"b=\",b,\"a==b is\",a==b)\nprint (\"a=\",a, \"b=\",b,\"a!=b is\",a!=b)<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>comparison of dictionary objects\na= {1: 1, 2: 2} b= {2: 2, 1: 1, 3: 3} a==b is False\na= {1: 1, 2: 2} b= {2: 2, 1: 1, 3: 3} a!=b is True<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p>Comparison  Operators in Python is a relevant topic for beginners to learn. Learning these operators will be useful during coding and writing  Python. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"python-comparison-operators-fa-qs\">Python Comparison Operators- FAQs<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1707376325247\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. What are the 6 comparison operators in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans == or equal to,  !=\u00a0or not equal to,  > or greater than, >= or greater than or equal to, &lt; or less than, and  &lt;= or less than or equal to <\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1707376333652\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. What is Elif in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. Elif will stand for if else and it functions to test the multiple conditions in Python.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1707376348652\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3.  What is float in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. Float is referred to as the reusable code in Python that can turn values into floating point numbers.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Python comparison operators will compare two values. == or equal to, !=&nbsp;or not equal to, &gt; or greater than, &gt;= or greater than or equal to, &lt; or less than, and &lt;= or less than or equal to are the six Python comparison operators. Read this article to learn more about Python Comparison Operators. What &#8230; <a title=\"Python Comparison Operators\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/python-comparison-operators\/\" aria-label=\"More on Python Comparison Operators\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":6967,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[864],"tags":[57,890,882],"class_list":["post-6966","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorial","tag-python","tag-python-comparison-operators","tag-python-operators","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/6966","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=6966"}],"version-history":[{"count":8,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/6966\/revisions"}],"predecessor-version":[{"id":8901,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/6966\/revisions\/8901"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/6967"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=6966"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=6966"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=6966"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}