For enquiries call:

Phone

+1-469-442-0620

April flash sale-mobile

HomeBlogProgrammingWhat are Membership Operators in Python

What are Membership Operators in Python

Published
05th Sep, 2023
Views
view count loader
Read it in
5 Mins
In this article
    What are Membership Operators in Python

    The membership operators are, as the name explains, used to verify a value membership. The operators are used to figure out if there are two forms of value as a part of a sequence, such as string or list membership operators:in and not in. 

    To check whether two variables point to the same location or not, identity operators are used. Two forms of identity operators are is and is not. 

    In general, operators are used to work on Python values and variables. These are regular symbols used for logical and arithmetical operations.

    Identity Operators: 

    The Python identity operatorsare used to figure out whether a value is of a certain class or type. Typically, they are used for evaluating the type of information in a given variable. For example, to make sure you work with the variable type, you can combine identity operators with the built-in type() function. Python’s two identity operators are (is, is not).

    Identity operators in python

    is:When evaluated, is Operator in Python returns true if the variables point to the same variable on either side of the operator and return false otherwise. 

    Example 1: 

    x = 5 
    if (type(x) is int): 
    print(“true”) 
    else: 
    print(“false”) 

    Output: 

    true 

    Example 2: 

    x =6 
    if(type(x) is int): 
    print("true") 
    else: 
    print("false") 

    Output: 

    true 

    Example 3: 

    list1 = [9, 8, 7, ‘i’] 
    list2 = list1 
    if list1 is list2: 
    print(“True”) 
    else: 
    print(“False”) 

    Output: 

    True 

    The output here is true because list2 also refers to a list1referenced by the variable list1. We may also use the isoperator to verify if two python objects of the same type have other functions such as the type() function. 

    is not:The operator is not is the exact opposite of ‘is operator’in Python. When evaluated, the operator returns false if the variables point to the same object on either side of the operator and return trueotherwise. 

    Example 1: 

    x = 5.2 
    if (type(x) is not int): 
    print(“true”) 
    else: 
    print(“false”) 

    Output: 

    true 

    Example 2: 

    x =7.2 
    if(type(x) is not int): 
    print("true") 
    else: 
    print("false") 

    Output: 

    true 

    Example 3: 

    new_list = [9,8,7, 'i'] 
    new_tuple = (9,8,7, 'i') 
    type(my_new_tuple) 
    if type(my_new_list) is not type(my_new_tuple): 
        print('True!, They are not of the same type') 
    else: 
        print("False, They are the same type") 

    Output: 

    True!, They are not of the same type 

    Since the tuple and the list are not the same and the operator does not check their inequality, it returns True. 

    Let us see a combined example of “is” and “is not”. 

    Example: 

    x = "identity operator" 
    if (type(x) is str):  
        print ("This is a string")  
    else:  
        print ("This is not a string")  
    y=987 
    if (type(y) is not str):  
        print ("This is a string")  
    else:  
        print ("This is not a string") 

    Output: 

    This is a string 
    This is not string 

    Python Example

    Declare the value for variable x and y. Use the operator “is” to check if the value of x is the same as y. Next, we use the operator “is not” to check if the value of x is not the same as y. 

    Example 2: 

    a1 = 10 
    b1 = 10 
    a2 = ‘PythonProgramming 
    b2 = ‘Programming’ 
    a3 = [1,2,6] 
    b3 = [1,2,3] 
    print(a1 is not b1) 
    print(a2 is b2) 
    print(a3 is b3) 

    Output: 

    True 
    False 
    False 

    Top Cities Where KnowledgeHut Conduct Python Certification Course Online

    Python Course in BangalorePython Course in ChennaiPython Course in Singapore
    Python Course in DelhiPython Course in DubaiPython Course in Indore
    Python Course in PunePython Course in BerlinPython Course in Trivandrum
    Python Course in NashikPython Course in MumbaiPython Certification in Melbourne
    Python Course in HyderabadPython Course in KolkataPython Course in Noida

    Membership Operators: 

    These operators evaluate membership in lists, sequences, or tuples in one sequence. In Python, there are two membership operators. (in, not in). It displays the result in the given sequence or string centered on the present variable.

    Membership Operators

    Membership Operators as a whole comprise a number of different operators. 

    in Operator:It tests whether or not the value is present in the data sequence. It analyses the true value if the component is in the series and the false value if the component is not present. 

    Example 1: 

    list1 = ['Aman', 'Bhuvan', 'Ashok', 'Vijay', 'Anil'] 
    if 'Aman' in list1: print('Name Aman exists in list1') 

    Output: 

    Name Aman exists in list1 

    Example 2: 

    list1=[1,2,4,5] 
    list2=[6,7,9] 
    for item in list1: 
    if item in list2: 
    print("overlapping")     
    else: 
    print("not overlapping") 

    Output: 

    not overlapping 

    Example 3: 

    new_list = [1,2,3,'a'] 
    # loop around the list 
    for i in new_list: 
        print(i) 

    Output: 

    1 
    2 
    3 
    a 

    The in operator allows the variable Ito refer to every element in the list iterated by the for loop. You have to think that the operator is used to check whether or not an element is present in a sequence, but what exactly happens? Well, when used in various ways, in a loop and in a conditional statement like if statement, the operator behaves differently. 

    Let us remove the in operator in the example and modify it. 

    Example: 

    def overlapping(list1,list2): 
    c=0 
    d=0 
    for i in list1: 
    c+=1 
    for i in list2: 
    d+=1 
    for i in range(0,c): 
    for j in range(0,d): 
    if(list1[i]==list2[j]): 
    return1 
    return 0 
    list1=[1,2,3,4,5] 
    list2=[6,7,8,9] 
    if(overlapping(list1,list2)): 
    print("overlapping") 
    else:
    Profile

    KnowledgeHut .

    Author

    KnowledgeHut is an outcome-focused global ed-tech company. We help organizations and professionals unlock excellence through skills development. We offer training solutions under the people and process, data science, full-stack development, cybersecurity, future technologies and digital transformation verticals.

    Share This Article
    Ready to Master the Skills that Drive Your Career?

    Avail your free 1:1 mentorship session.

    Select
    Your Message (Optional)

    Upcoming Programming Batches & Dates

    NameDateFeeKnow more
    Course advisor icon
    Course Advisor
    Whatsapp/Chat icon