Python Programming - Identity Operators

You can learn about Operators in Python Programs with Outputs helped you to understand the language better.

Python Programming – Identity Operators

Identity operators compare the memory locations of two objects. There are two Identity operators explained below:

is Operator
Evaluates to true if the variables on either side of the operator point to the same object and false other-wise. For example, x is y; here result is 1 if is(x) equals is(y).

>> X = y = [ 1 , 2 , 3 ]
>>> z = [1 , 2 , 3 ]
>>> X == y
True
>>> X = = z
True
>>> X is y
True
>>> X is z
False
>>> X = [ 1 , 2 , 3 ]
>>> y = [ 2 , 4 ]
>>> X is not y
True

is not Operator

Evaluates to false if the variables on either side of the operator point to the same object and true other-wise. For example, x is not y; here, is not results in 1, it is(x) is not equal to is(y).

Example 19
Demo of Identity operators.

# Demo of Identity operators
>>> s1=’xyz’
>>> s2=input(“Enter a string: “);
Enter a string: abc
>>> s1==s2
False
>>> s1 is s2
False
>>> s3 = ‘xyz’
>>> s1 is s3
True

Let’s Try

Write the output of following code :

# Working with Identity operators
>>> s1=’xyz’
>>> s2=input(“Enter a string: “);
Enter a string: xyz
>>> s1==s2
>>> s1 is s2
>>> s3 = ‘xyz’
>>> s1 is s3

Write the output of following code :

>> x = [2, 4, 6]
>>> y = [1, 2]
>>> x is not y

Leave a Reply

Your email address will not be published. Required fields are marked *