Nearby lessons

35 of 159

Python - Identity Operators

📌 What You Will Learn
  • Define identity operators and what they compare
  • Use the is and is not operators
  • Distinguish identity comparison from value comparison with ==
  • Check object identity using the id() function
  • Use is None to check for None values

Introduction

Identity operators are used to compare the identity of objects.

They check whether two variables refer to the same object or different objects in memory.

Identity operators do not compare the values stored inside the objects.

Identity Operators List

Operator Meaning
is Returns True if both variables refer to the same object.
is not Returns True if both variables refer to different objects.

Important Note

Identity operators compare the memory location (object identity), not the value stored inside the object.

Difference Between == and is

== Operator is Operator
Compares values. Compares object identity.
Checks content equality. Checks memory reference.

Example - Using == Operator

🐍Code Cell
1a = 10
2b = 10
3 
4print(a == b)
Output
True

Explanation

Both variables contain the same value.

Therefore, a == b returns True.

Example - Using is Operator

🐍Code Cell
1a = 10
2b = 10
3 
4print(a is b)
Output
True

Explanation

Both variables refer to the same object in memory.

Therefore, a is b returns True.

Checking Object Identity Using id()

Python provides the id() function to check the identity (memory reference) of an object.

Example - id() Function

🐍Code Cell
1a = 10
2b = 10
3 
4print(id(a))
5print(id(b))
Output
140723456100
140723456100

Important Observation

If both IDs are the same, both variables refer to the same object.

Example - Different Objects

🐍Code Cell
1a = [10, 20]
2b = [10, 20]
3 
4print(a == b)
5print(a is b)
Output
True
False

Explanation

a == b returns True because both lists contain the same values.

a is b returns False because they are different objects.

Checking IDs of Different Objects

🐍Code Cell
1a = [10, 20]
2b = [10, 20]
3 
4print(id(a))
5print(id(b))
Output
140723451000
140723452000

Important Observation

The IDs are different, so both variables refer to different objects.

Identity Operator with Strings

Python may reuse immutable objects such as strings to improve memory usage.

Example - String Identity

🐍Code Cell
1s1 = "python"
2s2 = "python"
3 
4print(s1 is s2)
Output
True

Explanation

Python may store both variables in the same memory location because strings are immutable.

Identity Operator with Mutable Objects

If two variables refer to the same mutable object, the identity operator returns True.

Example - Same List Object

🐍Code Cell
1l1 = [10, 20]
2l2 = l1
3 
4print(l1 is l2)
Output
True

Explanation

Both variables refer to the same list object.

Modifying a Shared Object

🐍Code Cell
1l1 = [10, 20]
2l2 = l1
3 
4l1.append(30)
5 
6print(l2)
Output
[10, 20, 30]

Why Did the Output Change?

Both variables refer to the same list object.

When one variable modifies the list, the change is visible through the other variable.

Identity Operator - is not

The is not operator checks whether two variables refer to different objects.

Example - is not Operator

🐍Code Cell
1a = [10, 20]
2b = [10, 20]
3 
4print(a is not b)
Output
True

Another Example - is not

🐍Code Cell
1a = 10
2b = 10
3 
4print(a is not b)
Output
False

Difference Between == and is

Expression Meaning Result
a == b Values are equal. True
a is b Same object. False

Example - == vs is

🐍Code Cell
1a = [1, 2, 3]
2b = [1, 2, 3]
3 
4print(a == b)
5print(a is b)
Output
True
False

Real World Usage of Identity Operators

Identity operators are useful for:

  • Comparing object references.
  • Checking None values.
  • Validating memory references.

Checking None Using is

The recommended way to check whether a variable contains None is by using the is operator.

Example - Checking None

🐍Code Cell
1x = None
2 
3print(x is None)
Output
True

Why is This Recommended?

None is a singleton object in Python.

Using is None is the recommended and preferred style.

Less Preferred Style

🐍Code Cell
1print(x == None)
Output
True
📝 Key Takeaways
  • Identity operators compare object identity, not value
  • is returns True when two variables refer to the same object
  • is not returns True when two variables refer to different objects
  • == compares values, while is compares memory reference
  • id() returns the memory identity of an object
  • is None is the recommended way to check for None

🧠 Test Your Knowledge

8 Questions
Progress: 0 / 8