2 types of Identity Operators in Python 3

2 types of Identity Operators in Python 3



Identity operators in Python are used to verify whether two variables refer to same memory location or not.

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



Spread the love
Lesson tags: 2 types of Identity Operators in Python 3, Identity Operators in Python
Back to: Python Programming Tutorial
Spread the love