{"id":29,"date":"2024-01-24T13:14:32","date_gmt":"2024-01-24T13:14:32","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=29"},"modified":"2024-01-24T13:14:34","modified_gmt":"2024-01-24T13:14:34","slug":"python-comparison-operators","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/24\/python-comparison-operators\/","title":{"rendered":"Python Comparison Operators"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn about Python comparison operators and how to use them to compare two values.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to Python comparison operators<\/h2>\n\n\n\n<p>In programming, you often want to compare a value with another value. To do that, you use comparison operators.<\/p>\n\n\n\n<p>Python has six comparison operators, which are as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Less than (&nbsp;<code>&lt;<\/code>&nbsp;)<\/li>\n\n\n\n<li>Less than or equal to (<code>&lt;=<\/code>)<\/li>\n\n\n\n<li>Greater than (<code>&gt;<\/code>)<\/li>\n\n\n\n<li>Greater than or equal to (<code>&gt;=<\/code>)<\/li>\n\n\n\n<li>Equal to (&nbsp;<code>==<\/code>&nbsp;)<\/li>\n\n\n\n<li>Not equal to (&nbsp;<code>!=<\/code>&nbsp;)<\/li>\n<\/ul>\n\n\n\n<p>These comparison operators compare two values and return a\u00a0boolean\u00a0value, either\u00a0<code>True<\/code>\u00a0or\u00a0<code>False<\/code>.<\/p>\n\n\n\n<p>You can use these comparison operators to compare both\u00a0numbers\u00a0and\u00a0strings.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Less than operator (&lt;)<\/h2>\n\n\n\n<p>The Less Than operator (&lt;) compares two values and returns\u00a0<code>True<\/code>\u00a0if the value on the left is less than the value on the right. Otherwise, it returns\u00a0<code>False<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>left_value &lt; right_value<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The following example uses the Less Than (<code>&lt;<\/code>) operator to compare two numbers:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>>>> 10 &lt; 20 True >>> 30 &lt; 20 False<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>It\u2019s quite obvious when you use the less-than operator with the numbers.<\/p>\n\n\n\n<p>The following example uses the less than operator (<code>&lt;<\/code>) to compare two strings:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>>>> 'apple' &lt; 'orange' True >>> 'banana' &lt; 'apple' False<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The expression&nbsp;<code>'apple' &lt; 'orange'<\/code>&nbsp;returns&nbsp;<code>True<\/code>&nbsp;because the letter&nbsp;<code>a<\/code>&nbsp;in&nbsp;<code>apple<\/code>&nbsp;is before the letter&nbsp;<code>o<\/code>&nbsp;in&nbsp;<code>orange<\/code>.<\/p>\n\n\n\n<p>Similarly, the&nbsp;<code>'banana' &lt; 'apple'<\/code>&nbsp;returns&nbsp;<code>False<\/code>&nbsp;because the letter&nbsp;<code>'b'<\/code>&nbsp;is after the letter&nbsp;<code>'a'<\/code>.<\/p>\n\n\n\n<p>The following example shows how to use the less-than operator with\u00a0variables:<code>>>> x = 10 >>> y = 20 >>> x &lt; y True >>> y &lt; x False<\/code><small>Code language: Python (python)<\/small><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Less than or equal to operator (&lt;=)<\/h2>\n\n\n\n<p>The less than or equal to operator compares two values and returns\u00a0<code>True<\/code>\u00a0if the left value is less than or equal to the right value. Otherwise, it returns\u00a0<code>False<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>left_value &lt;= right_value<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The following example shows how to use the less than or equal to operator to compare two numbers:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>>>> 20 &lt;= 20 True >>> 10 &lt;= 20 True >>> 30 &lt;= 30 True<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>This example shows how to use the less than or equal to operator to compare the values of two variables:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>>>> x = 10 >>> y = 20 >>> x &lt;= y True >>> y &lt;= x False<\/code><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Greater than operator (&gt;)<\/h2>\n\n\n\n<p>The greater than the operator (<code>><\/code>) compares two values and returns\u00a0<code>True<\/code>\u00a0if the left value is greater than the right value. Otherwise, it returns\u00a0<code>False<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>left_value > right_value<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>This example uses the greater than operator (<code>><\/code>) to compare two numbers:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>>>> 20 > 10 True >>> 20 > 20 False >>> 10 > 20 False<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The following example uses the greater than operator (<code>><\/code>) to compare two strings:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>>>> 'apple' > 'orange' False >>> 'orange' > 'apple' True<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Greater Than or Equal To operator (<code>&gt;=<\/code>)<\/h2>\n\n\n\n<p>The greater than or equal to operator (<code>>=<\/code>) compares two values and returns\u00a0<code>True<\/code>\u00a0if the left value is greater than or equal to the right value. Otherwise, it returns\u00a0<code>False<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>left_value >= right_value<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The following example uses the greater than or equal to an operator to compare two numbers:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>>>> 20 >= 10 True >>> 20 >= 20 True >>> 10 >= 20 False<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The following example uses the greater than or equal to operator to compare two strings:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>>>> 'apple' >= 'apple' True >>> 'apple' >= 'orange' False >>> 'orange' >= 'apple' True<\/code><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Equal To operator (==)<\/h2>\n\n\n\n<p>The equal to operator (<code>==<\/code>) compares two values and returns\u00a0<code>True<\/code>\u00a0if the left value is equal to the right value. Otherwise, it returns\u00a0<code>False<\/code>\u00a0:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>left_value == right_value<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The following example uses the equal to operator (<code>==<\/code>) to compare two numbers:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>>>> 20 == 10 False >>> 20 == 20 True<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>And the following example uses the equal to operator (<code>==<\/code>) to compare two strings:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>>>> 'apple' == 'apple' True >>> 'apple' == 'orange' False<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Not Equal To operator (!=)<\/h2>\n\n\n\n<p>The not equal to operator (<code>!=<\/code>) compares two values and returns\u00a0<code>True<\/code>\u00a0if the left value isn\u2019t equal to the right value. Otherwise, it returns\u00a0<code>False<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>left_value != right_value<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>For example, the following uses the not equal to operator to compare two numbers:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>>>> 20 != 20 False >>> 20 != 10 True<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The following example uses the not equal to operator to compare two strings:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>>>> 'apple' != 'apple' False >>> 'apple' != 'orange' True<\/code>\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn about Python comparison operators and how to use them to compare two values. Introduction to Python comparison operators In programming, you often want to compare a value with another value. To do that, you use comparison operators. Python has six comparison operators, which are as follows: These comparison operators [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-29","post","type-post","status-publish","format-standard","hentry","category-2-operators"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/29","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=29"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/29\/revisions"}],"predecessor-version":[{"id":30,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/29\/revisions\/30"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=29"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=29"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=29"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}