{"id":3121,"date":"2022-11-04T20:02:43","date_gmt":"2022-11-04T20:02:43","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=3121"},"modified":"2022-11-04T20:02:45","modified_gmt":"2022-11-04T20:02:45","slug":"python-comparison-operators","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/python-comparison-operators\/","title":{"rendered":"Python Comparison Operators"},"content":{"rendered":"\n<p class=\"has-text-align-center\">COMPARISON OPERATORS IN PYTHON<\/p>\n\n\n\n<p>Now, we are going to have a look at some of the comparison operators (<strong>Python Relational operators<\/strong>). Basically, as the name suggests, these operators are going to be used for comparison purposes, like for checking if two values are equal or not, or if one value is greater than the other, and some other such comparisons. Once we have a look at some of the examples, we would have a broader idea about using the comparison operators.<\/p>\n\n\n\n<p>Have a look at the below table, where you can find the comparison operator symbols, with some descriptions, and examples. For the below examples, the value of x is considered to be 5<\/p>\n\n\n\n<h2 class=\"has-text-align-center wp-block-heading\">Python Comparison Operators<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>Operator<\/td><td>Description<\/td><td>Example<\/td><\/tr><tr><td>==<\/td><td>This compares the two values and checks if they are equal.<\/td><td>x == 5. This compares the value of 5 with 5. Printing this will give True as output if the values are equal, and False otherwise. <br>As value of x is 5, the output is going to be True.<\/td><\/tr><tr><td>!=<\/td><td>This compares the two values and checks if they are not equal.<\/td><td>x!=4. This compares the value of x with 4. printing this will give True as output, as x has value 5 and 5 is not equal to 4.<br>So, if the two values are not equal, it will return True, and False otherwise.<\/td><\/tr><tr><td>&gt;=<\/td><td>This compares the two values and checks if one value is greater than or equal to the other value.<\/td><td>x&gt;=7. This compares the value of x with 7. printing this will give False as output, as x has value 5 and 5 is not greater than or equal to 7.<br>So, if one value is greater than or equal to the other value, we get True, and False otherwise.<\/td><\/tr><tr><td>&lt;=<\/td><td>This compares the two values and checks if one value is less than or equal to another value.<\/td><td>x&lt;=6. This compares the value of x with 6. printing this will give True as output, as x has value 5 and 5 is less than 6.<br>So, if one value is less than or equal to the another, the output is True, and False otherwise.<\/td><\/tr><tr><td>&gt;<\/td><td>This compares the two values and checks if one value is greater than the other.<\/td><td>x&gt;2. This compares the value of x with 2. printing this will give True as output, as x has a value greater than 2.<br>So, if one value is greater than the other, the output is True, and False otherwise.<\/td><\/tr><tr><td>&lt;<\/td><td>This compares the two values and checks if one value is less than the other.<\/td><td>x&lt;10. This compares the value of x with 10. printing this will give True as output, as x has value less than 10.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>So, if some value is less than the other value, the output is True, and False otherwise.<\/p>\n\n\n\n<p>Now, let\u2019s have a look at a simple program, which demonstrates about the different comparison operators.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"1081\" height=\"203\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/COMPARISON-OPERATORS-PROGRAM-IN-PYTHON.jpg\" alt=\"\" class=\"wp-image-3107\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/COMPARISON-OPERATORS-PROGRAM-IN-PYTHON.jpg 1081w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/COMPARISON-OPERATORS-PROGRAM-IN-PYTHON-300x56.jpg 300w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/COMPARISON-OPERATORS-PROGRAM-IN-PYTHON-1024x192.jpg 1024w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/COMPARISON-OPERATORS-PROGRAM-IN-PYTHON-768x144.jpg 768w\" sizes=\"auto, (max-width: 1081px) 100vw, 1081px\" \/><\/figure>\n<\/div>\n\n\n<p>As you can see in the above program, we are performing different comparison operations with the value. The comparisons are very readable, and useful as well. Just by reading the comparisons, one can identify the outputs as well, since they are going to be True or False. So, Let\u2019s have a look at the output for the above program.<br>True<br>True<br>False<br>False<br>True<br>True<\/p>\n\n\n\n<p>As you can see, we are getting the outputs accordingly. So, whenever we need to perform some comparisons between the values, we can make use of the different comparison operators.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>COMPARISON OPERATORS IN PYTHON Now, we are going to have a look at some of the comparison operators (Python Relational operators). Basically, as the name suggests, these operators are going to be used for comparison purposes, like for checking if two values are equal or not, or if one value is greater than the other, [&hellip;]<\/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":{"0":"post-3121","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-python-tutorial","7":"tag-python","8":"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":2,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3121\/revisions"}],"predecessor-version":[{"id":3962,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3121\/revisions\/3962"}],"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}]}}