In the previous article we have gone through the logical operator concepts in Python, in this article will look at the identity comparison operators in Python. Identity or identity operators compare the location of two objects in memory and their relation with each other.
Utilizing Identity Comparison Operators in Python
In Python identity operators are used to determine whether a value is of a certain class or type. They are usually used to determine the type of data a certain variable contains. For example, you can combine the identity operators with the built-in type( ) function to ensure that you are working with the specific variable type. The identity of an object is determined using the function id( ). In other words, objects are identical if they have the same identifier at runtime.
Operator behavior is
and is not
cannot be customized. Moreover, they can be applied to any two objects at the same time and never throw an exception.
There are different identity operators such as:
OPERATOR | DESCRIPTION | EXAMPLE |
---|---|---|
is | It is considered true if the variables on the left/right of the operator point to the same object, and false otherwise. | x is y, here is results in 1 if id (x) equals id (y). |
is not | Considered false if the variables on the left/right of the operator point to the same object, and true otherwise . | x is not y, here is not results in 1 if id (x) is not equal to id (y). |
‘is’ operator – Evaluates to true if the variables on either side of the operator point to the same object and false otherwise.
# Python program to illustrate the use
# of 'is' identity operator
x = 5
if (type(x) is int):
print("true")
else:
print("false")
Output:
true
x = ["apple", "banana"]
y = ["apple", "banana"]
z = x
print(x is z)
# returns True because z is the same object as x
print(x is y)
# returns False because x is not the same object as y, even if they have the same content
print(x == y)
# to demonstrate the difference betweeen "is" and "==": this comparison returns True because x is equal to y
Output:
True
False
True
‘is not’ operator – Evaluates to false if the variables on either side of the operator point to the same object and true otherwise.
# Python program to illustrate the
# use of 'is not' identity operator
x = 5.2
if (type(x) is not int):
print("true")
else:
print("false")
Output:
true
x = ["apple", "banana"]
y = ["apple", "banana"]
z = x
print(x is not z)
# returns False because z is the same object as x
print(x is not y)
# returns True because x is not the same object as y, even if they have the same content
print(x != y)
# to demonstrate the difference betweeen "is not" and "!=": this comparison returns False because x is equal to y
Output:
False
True
False
Python id() Function
An identifier (object) returns the “identification number” of an object, which is unique and immutable, but in a life cycle that does not overlap, the same identifier value may appear. The object referred to here must specifically refer to the composite type of objects (such as class, list, etc.). For strings, integers, and other types, the variable identifier changes with the value. The id value of an object represents its memory address. The is and is not functions automatically make use of this particular function for comparing the values.
id(object)
is() Comparison vs =
Sometimes it is common to mistake one function for another, the is( ) function works differently from the equal ‘=’ to operator as it only compares the memory address whereas the ‘=’ compares the values for equality. Objects with the same value can still be stored at various different memory addresses while in the Python program.
a = [1, 2, 3]
b = a.copy()
print(a == b)
print(a is b)
print(id(a))
print(id(b))
Output:
True
False
140417849799616
140417849798976
a
and b
are stored at variant memory locations, so a is b
will no longer return ‘True’. However, a == b
returns ‘True’ because both objects have the same value.
We have gone through the logical operators, in the next article we will explore Membership Test Operators.