{"id":6992,"date":"2024-04-11T12:01:13","date_gmt":"2024-04-11T12:01:13","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=6992"},"modified":"2024-04-11T12:01:13","modified_gmt":"2024-04-11T12:01:13","slug":"python-membership-operator","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/python-membership-operator\/","title":{"rendered":"Python Membership Operator"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#what-is-a-python-membership-operator\">What is a Python Membership Operator?<\/a><\/li><li ><a href=\"#what-is-in-operator\">What is In operator?<\/a><\/li><li ><a href=\"#what-is-not-in-the-operator\">What is NOT in the operator?<\/a><\/li><li ><a href=\"#what-is-an-identity-operator\">What is an Identity Operator?<\/a><\/li><li ><a href=\"#what-is-operator\">What &#8216;is&#8217; operator<\/a><\/li><li ><a href=\"#what-is-is-not-operator\">What is &#8216;Is not&#8217; operator<\/a><\/li><li ><a href=\"#conclusion\">Conclusion<\/a><\/li><li ><a href=\"#python-membership-operator-fa-qs\">Python Membership Operator- FAQs<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">The membership operators in<a href=\"https:\/\/www.skillvertex.com\/blog\/python-type-casting\/\" data-type=\"post\" data-id=\"6943\"> Python<\/a> will allow us to know if an item is available in the given container-type object. Let us look at this article to learn more about Python Membership Operator<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-a-python-membership-operator\">What is a Python Membership Operator?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/www.skillvertex.com\/blog\/assignment-operators-in-python\/\" data-type=\"post\" data-id=\"6970\">Python<\/a> will provide two <strong>membership operators<\/strong> for checking the membership value. It will check for membership in a <a href=\"https:\/\/www.skillvertex.com\/blog\/what-does-0-mean-in-this-python-string\/\" data-type=\"post\" data-id=\"3237\">string<\/a>, list, or tuples sequence.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-in-operator\">What is <strong>In operato<\/strong>r?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The in-operator will monitor whether a <a href=\"https:\/\/www.skillvertex.com\/blog\/character-arithmetic-in-c-and-c\/\" data-type=\"post\" data-id=\"1876\">character<\/a> or substring will be present in the sequence.  So, this operator will be considered true only if it can find the specified element, or else it will be false.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>'S' in 'Skillvertex'   # Checking 'S' in String\nTrue\n's' in 'Skillvertex'   #Checking 's' in string since Python is case-sensitive,returns False\nFalse\n'Skill' in &#91;'Skill','Vertex']   #Checking 'Skill' in list of strings\nTrue\n10 in &#91;10000,1000,100,10]        #Checking 10 in list of integers\nTrue\ndict1={1:'Skill,2:'For',3:'Skill'}     # Checking 3 in keys of dictionary\n3 in dict1\nTrue<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Python Program to  find the common member is provided below<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Membership operator example\nfruits = &#91;'apple', 'banana', 'orange', 'grape']\n\n# Using 'in' operator\nsearch_fruit = 'banana'\nif search_fruit in fruits:\n    print(f\"{search_fruit} is present in the list.\")\nelse:\n    print(f\"{search_fruit} is not present in the list.\")\n\n# Using 'not in' operator\nsearch_fruit = 'kiwi'\nif search_fruit not in fruits:\n    print(f\"{search_fruit} is not present in the list.\")\nelse:\n    print(f\"{search_fruit} is present in the list.\")\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>banana is present in the list.\nkiwi is not present in the list.<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now, the Python<a href=\"https:\/\/www.skillvertex.com\/blog\/python-program-to-print-hello-world\/\" data-type=\"post\" data-id=\"6880\"> Program <\/a>below will use the same example without an operator.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Membership operator example without using <a href=\"https:\/\/www.skillvertex.com\/blog\/python-comparison-operators\/\" data-type=\"post\" data-id=\"6966\">operators<\/a>\nfruits = &#91;'apple', 'banana', 'orange', 'grape']\n\n# Without using 'in' operator\nsearch_fruit = 'banana'\nfound = False\nfor fruit in fruits:\n    if fruit == search_fruit:\n        found = True\n        break\nif found:\n    print(f\"{search_fruit} is present in the list.\")\nelse:\n    print(f\"{search_fruit} is not present in the list.\")\n\n# Without using 'not in' operator\nsearch_fruit = 'kiwi'\nnot_found = True\nfor fruit in fruits:\n    if fruit == search_fruit:\n    not_found = False\n        break\nif not_found:\n    print(f\"{search_fruit} is not present in the list.\")\nelse:\n    print(f\"{search_fruit} is present in the list.\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>banana is present in the list.\nkiwi is not present in the list.\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The execution speed of the in-operator will mainly depend on the target object type. The average time complexity of the in operator for the list is 0(n). Hence, it will get slow due to the number of elements increasing.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Moreover, the average time complexity of the in-operator for sets is  0(1). Hence, it won&#8217;t rely on the number of elements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Whereas in dictionaries, the keys in the dictionary are unique values such as set. It is possible to repeat the value as similar to the list. So, the   &#8216;in&#8217; for values () will be executed as similar to the lists.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-not-in-the-operator\">What is <strong><strong>NOT in the operator<\/strong><\/strong>?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This <strong>NOT in operator <\/strong>can run the program to get the true value if it can&#8217;t find the variable in the specified sequence or else, it will be declared as false.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Take a look at the Python Program to illustrate the not-in operator.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Membership operator 'not in' example with output\nfruits = &#91;'apple', 'banana', 'orange', 'grape']\n\n# Using 'not in' operator\nsearch_fruit = 'kiwi'\nif search_fruit not in fruits:\n    print(f\"{search_fruit} is not present in the list.\")\nelse:\n    print(f\"{search_fruit} is present in the list.\")\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kiwi is not present in the list.\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This example mentions that the not-in operator will be used to evaluate the value such as kiwi is not present in the list of fruits.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-an-identity-operator\">What is an Identity Operator?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The<strong> identity operator<\/strong> functions to compare the objects as both the objects are the same data type and will share the same memory location.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-operator\">What <strong>&#8216;is&#8217; operator<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This <strong>&#8216;is&#8217; operator <\/strong>will be used to check if the value can come true, only if the <a href=\"https:\/\/www.skillvertex.com\/blog\/python-variables\/\" data-type=\"post\" data-id=\"6932\">variable<\/a> on either side of the operator points to the same object or else it will be false.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Using 'is' operator for membership-like testing\nfruits = &#91;'apple', 'banana', 'orange', 'grape']\n\n# Object to test for membership\nsearch_fruit = 'banana'\n\n# Check using 'is' operator\nis_member = any(fruit is search_fruit for fruit in fruits)\n\nif is_member:\n    print(f\"{search_fruit} is in the list.\")\nelse:\n    print(f\"{search_fruit} is not in the list.\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>banana is in the list.\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-is-not-operator\">What is &#8216;Is not&#8217; operator<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This &#8216;<strong>Is not&#8217; operator <\/strong>will check if the values come true,  only if both variables are not in the same <a href=\"https:\/\/www.skillvertex.com\/blog\/which-is-not-an-ado-net-dataadapter-object\/\" data-type=\"post\" data-id=\"2225\">object<\/a>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Using 'is not' operator for membership-like testing\nfruits = &#91;'apple', 'banana', 'orange', 'grape']\n\n# Object to test for non-membership\nsearch_fruit = 'kiwi'\n\n# Check using 'is not' operator\nis_not_member = all(fruit is not search_fruit for fruit in fruits)\n\nif is_not_member:\n    print(f\"{search_fruit} is not in the list.\")\nelse:\n    print(f\"{search_fruit} is in the list.\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kiwi is not in the list.\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To conclude, in Python membership operators (in and not in)  will allow us to check if something is part of a group,  such as looking if a specific word is in a list.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"> If it&#8217;s on the list, it will give us True, otherwise False. On the other hand,<code> <\/code>not in does the opposite; it gives us True if the thing is not in the group, and False.  This article has also discussed the different operators of the membership operator. Those operators include &#8216;<strong>In&#8217; <\/strong>operator, <strong>&#8216;not in&#8217;<\/strong> operator, and several other identity operators.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"python-membership-operator-fa-qs\">Python Membership Operator- FAQs<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1707478574822\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. What is membership testing in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans.  Membership testing is used to check if the collection of list  has a specific item such as list , set or dictionary.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1707478586088\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. <strong> How many operators are in Python?<\/strong><\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. There are seven different operators in Python. Those operators are arithmetic, assignment, comparison, logical, identity, membership, and boolean operators. <\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1707478601486\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. What is a tuple example?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. The Tuplr is referred to as an ordered as the uniqueness in order will define the tuple. Examples of Tuple  are  a<sub>1<\/sub>\u00a0equals b<sub>1<\/sub>, a<sub>2<\/sub>\u00a0equals b<sub>2<\/sub>, a<sub>3<\/sub>\u00a0equals b<sub>3<\/sub>\u00a0<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>The membership operators in Python will allow us to know if an item is available in the given container-type object. Let us look at this article to learn more about Python Membership Operator What is a Python Membership Operator? Python will provide two membership operators for checking the membership value. It will check for membership &#8230; <a title=\"Python Membership Operator\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/python-membership-operator\/\" aria-label=\"More on Python Membership Operator\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":6993,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[864],"tags":[895],"class_list":["post-6992","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorial","tag-python-membership-operator","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/6992","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=6992"}],"version-history":[{"count":8,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/6992\/revisions"}],"predecessor-version":[{"id":8905,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/6992\/revisions\/8905"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/6993"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=6992"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=6992"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=6992"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}