CSDT BLOG

DISCOVER COLLECTIONS AND BLOGS THAT MATCH YOUR INTERESTS.




Share ⇓




What is identity Operator in Python language?

Bookmark

What is identity Operator in Python language?

Identity Operators in Python

Other than membership operators, there exists another type of operators in Python which are known ad Identity Operators


In Python, identity operators are used to check if a particular value is of a certain class or type. In most cases, identity operators are used to define the type of data a certain variable contains. There are two main types of identity operators in Python.


  • Is Operator: When evaluated, the Is Operator in Python returns true if the variables on either side of the operator are pointing to the same variable and otherwise returns false. To understand this better, take a look at the example below.


# Python program to illustrate the use of 'is' identity operator 


x = 26

if (type(x) is int): 

    print ("true") 

else: 

    print ("false")



Output:

True


//another Examples

x = [“Patna”, “Gaya”]

print(“Gaya” is x)

 

# returns True because a sequence with the value “Gaya” is in the list


Output:

True


Is Not Operator

The is not operator in Python is the exact opposite of the is Operator. When evaluated, the operator returns false if the variables on either sides of the operator point to the same object and otherwise returns false. To understand this better, take a look at the example below.


# Python program to illustrate the 

# use of 'is not' identity operator 


x = 17.2

if (type(x) is not int): 

    print ("true") 

else: 

    print ("false")


Output:

True



#another example of this operator.


x = [“CSDT”, “Patna”]

 

print(“Solution” not in x)

 

# returns True because a sequence with the value “Solution” is not in the list


Output:

True


0

Our Recent Coment