Skip to content

Python Identity Operators

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
isReturns True if both variables are the same objectx is y
is notReturns True if both variables are not the same objectx is not y

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

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

Output:

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

In the above example, we have used the is operator to check if the value of x is the same as the value of y. Since the value of x is the same as the value of y, the condition becomes True. The result of the is operator is then assigned to the variable z. The value of z is then printed to the console.

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

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

In the above example, we have used the is not operator to check if the value of x is not the same as the value of y. Since the value of x is not the same as the value of y, the condition becomes True. The result of the is not operator is then assigned to the variable z. The value of z is then printed to the console.

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)

Output:

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

In the above example, we have used the is operator to check if the value of x is the same as the value of y. Since the value of x is not the same as the value of y, the condition becomes False. The result of the is operator is then assigned to the variable z. The value of z is then printed to the console.

In the above example, we have used the is not operator to check if the value of x is not the same as the value of y. Since the value of x is not the same as the value of y, the condition becomes True. The result of the is not operator is then assigned to the variable t. The value of t 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)

Output:

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

In the above example, we have used the is operator to check if the value of x is the same as the value of y. Since the value of x is not the same as the value of y, the condition becomes False. The result of the is operator is then assigned to the variable z. The value of z is then printed to the console.

In the above example, we have used the is not operator to check if the value of x is not the same as the value of y. Since the value of x is not the same as the value of y, the condition becomes True. The result of the is not operator is then assigned to the variable t. The value of t is then printed to the console.

In the above example, we have assigned the value of x to y. Since both variables point to the same object, the is operator returns True and the is not operator returns False.

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)

Output:

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

In the above example, we have used the is operator to check if the value of x is the same as the value of y. Since the value of x is the same as the value of y, the condition becomes True. The result of the is operator is then assigned to the variable z. The value of z is then printed to the console.

In the above example, we have used the is not operator to check if the value of x is not the same as the value of y. Since the value of x is the same as the value of y, the condition becomes False. The result of the is not operator is then assigned to the variable t. The value of t is then printed to the console.

In the above example, we have assigned the value of x to y. Since both variables point to the same object, the is operator returns True and the is not operator returns False.

sketch is compares identity; == compares value p5.js
They agree often enough to hide the bug and disagree exactly where it hurts. Python caches small integers from -5 to 256, so two separately computed 256s really are the same object and is returns True -- then 257 returns False with no warning. Strings behave the same way: identical literals share an object, and constant folding covers simple concatenation, but a string genuinely assembled at runtime is a different object with equal contents. All values below were checked in the interpreter.

Comparison Operators vs Identity Operators

Section titled “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)

Output:

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 x is the same as the value of y. Since the value of x is the same as the value of y, the condition becomes True. The result of the == operator is then assigned to the variable z. The value of z is then printed to the console.

In the above example, we have used the is operator to check if the value of x is the same as the value of y. Since the value of x is not the same as the value of y, the condition becomes False. The result of the is operator is then assigned to the variable t. The value of t is then printed to the console.

Conditional Statements with Identity Operators

Section titled “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")

Output:

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

In the above example, we have used the is operator in the conditional statement. Since the value of x is not the same as the value of y, the condition becomes False. The else block is executed in this case.

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!


pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading