×

### Identity Operators in Python - Explained with examples

Identity operators in Python are used to determine if a value belongs to a specific class or type and are frequently utilized to figure out what kind of data a variable contains. Identity operators, as their name implies, are used to locate an object’s memory unit, particularly when two objects have the same name and can only be distinguished by their memory location.

Here at Ibmi Media, we shall look into Identity Operators in Python.

#### Different Types of Identity Operators in Python:

• Is operator.
• Is Not operator.

#### Python Identity operators with examples

There are two main identity operators, which are the "is" operator and the "is not" operator, that will be discussed here.

#### 1. Is Operator in Python

When two variables point to the same object or have the same value, the "is" operation returns true; otherwise, it returns false.

For example;

i. Taka a look at the below example

`x = 5y = 5print(x is y)Output:Truex = 5y = 10print(x is y)Output:False`

We are utilizing two variables, "x" and "y" in the preceding example of the "is" operator and giving them the same values, as x = 5 and y = 5 implies the same memory address or refers to the same object. In the second example, you can see that their values are 5 and 10, which are not the same, which is why the result is false.

ii. Another example is given below

Here,we are going to use if-else statements for better understanding the same variables such as x and y and assign them values to differentiate them from each other.

`x = 5y = 5if ( x is y ):print("x and y have same identity")else:print("x and y do not have same identity")Output:x and y have same identityx = 5y = 10if ( x is y ):print("x and y have same identity")else:print("x and y do not have same identity")Output:x and y do not have same identity`

#### 2. Is Not Operator in Python

Is Not operator works in the opposite direction to the operator. If two objects have different memory locations, this returns true. If two objects or variables have the same memory location, this returns False.

i. Take a look at this example

`x = 5y = 10print(x is not y)Output:Truex = 10y = 10print(x is not y)Output:False`

Here, the "is not" operator we are using two variables, "x" and "y" and assigning them different values, as x = 5 and y = 10 means they have different memory locations or are pointed to a different object. As their values are not the same, in this case, the output will be true, whereas you can see that in the second example, the value is 5 for each variable, which is the same that’s why the output will be false

ii. Another example is given here

Same as the above example of "is" operator we are going to use if-else statements for better understanding of the "is not" operator with the same variables such as x and y and assigning them values to differentiate the memory location or object of variables from each other.

`x = 5y = 5if ( x is not y ):print("x and y have same identity")else:print("x and y do not have same identity")Output:x and y do not have same identityx = 5y = 10if ( x is not y ):print("x and y have same identity")else:print("x and y do not have same identity")Output:x and y do not have same identity`

Here, as you can see, we used condition statements "if-else" with the "is not" operator to identify the variable’s memory location, whether they are pointing to the same object or not, or whether they are identical or not. In the above example if the condition is True, it returns the result both "x and y have the same identity" otherwise it returns "x and y do not have the same identity".

## Conclusion

This article covers identity operators in Python which are used to determine if a value belongs to a given class or type, and they are typically used to identify what sort of data a variable contains. In fact, Identity operators are used to find an object's memory unit, which is especially useful when two objects have the same name and can only be identified by their memory location.