Menu Close

Python Membership and Identity Operators

python membership and identity operators

In this tutorial, you will learn all about Python membership and identity operators with the help of examples.

In our previous Python tutorial, we have seen approx all the Python operators along with their examples. Before going through this article, you should have basic knowledge of the following Python topics.

Python Membership Operators

Membership operators in Python are used to check if a sequence present in the object or not.

Types of Python membership operators

There are two types of membership operators available in Python.

OperatorDescription
inReturns True if the specified sequence present in the object
not inReturns True if the specified sequence not present in the object

in operator

in operator returns True if the specified sequence present in the object, otherwise it returns False.

Python Membership in Operator Examples


Example 1:

fruit_name = input("Please enter any fruit name:- ")
fruits = ['Banana', 'Apple', 'Orange', 'Grapse', 'Coconut', 'Pineapple']

if fruit_name in fruits:
    print(f"Your name {fruit_name} present in our Dictionary")
else:
    print("Please, enter another fruit name")

Output

Please enter any fruit name:- Banana
Your name Banana present in our Dictionary

Example 2:

fruit_name = input("Please enter any fruit name:- ")
fruits = ['Banana', 'Apple', 'Orange', 'Grapse', 'Coconut']

for fruit in fruits:
    if fruit in fruit_name.capitalize().strip().split():
        print(f"Your name {fruit_name} present in our Dictionary")
        break  

Output

Please enter any fruit name:- Orange
Your name Orange present in our Dictionary

not in operator

Python membership not in operator is used to return True if the specified sequence not present in the object otherwise return False.

Python membership not in operator examples


Example 1:

fruit_name = input("Please enter any fruit name:- ")
fruits = ['Banana', 'Apple', 'Orange', 'Grapse', 'Coconut', 'Pineapple', 'Papaya ', 'Mango']

if fruit_name not in fruits:
    print(f"Your name {fruit_name} not present in our Dictionary")
else:
    print("Success..")

Output

Please enter any fruit name:- Coconut
Success..

Example 2:

fruit_name = input("Please enter any fruit name:- ")
fruits = ['Banana', 'Apple', 'Orange', 'Grapse', 'Coconut', 'Pineapple', 'Papaya', 'Mango']

for fruit in fruits:
    if fruit not in fruit_name.capitalize().strip().split():
        print(f"Your name {fruit_name} not present in our Dictionary")
        	
    else:
        print("Success..")

Output

Please enter any fruit name:- Apple
Your name Apple not present in our Dictionary
Success..
Your name Apple not present in our Dictionary
Your name Apple not present in our Dictionary
Your name Apple not present in our Dictionary
Your name Apple not present in our Dictionary
Your name Apple not present in our Dictionary
Your name Apple not present in our Dictionary

Python Identity Operators

identity operators in Python are used to compare the objects. not if they are equal, but they are actually the same objects with the same memory location.

Type of identity operators

OperatorDescription
isReturn True if both variables with same objects.
is notReturn True if both variables, not the same objects.

is operator

is identity operator is used to returning True if both variables with the same objects.

Python is identity operator examples


Example 1:

a = ["Coding"]
b = ["Coding"]

c = a
if c is a:
    print("Success..")
    
#Output
# Output would be Success.. because both a and c is the same object.

Example 2:

if a is b:
    print("Success..")
else:
    print("Fail..")
    
#Output
# Output would be Fail.. because both a and b is not the same object.

is not

is not operator is used to return True if both the variable is not the same objects.

Python is not identity operator example


Example 1:

a = ["Coding"]
b = ["Coding"]

c = a
if c is not a:
    print("True")
else:
    print("False")
    
#Output
# Output would be False because both a and c is the same object.

Example 2:

if a is not b:
    print("True")
else:
    print("False")
    
#Output
# Output would be True because both a and b is not the same object.

Conclusion

That’s all about Python membership and identity operator. These operators are going to be very helpful when you are going to implement logic in your Python Project or Python code.

Operators in Python are most powerful to implement logic in the Python application or code. When you will work with any Python application or project in the future, Python operators are going to be very helpful.

If you like this article, please share and keep visiting for further Python tutorials.

~ Thanks for reading

Python Property Decorator - @property
Python Calendar Module

Related Posts