{"id":33498,"date":"2020-03-17T13:07:07","date_gmt":"2020-03-17T07:37:07","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=33498"},"modified":"2020-03-17T13:07:07","modified_gmt":"2020-03-17T07:37:07","slug":"difference-between-and-is-operator-in-python","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/difference-between-and-is-operator-in-python\/","title":{"rendered":"Difference between == and is operator in Python"},"content":{"rendered":"<p>The == operator compares the values of both the operands and checks for value equality. Whereas is operator checks whether both the operands ask an equivalent object or not.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\"># python3 code to  <br\/># illustrate the  <br\/># difference between <br\/># == and is operator <br\/># [] is an empty list <br\/>list1 = [] <br\/>list2 = [] <br\/>list3=list1 <br\/>  <br\/>if (list1 == list2): <br\/>    print(&quot;True&quot;) <br\/>else: <br\/>    print(&quot;False&quot;) <br\/>  <br\/>if (list1 is list2): <br\/>    print(&quot;True&quot;) <br\/>else: <br\/>    print(&quot;False&quot;) <br\/>  <br\/>if (list1 is list3): <br\/>    print(&quot;True&quot;) <br\/>else:     <br\/>    print(&quot;False&quot;) <\/code><\/pre> <\/div>\n<h3 id=\"output\"><strong>OUTPUT :<\/strong><\/h3>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">True<br\/>False<br\/>True<\/code><\/pre> <\/div>\n<ul>\n<li>Output of the primary if condition is \u201cTrue\u201d as both list 1 and list 2 are empty lists.<\/li>\n<li>Second if condition shows \u201cFalse\u201d because two empty lists are at different memory locations. Hence list 1 and list 2 ask different objects. we will check it with id () function in python which returns the \u201cidentity\u201d of an object.<\/li>\n<li>Output of the third if condition is \u201cTrue\u201d as both list 1 and list 3 are pointing to an equivalent object.<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">list1 = [] <br\/>list2 = [] <br\/>  <br\/>print(id(list1)) <br\/>print(id(list2)) <\/code><\/pre> <\/div>\n<h3 id=\"output-2\"><strong>OUTPUT :<\/strong><\/h3>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">139877155242696<br\/>139877155253640<\/code><\/pre> <\/div>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The == operator compares the values of both the operands and checks for value equality. Whereas is operator checks whether both the operands ask an equivalent object or not. OUTPUT : Output of the primary if condition is \u201cTrue\u201d as both list 1 and list 2 are empty lists. Second if condition shows \u201cFalse\u201d because [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":33508,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[86902,86901,86886],"class_list":["post-33498","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-coding","tag-conditions-methods-in-python","tag-different-operators-in-python","tag-operators-in-python"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/33498","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=33498"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/33498\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media\/33508"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=33498"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=33498"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=33498"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}