2 types of Identity Operators in Python 3

2 types of Identity Operators in Python 3

The 2 Types of Identity Operators in Python 3 are used to check whether two variables refer to the same object in memory. Unlike equality operators that compare values, identity operators compare object identity. They are of two types:

i. is operator:

It returns true if two variables refer to same memory location.

Example:

a=10
b=10
print(a is b)

Output is True as id(a) is same as id(b) as they both refer to same value 10.

ii. is not operator:

It returns true if two variables don’t refer to same memory location.

Example:

a=10
b=10
print(a is not b)

Output is False as id(a) is same as id(b). Output will be True only if a and b both contain different values.

Program to demonstrate Identity Operators

Output

a=10
b=10
print(a is b)
print(a is not b)
True
False

Lesson tags: 2 types of Identity Operators in Python 3, Identity Operators in Python
Back to: Python Programming