Python Identity vs Equality
Core idea
Equality (==) asks whether two values compare equal. Identity (is) asks whether both expressions refer to the very same object. An object is a value with its own identity in memory. Different objects can compare equal. Use is None and is not None for absence checks, not is for comparing ordinary numeric or text values.
first = [1, 2]second = [1, 2]print(first == second)print(first is second)A list is an ordered collection of values; square brackets create a new list here. Each literal creates a separate list, and equal elements make their values compare equal. Lists will be explored in a later chapter.
Two names, one object
first = [1, 2]saved = firstprint(saved is first)print(saved is not first)Assignment binds saved to the same list; it does not create a copy.
Check for a missing reading
reading = Noneprint(reading is None)reading = 0print(reading is not None)Zero can be a valid reading. A None check distinguishes it from absence.
Common mistake
Do not rely on identity between separately written integers or strings. Python implementations may reuse immutable objects. Use == when the goal is value comparison.
Practice
Create two lists with the same elements, then bind a third name to the first list. Predict the equality and identity results for each pair.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Identity and equality
Check your understanding of identity and equality.
What is printed?
a = [1]b = [1]print(a is b)What is printed?
a = [1]b = [1]print(a == b)What is printed?
a = [1]b = aprint(a is b)Which expression specifically checks that a reading is not absent?