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

Loading editor…

Explanation

Both variables contain the same value.

Therefore, a == b returns True.

Example - Using is Operator

Loading editor…

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

Loading editor…

Important Observation

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

Example - Different Objects

Loading editor…

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

Loading editor…

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

Loading editor…

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

Loading editor…

Explanation

Both variables refer to the same list object.

Modifying a Shared Object

Loading editor…

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

Loading editor…

Another Example - is not

Loading editor…

Difference Between == and is

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

Example - == vs is

Loading editor…

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

Loading editor…

Why is This Recommended?

None is a singleton object in Python.

Using is None is the recommended and preferred style.

Less Preferred Style

Loading editor…
📝 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

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.