Skip to content

Python Identity Operators

Unveiling Identity Operators in Python

Identity operators in Python are essential tools for comparing the memory addresses of objects. These operators, is and is not, evaluate whether two variables reference the same object or not. Understanding identity operators is crucial for dealing with object references and managing memory effectively in Python programming. In this comprehensive guide, we’ll delve into the world of identity operators, their syntax, and their applications.

The following table lists the identity operators in Python:

OperatorDescriptionExample
isisReturns TrueTrue if both variables are the same objectx is yx is y
is notis notReturns TrueTrue if both variables are not the same objectx is not yx is not y

is Operator

isis Operator

The isis operator returns TrueTrue if both variables are the same object. It returns TrueTrue if both variables point to the same memory location otherwise FalseFalse. The following example demonstrates how to use the isis operator in Python:

operators.py
# is operator
x = 10
y = 10
z = x is y
print(z)
operators.py
# is operator
x = 10
y = 10
z = x is y
print(z)

Output:

command
C:\Users\Your Name> python operators.py
True
command
C:\Users\Your Name> python operators.py
True

In the above example, we have used the isis operator to check if the value of xx is the same as the value of yy. Since the value of xx is the same as the value of yy, the condition becomes TrueTrue. The result of the isis operator is then assigned to the variable zz. The value of zz is then printed to the console.

is not Operator

is notis not Operator

The is notis not operator returns TrueTrue if both variables are not the same object. It returns TrueTrue if both variables point to different memory locations otherwise FalseFalse. The following example demonstrates how to use the is notis not operator in Python:

operators.py
# is not operator
x = 10
y = 5
z = x is not y
print(z)
operators.py
# is not operator
x = 10
y = 5
z = x is not y
print(z)

Output:

command
C:\Users\Your Name> python operators.py
True
command
C:\Users\Your Name> python operators.py
True

In the above example, we have used the is notis not operator to check if the value of xx is not the same as the value of yy. Since the value of xx is not the same as the value of yy, the condition becomes TrueTrue. The result of the is notis not operator is then assigned to the variable zz. The value of zz is then printed to the console.

Identity Operators with Lists

The identity operators can be used with lists. The following example demonstrates how to use the identity operators with lists in Python:

operators.py
# Identity operators with lists
x = [1, 2, 3, 4, 5]
y = 10
z = y is x
t = y is not x
print(z)
print(t)
operators.py
# Identity operators with lists
x = [1, 2, 3, 4, 5]
y = 10
z = y is x
t = y is not x
print(z)
print(t)

Output:

command
C:\Users\Your Name> python operators.py
False
True
command
C:\Users\Your Name> python operators.py
False
True

In the above example, we have used the isis operator to check if the value of xx is the same as the value of yy. Since the value of xx is not the same as the value of yy, the condition becomes FalseFalse. The result of the isis operator is then assigned to the variable zz. The value of zz is then printed to the console.

In the above example, we have used the is notis not operator to check if the value of xx is not the same as the value of yy. Since the value of xx is not the same as the value of yy, the condition becomes TrueTrue. The result of the is notis not operator is then assigned to the variable tt. The value of tt is then printed to the console.

Another example of using the identity operators with lists is given below:

operators.py
# Identity operators with lists
x = [1, 2, 3, 4, 5]
y = [1, 2, 3, 4, 5]
z = x is y
t = x is not y
print(z)
print(t)
# After changing the value of y
y = x
y[0] = 10
z = x is y
t = x is not y
print(z)
print(t)
operators.py
# Identity operators with lists
x = [1, 2, 3, 4, 5]
y = [1, 2, 3, 4, 5]
z = x is y
t = x is not y
print(z)
print(t)
# After changing the value of y
y = x
y[0] = 10
z = x is y
t = x is not y
print(z)
print(t)

Output:

command
C:\Users\Your Name> python operators.py
False
True
True
False
command
C:\Users\Your Name> python operators.py
False
True
True
False

In the above example, we have used the isis operator to check if the value of xx is the same as the value of yy. Since the value of xx is not the same as the value of yy, the condition becomes FalseFalse. The result of the isis operator is then assigned to the variable zz. The value of zz is then printed to the console.

In the above example, we have used the is notis not operator to check if the value of xx is not the same as the value of yy. Since the value of xx is not the same as the value of yy, the condition becomes TrueTrue. The result of the is notis not operator is then assigned to the variable tt. The value of tt is then printed to the console.

In the above example, we have assigned the value of xx to yy. Since both variables point to the same object, the isis operator returns TrueTrue and the is notis not operator returns FalseFalse.

Identity Operators with Strings

The identity operators can be used with strings. The following example demonstrates how to use the identity operators with strings in Python:

operators.py
# Identity operators with strings
x = "Hello"
y = "Hello"
z = x is y
t = x is not y
print(z)
print(t)
# After changing the value of y
y = "World"
z = x is y
t = x is not y
print(z)
print(t)
operators.py
# Identity operators with strings
x = "Hello"
y = "Hello"
z = x is y
t = x is not y
print(z)
print(t)
# After changing the value of y
y = "World"
z = x is y
t = x is not y
print(z)
print(t)

Output:

command
C:\Users\Your Name> python operators.py
True
False
False
True
command
C:\Users\Your Name> python operators.py
True
False
False
True

In the above example, we have used the isis operator to check if the value of xx is the same as the value of yy. Since the value of xx is the same as the value of yy, the condition becomes TrueTrue. The result of the isis operator is then assigned to the variable zz. The value of zz is then printed to the console.

In the above example, we have used the is notis not operator to check if the value of xx is not the same as the value of yy. Since the value of xx is the same as the value of yy, the condition becomes FalseFalse. The result of the is notis not operator is then assigned to the variable tt. The value of tt is then printed to the console.

In the above example, we have assigned the value of xx to yy. Since both variables point to the same object, the isis operator returns TrueTrue and the is notis not operator returns FalseFalse.

Comparison Operators vs Identity Operators

The comparison operators and identity operators are different. The comparison operators compare the values of two objects. The identity operators compare the memory locations of two objects. The following example demonstrates the difference between the comparison operators and identity operators in Python:

operators.py
# Comparison operators vs Identity operators
x = [1, 2, 3, 4, 5]
y = [1, 2, 3, 4, 5]
z = x == y
t = x is y
print(z)
print(t)
operators.py
# Comparison operators vs Identity operators
x = [1, 2, 3, 4, 5]
y = [1, 2, 3, 4, 5]
z = x == y
t = x is y
print(z)
print(t)

Output:

command
C:\Users\Your Name> python operators.py
True
False
command
C:\Users\Your Name> python operators.py
True
False

In the above example, we have used the ==== operator to check if the value of xx is the same as the value of yy. Since the value of xx is the same as the value of yy, the condition becomes TrueTrue. The result of the ==== operator is then assigned to the variable zz. The value of zz is then printed to the console.

In the above example, we have used the isis operator to check if the value of xx is the same as the value of yy. Since the value of xx is not the same as the value of yy, the condition becomes FalseFalse. The result of the isis operator is then assigned to the variable tt. The value of tt is then printed to the console.

Conditional Statements with Identity Operators

The identity operators can be used in conditional statements. The following example demonstrates how to use the identity operators in conditional statements in Python:

operators.py
# Conditional statements with identity operators
x = 10
y = 5
if x is y:
    print("x is y")
else:
    print("x is not y")
operators.py
# Conditional statements with identity operators
x = 10
y = 5
if x is y:
    print("x is y")
else:
    print("x is not y")

Output:

command
C:\Users\Your Name> python operators.py
x is not y
command
C:\Users\Your Name> python operators.py
x is not y

In the above example, we have used the isis operator in the conditional statement. Since the value of xx is not the same as the value of yy, the condition becomes FalseFalse. The elseelse block is executed in this case.

Conclusion

Identity operators in Python provide a powerful mechanism for comparing object identity and avoiding unintended side effects in your code. Whether you’re checking for None, ensuring variables reference the same object, or managing mutable objects, is and is not offer a precise and efficient way to handle object identity.

As you progress in your Python programming journey, experiment with identity operators, understand their use cases, and incorporate them into your code where needed. For more insights and practical examples, check out our tutorials on Python Central Hub!

Was this page helpful?

Let us know how we did