{"id":74565,"date":"2019-12-24T09:47:30","date_gmt":"2019-12-24T04:17:30","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=74565"},"modified":"2024-08-22T20:17:47","modified_gmt":"2024-08-22T14:47:47","slug":"python-comparison-operators","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/python-comparison-operators\/","title":{"rendered":"Python Comparison Operators &#8211;  Learn how to compare values in Python"},"content":{"rendered":"<p>In this article, we are going to learn about the python comparison operators.<\/p>\n<p>Python comparison operators are also known as <strong>relational operators<\/strong>. It\u2019s very important to understand everything about them as you will be using them a lot when writing codes.<\/p>\n<h3>Python Comparison Operators<a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/12\/python-comparison-operators.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-74715 aligncenter\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/12\/python-comparison-operators.jpg\" alt=\"comparison operators in python\" width=\"802\" height=\"420\" \/><\/a><\/h3>\n<p>There are many types of Python comparison operators.<\/p>\n<p>They include<strong> Less than(&lt;)<\/strong>, <strong>Greater than(&gt;)<\/strong>,<strong> Less than or equal to(&lt;=)<\/strong>, <strong>Greater than or equal to(&gt;=)<\/strong>,<strong> Equal to(==)<\/strong> and<strong> Not equal to (!=)<\/strong>.<\/p>\n<p>The comparison operators return <strong>True<\/strong> or <strong>False<\/strong> by evaluating the expression.<\/p>\n<h3>Types of Python Comparison Operators<\/h3>\n<h4>1. Less than (&lt;)<\/h4>\n<p>The first comparison operator we will see is the <strong>less-than<\/strong> operator. It\u2019s denoted by <strong>\u2018&lt;\u2019<\/strong> and it is used to check if the left value is less than the right value or not. If the left operand is indeed smaller, it returns True; otherwise, it returns False. This operator is commonly used in conditional statements and loops to control the flow of a program based on numerical comparisons.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">8&lt;10<\/pre>\n<p><strong>Output: <\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<p>8 is less than 10 so, it returns <strong>True<\/strong>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">20&lt;20<\/pre>\n<p><strong>Output: <\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<p>It results in <strong>False<\/strong> as 20 is equal to 20 and not less than 20.<\/p>\n<p><strong>Float <\/strong>values can also be evaluated with these operators.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">5.2&lt;5.4<\/pre>\n<p><strong>Output: <\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<p>Also, you can try this operator with <strong>strings<\/strong>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">\u2018a\u2019 &lt; \u2018b\u2019<\/pre>\n<p><strong>Output: <\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">\u2018abcd\u2019 &lt; \u2018abca\u2019<\/pre>\n<p><strong>Output: <\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<p>A fascinating thing about this is we can also <strong>compare tuples<\/strong> using this operator.<\/p>\n<p>Let\u2019s see them in action.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">(1,2,3) &lt; (1,2,3,4)<\/pre>\n<p><strong>Output: <\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">(1,2,3) &lt; (1,2,3)<\/pre>\n<p><strong>Output: <\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<p>This also works on lists. It compares list <strong>index by index<\/strong>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">[1,2,3] &lt; [1,2,4]<\/pre>\n<p><strong>Output: <\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">[5,9,10] &lt; [5,8,5]<\/pre>\n<p><strong>Output: <\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<p>This doesn\u2019t work on <strong>dictionaries<\/strong>.<\/p>\n<h4>2. Greater than (&gt;)<\/h4>\n<p>Let\u2019s see the Python <strong>greater than<\/strong> symbol. It\u2019s denoted by <strong>\u2018&gt;\u2019<\/strong> symbol and it checks whether the value on the left side is greater than the right side. If the left operand is larger, it returns True; otherwise, it returns False. This operator is frequently used in conditions and loops to execute code only when certain criteria are met, making it essential for comparison operations.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">0.5 &gt; False<\/pre>\n<p><strong>Output: <\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<p>Since False is considered as <strong>zero<\/strong> and 0.5 is greater than 0.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">\u2018a\u2019&gt;\u2019A\u2019<\/pre>\n<p><strong>Output: <\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<p>In the case of <strong>strings<\/strong>, Python compares the <strong>ASCII<\/strong> values of the characters. Here, \u2018a\u2019 ASCII value is 97 and \u2018A\u2019 ASCII value is 65 that\u2019s why \u2018a\u2019 is greater than \u2018A\u2019.<\/p>\n<h4>3. Less than or equal to (&lt;=)<\/h4>\n<p>Now you get the idea of comparison operators, we can quickly understand the code with examples. The less than or equal to operator, denoted by <strong>\u2018&lt;=\u2019<\/strong> returns True when the <strong>left side<\/strong> operand is either <strong>less than or equal to<\/strong> the <strong>right side<\/strong> operand. This operator is useful in loops and conditional statements where you want to include boundary values in your comparisons, ensuring comprehensive checks within specified limits.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">5&lt;=10<\/pre>\n<p><strong>Output: <\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">10&lt;=10<\/pre>\n<p><strong>Output: <\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<p>As you see, in just less than operator<strong> 10&lt;10<\/strong> would give us <strong>false<\/strong>. But for conditions when we also need to check for <strong>equality<\/strong>, we will use <strong>&lt;=<\/strong>.<\/p>\n<h4>4. Greater than or equal to (&gt;=)<\/h4>\n<p>The greater than or equal to operator is just like<strong> less than or equal to<\/strong>. The only difference is that it checks that the <strong>left side<\/strong> value should be <strong>greater<\/strong> than or equal to the <strong>right side<\/strong> value.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">14&gt;=10<\/pre>\n<p><strong>Output: <\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">4&gt;=5<\/pre>\n<p><strong>Output: <\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<h4>5. Equal to (==)<\/h4>\n<p>The final two operators are <strong>equal to (==)<\/strong> and <strong>not equal to (!=) <\/strong>The equal to operator will return True when both the values on either side of the operator are <strong>equal<\/strong>. You can compare <strong>integers<\/strong>, <strong>float<\/strong>, and also <strong>strings<\/strong>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">23 == 23<\/pre>\n<p><strong>Output: <\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">2 == 2.0<\/pre>\n<p><strong>Output: <\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">13 == \u201813\u2019<\/pre>\n<p><strong>Output: <\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<p>Here, 13 is an <strong>integer<\/strong> value and \u201813\u2019 is a <strong>string<\/strong>, that\u2019s why they are <strong>not equal<\/strong>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">\u201cTechVidvan\u201d == \u201cTechVidvan\u201d<\/pre>\n<p><strong>Output: <\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<p>You can also compare <strong>lists<\/strong> and <strong>set<\/strong>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">{1,2,3} == {3,2,1,1}<\/pre>\n<p><strong>Output: <\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<p>To understand this, you should know about the set. Python set data structure holds <strong>unique values<\/strong> and they <strong>rearrange themselves<\/strong> in a sorted order.<\/p>\n<h4>6. Not equal to (!=)<\/h4>\n<p>The <strong>not equal to<\/strong> operator <strong>(!=) <\/strong>is <strong>opposite<\/strong> to the <strong>equal to<\/strong> operator. It returns true when the values on either side are <strong>unequal<\/strong> to each other.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">\u201c26\u201d != 26<\/pre>\n<p><strong>Output: <\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">\u201cPython\u201d != \u201cPython\u201d<\/pre>\n<p><strong>Output: <\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<p><strong>Note:<\/strong> Python also had<strong> &lt;&gt; operator<\/strong> which had the same purpose as <strong>not equal to operator<\/strong> but it is now been <strong>removed<\/strong> from Python 3 versions.<\/p>\n<h3>Summary<\/h3>\n<p>In today\u2019s python comparison operators article by TechVidvan, we saw the six comparison operators of Python named as less than, greater than, less than or equal to, greater than or equal to, equal to and not equal to operator.<\/p>\n<p>These operators are self-explanatory and very easy to understand. Later on, you will use these operators a lot in decision-making statements.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we are going to learn about the python comparison operators. Python comparison operators are also known as relational operators. It\u2019s very important to understand everything about them as you will be&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":74715,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1053],"tags":[1134,1135,1136,1137,1138,1140],"class_list":["post-74565","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-comparison-operators-in-python","tag-equal-to-comparison-operator","tag-greater-than-comparison-operator","tag-less-than-comparison-operator","tag-python-comparison-operators","tag-types-of-python-comparison-operators"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Comparison Operators - Learn how to compare values in Python - TechVidvan<\/title>\n<meta name=\"description\" content=\"Python Comparison Operators - Learn the basics of comparison operators in Python. See different types of comparison operators with examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/python-comparison-operators\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Comparison Operators - Learn how to compare values in Python - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Python Comparison Operators - Learn the basics of comparison operators in Python. See different types of comparison operators with examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/python-comparison-operators\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2019-12-24T04:17:30+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-08-22T14:47:47+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/12\/python-comparison-operators.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Comparison Operators - Learn how to compare values in Python - TechVidvan","description":"Python Comparison Operators - Learn the basics of comparison operators in Python. See different types of comparison operators with examples.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/python-comparison-operators\/","og_locale":"en_US","og_type":"article","og_title":"Python Comparison Operators - Learn how to compare values in Python - TechVidvan","og_description":"Python Comparison Operators - Learn the basics of comparison operators in Python. See different types of comparison operators with examples.","og_url":"https:\/\/techvidvan.com\/tutorials\/python-comparison-operators\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2019-12-24T04:17:30+00:00","article_modified_time":"2024-08-22T14:47:47+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/12\/python-comparison-operators.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/python-comparison-operators\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-comparison-operators\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Python Comparison Operators &#8211; Learn how to compare values in Python","datePublished":"2019-12-24T04:17:30+00:00","dateModified":"2024-08-22T14:47:47+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-comparison-operators\/"},"wordCount":757,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-comparison-operators\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/12\/python-comparison-operators.jpg","keywords":["comparison operators in python","Equal to comparison operator","Greater than comparison operator","Less than comparison operator","python comparison operators","types of python comparison operators"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/python-comparison-operators\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/python-comparison-operators\/","url":"https:\/\/techvidvan.com\/tutorials\/python-comparison-operators\/","name":"Python Comparison Operators - Learn how to compare values in Python - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-comparison-operators\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-comparison-operators\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/12\/python-comparison-operators.jpg","datePublished":"2019-12-24T04:17:30+00:00","dateModified":"2024-08-22T14:47:47+00:00","description":"Python Comparison Operators - Learn the basics of comparison operators in Python. See different types of comparison operators with examples.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-comparison-operators\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/python-comparison-operators\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/python-comparison-operators\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/12\/python-comparison-operators.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/12\/python-comparison-operators.jpg","width":802,"height":420,"caption":"comparison operators in python"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/python-comparison-operators\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Python Comparison Operators &#8211; Learn how to compare values in Python"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/74565","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=74565"}],"version-history":[{"count":2,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/74565\/revisions"}],"predecessor-version":[{"id":447699,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/74565\/revisions\/447699"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/74715"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=74565"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=74565"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=74565"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}