Social Media

12/23/2018

Identity and Membership Operators

 Identity operators:
Two operators comes under the category of identity operator.
1. is
2. is not


  • is operator: is operator is used to compare the memory locations. It tells whether both the reference variables pointing to same memory locations or not. It returns True if they are referring to same memory locations otherwise return False.

example
x=4
y=4
print(x is y)  # True

x=4

y=4.0
print(x is y) #False


  • is not operator: is  not operator gives the reverse result of is operator. You can apply is operator first and then you can reverse the result for is not operator.


Membership Operator:
Membership operator can only be applied on sequential data types eg. string, list, tuples etc.
There are two types of membership operators present in the python language.
1. in operator
2. not in operator


  • in operator: If specified value is present in the sequence it will return True otherwise it will return False.  See the examples given below


x="mayank"
"a" in x # True
"A" in x  # False

x=2,4,5,6 (tuple)

4 in x # True

x="mayank"

"may" in x # True

x=345

4 in x # error because x is not iterable here


  • not in operator: It gives the reverse result of in operator. You can perform in operation first and then reverse the result to get output of not in operator.


Be First to Post Comment !
Post a Comment