Python Identity Operators

1. What are Identity Operators?

Identity operators are used to check whether two variables point to the same object in memory.

In simple words:
πŸ‘‰ They check β€œAre both variables the same thing?”

Python has two identity operators:

  • is

  • is not

2. Identity vs Equality (Very Important)

  • == checks value

  • is checks identity (memory location)

Example

a = 10
b = 10
print(a == b) # True (same value)
print(a is b) # True (same object for small numbers)

3. The is Operator

is returns True if both variables refer to the same object.

Example

x = ["apple", "banana"]
y = x
print(x is y)

Output:

True

Here:

  • y points to the same list as x

4. The is not Operator

is not returns True if both variables do not refer to the same object.

Example

a = ["red", "blue"]
b = ["red", "blue"]
print(a is not b)

Output:

True

Even though values look same, they are different objects.

5. Identity with Numbers (Simple Note)

Small numbers may behave the same because Python saves memory.

Example

x = 5
y = 5
print(x is y)

Output:

True

πŸ‘‰ Do not depend on this behavior.

6. Identity with Lists (Important)

Lists with same values are not the same object.

Example

a = [1, 2, 3]
b = [1, 2, 3]
print(a == b) # True
print(a is b) # False

7. Identity Operator in if Statement

Example

x = None

if x is None:
print(“x has no value”)

πŸ‘‰ This is a very common and correct use of is.

8. Common Beginner Mistakes

❌ Using is Instead of ==

if name is "Amit":

βœ” Correct:

if name == "Amit":

❌ Comparing Values Instead of Objects

print(10 is 10)

It works, but it is not good practice.

9. Simple Practice Examples

Example 1: Check None

value = None

print(value is None)

Example 2: Same List Check

list1 = [1, 2]
list2 = list1
print(list1 is list2)

Example 3: Different Objects

a = "Hello"
b = "Hello"
print(a == b)
print(a is b)

10. Summary (Identity Operators)

βœ” Identity operators check memory location
βœ” is means same object
βœ” is not means different object
βœ” Use == for value check
βœ” Use is for None check

πŸ“˜ Perfect for Beginner eBook

This chapter is ideal for:

  • Python beginner books

  • School & college students

  • Self-learners

If you want next, I can write:

  • Python Membership Operators

  • Operator Exercises

  • if–else statements

  • MCQs with answers

Just tell me 😊

You may also like...