{"id":8085,"date":"2019-10-22T00:14:21","date_gmt":"2019-10-21T18:44:21","guid":{"rendered":"https:\/\/java2blog.com\/?p=8085"},"modified":"2023-11-25T16:37:04","modified_gmt":"2023-11-25T11:07:04","slug":"python-not-equal-operator","status":"publish","type":"post","link":"https:\/\/java2blog.com\/python-not-equal-operator\/","title":{"rendered":"Python not equal operator"},"content":{"rendered":"<p>In this post, we will see about Python not equal operator.<\/p>\n<p>Not equal operator is denoted by <code>!=<\/code> in Python and returns true when two variables are of same type but have different value. If two variable posses same value, then not equal operator will return <code>False<\/code>.<br \/>\n<div id=\"toc_container\" class=\"toc_light_blue no_bullets\"><p class=\"toc_title\">Table of Contents<\/p><ul class=\"toc_list\"><li><a href=\"#Python_not_equal_operator_example\">Python not equal operator example<\/a><\/li><li><a href=\"#Python_not_equal_operator_in_case_of_Custom_object\">Python not equal operator in case of Custom object<\/a><\/li><\/ul><\/div>\n\n<hr\/>\n<h2><span id=\"Python_not_equal_operator_example\"><span style=\"color: #f89820;\">Python not equal operator example<\/span><\/span><\/h2>\n<p>Here is simple example of non equal operator<\/p>\n<pre class=\"lang:python decode:1 \">\nprint(2.2!=2)\n<\/pre>\n<p>This will print <span style=\"color: #f89820;\">True<\/span>.<\/p>\n<pre code=\"python\">\nstr1 = \"world\"\nstr2 = \"world\"\nstr3 = \"WORLD\"\n\nprint(str1!=str2)\nprint(str1!=str3)\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"content-box-blue\">\nFalse<br \/>\nTrue\n<\/div>\n<p><span style=\"color: #f89820;\"><strong>str1!=str2 returns False:<\/strong><\/span><br \/>\nAs str1 and str2 are exactly same String, so it return False.<\/p>\n<p><strong><span style=\"color: #f89820;\">str1!=str3 returns True:<\/span><\/strong><br \/>\nAs str1 and str3 differ in case, it return True.<\/p>\n<blockquote><p>Please note that you can use &lt;&gt; for not equal in Python 2 but &lt;&gt; is deprecated in Python 3.<\/p><\/blockquote>\n<hr\/>\n<h2><span id=\"Python_not_equal_operator_in_case_of_Custom_object\"><span style=\"color: #f89820;\">Python not equal operator in case of Custom object<\/span><\/span><\/h2>\n<\/p>\n<p>When you call !=, python internally calls <code><strong>ne<\/strong>(self, other)<\/code> method. In case of custom objects, you can implement your own version in the class.<\/p>\n<p>Here is quick example.<\/p>\n<pre code=\"python\">\nclass Country:\n    name = ''\n    population = 0\n\n    def __init__(self, name, population):\n        self.name = name\n        self.population = population\n\n    def __ne__(self, other):\n        # If different type, you can return true\n        if type(other) != type(self):\n            return True\n        if self.name != other.name:\n            return True\n        else:\n            return False\n\nindia1 = Country('India', 1000)\nindia2 = Country('India', 2000)\nchina = Country('China', 3000)\n\nprint(india1 != india2)\nprint(india1 != china)\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"content-box-green\">\nFalse<br \/>\nTrue\n<\/div>\n<p>As you can see, we have implemented <code><strong>ne<\/strong>(self, other)<\/code> and two countries are not equal, if there name are not same.<\/p>\n<p>So that&#8217;s why<br \/>\n<code>india1 != india2<\/code> returned <code>False<\/code> as name is same in both objects even though population is different.<\/p>\n<p>That&#8217;s all about Python not equal operator.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Table of ContentsPython not equal operator examplePython not equal operator in case of Custom object In this post, we will see about Python not equal operator. Not equal operator is denoted by != in Python and returns true when two variables are of same type but have different value. If two variable posses same value, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false},"categories":[145],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/8085"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=8085"}],"version-history":[{"count":1,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/8085\/revisions"}],"predecessor-version":[{"id":25883,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/8085\/revisions\/25883"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=8085"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=8085"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=8085"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}