PYTHON IDENTITY AND EQUALITY | PYTHON FULL COURSE - 2026

Опубликовано: 30 Июль 2026
на канале: Tech With Machines
35
2

#python #pythonidentity #pythonequality #equalityinpython #pythonprogramming #equalityoperator



Python Identity and Equality
In Python, identity and equality refer to two different concepts when comparing objects. Understanding the difference between them is important for writing accurate and efficient code.

1. Identity (is)
Identity refers to whether two variables refer to the same object in memory.
The is operator is used to check if two objects have the same identity (i.e., they are the same object).
This is not about the values of the objects but whether they point to the same location in memory.
Example:

python
Copy code
a = [1, 2, 3]
b = a # b points to the same list as a

print(a is b) # Output: True, because both a and b refer to the same object in memory

b = [1, 2, 3] # Now b points to a new list
print(a is b) # Output: False, because a and b refer to different objects in memory
In the first case, a and b refer to the same object in memory, so a is b returns True. In the second case, a and b refer to two different objects, so a is b returns False.

2. Equality (==)
Equality refers to whether two objects have the same value or content, regardless of whether they are the same object in memory.
The == operator is used to compare the values of two objects to see if they are equal.
Example:

python
Copy code
a = [1, 2, 3]
b = [1, 2, 3] # b has the same content as a, but it's a different object

print(a == b) # Output: True, because the contents of a and b are the same
In this case, a == b returns True because both lists have the same values, even though they are two distinct objects in memory.

Identity vs Equality: Key Differences
Concept Operator What It Compares Example
Identity is Whether two objects are the same in memory a is b (True if a and b are the same object)
Equality == Whether two objects have the same value/content a == b (True if a and b have the same value)
Common Pitfalls and Important Notes
Immutable Objects: For immutable objects like integers and strings, Python may reuse the same object in certain cases for optimization. This means the is operator might return True for objects that appear to have the same value.

Example:

python
Copy code
a = 1000
b = 1000
print(a is b) # Output: False, because Python may create two different objects for large numbers

a = 10
b = 10
print(a is b) # Output: True, because Python reuses small integers (typically between -5 and 256)
Mutable Objects: For mutable objects like lists and dictionaries, is will generally return False if you create a new instance with the same content, even if the content is identical.

Example:

python
Copy code
a = [1, 2, 3]
b = [1, 2, 3]
print(a is b) # Output: False, because a and b are different objects in memory
Summary
is checks if two variables refer to the same object in memory (identity comparison).
== checks if two variables have equal values (value comparison).
While they may often return similar results, especially for small, immutable objects, they are fundamentally different in how they compare objects.