IDENTITY OPERATORS IN PYTHON
Now, we are going to learn about identity operators in Python. As the name suggests, identity operators are used for checking the identity of the object. Here, the identity operators that we have here are is and is not. We would soon have a look at a simple example, which demonstrates the identity operators in Python, but first of all, here is the table, with the identity operators, their descriptions, and some example. Here, the identity can be determined using the id function, and here it is the address of the object in the memory.
Operator | Description | Example |
is | This operator checks if the two objects have the same id | a = 4.0 b = 4.0 print(a is b) print(id(a)) #this prints the Identity of the objects. print(id(b)) so, the output of the above code would be True, as the id of both the objects a and b are same. |
is not | This operator checks if the two objects do not have the same ids | a = 4.0 b = ‘4.0’ print(a is not b) so, this will print True, as the id of both the objects a and b are not same. |
Now, Let’s have a look at a simple example, which demonstrates about the identity operators in Python.
As you can see, in the above program, we have two strings, ‘GyaniPandit’, and ‘Python’. In the below lines, we have printed the id of both objects, and then we are using the is, and is not operators, for both the objects.
Now, the output of name1 is name2, is True, if and only if they are the same objects, which is not True in this case. Also, you can understand from this, that what the is not operator seems to be doing. It simply yields the inverse truth value.
So, we have now discussed many different operators in Python. Basically, we have these different operators, for performing different operations on the data. We would use these different operators as and when required in our python programs.