{"id":3125,"date":"2022-11-04T20:11:15","date_gmt":"2022-11-04T20:11:15","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=3125"},"modified":"2022-11-04T20:11:17","modified_gmt":"2022-11-04T20:11:17","slug":"membership-operators-in-python","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/membership-operators-in-python\/","title":{"rendered":"MEMBERSHIP OPERATORS IN PYTHON"},"content":{"rendered":"\n<p class=\"has-text-align-center\">MEMBERSHIP OPERATORS IN PYTHON<\/p>\n\n\n\n<p>Now, we are going to learn about the membership operators in Python. Basically, as the name suggests, the membership operators are used to check whether or not some value is a member of some sequence. This is like we are checking whether there is &#8216;P&#8217; in Python.<\/p>\n\n\n\n<p>As the membership operators, we have the in and not in operators. Here is the table, with the operators, their descriptions, and example. Soon, we would also discuss a simple program, which demonstrates the membership operators.<\/p>\n\n\n\n<p>For the example in the table, we consider the list games = [\u201cCricket\u201d, \u201cHockey\u201d, \u201cKabaddi\u201d]. This is a list, and list is nothing but a collection of data, about which we are going to learn soon.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>Operator<\/td><td>Description<\/td><td>Example<\/td><\/tr><tr><td>in<\/td><td>This operator checks whether the specified value is present in a sequence.<\/td><td>print(\u201cCricket\u201d in games)<br>this will print True, as a string named \u201cCricket\u201d is present in the list games.<\/td><\/tr><tr><td>not in<\/td><td>This operator checks whether the specified value is not present in a sequence.<\/td><td>print(\u201cApple\u201d not in games)<br>this will print True, as the string named \u201cApple\u201d is not present in the list games.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Now, let\u2019s have a look at a simple program, which demonstrates the above-discussed membership operators.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"1076\" height=\"129\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/MEMBERSHIP-OPERATORS-IN-PYTHON.jpg\" alt=\"\" class=\"wp-image-3109\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/MEMBERSHIP-OPERATORS-IN-PYTHON.jpg 1076w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/MEMBERSHIP-OPERATORS-IN-PYTHON-300x36.jpg 300w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/MEMBERSHIP-OPERATORS-IN-PYTHON-1024x123.jpg 1024w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/MEMBERSHIP-OPERATORS-IN-PYTHON-768x92.jpg 768w\" sizes=\"auto, (max-width: 1076px) 100vw, 1076px\" \/><\/figure>\n<\/div>\n\n\n<p>As you can see, we have a string, and then we are checking for the membership of some element in the sequence like we are first checking if &#8216;P&#8217; is there in the string, which comes out to be True, and then we are also checking if &#8216;G&#8217; is not there in string, which again comes out to be True. So, have a look at the output of the given program.<\/p>\n\n\n\n<p>True<br>True<\/p>\n\n\n\n<p>As you can see, in both cases, we got True, as our output. There would be many situations in our programs, where we would use the membership operators. We would use the membership operators as and when required, and you can try them for other things as well, like the list, tuples, etc., as we would learn them later.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MEMBERSHIP OPERATORS IN PYTHON Now, we are going to learn about the membership operators in Python. Basically, as the name suggests, the membership operators are used to check whether or not some value is a member of some sequence. This is like we are checking whether there is &#8216;P&#8217; in Python. As the membership operators, [&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-3125","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\/3125","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=3125"}],"version-history":[{"count":2,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3125\/revisions"}],"predecessor-version":[{"id":3964,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3125\/revisions\/3964"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=3125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=3125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=3125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}