Hackforge Academy

Category: python

Identity vs Equality

Published on 27 Oct 2026

Explanation

== compares values; is checks whether two references identify the same object.

Code:

a = [1, 2]
b = [1, 2]
print(a == b, a is b)

Explanation

Lists with equal contents may be separate objects. Mutating one does not change the other unless they share a reference.

Code:

first = [10, 20]
second = [10, 20]
second.append(30)
print(first == second)

Explanation

Use is None or is not None to check for the special None object.

Code:

result = None
if result is None:
    print('No result')

Explanation

Do not use is to compare ordinary numbers or strings; implementation optimizations can make identity behavior unpredictable.

Code:

a = 1000
b = 1000
print(a == b)  # Compare values with ==

Explanation

Separate instances are not identical. Unless equality is customized, separate instances are also unequal with ==.

Code:

class Student:
    def __init__(self, name): self.name = name
a = Student('Arun')
b = Student('Arun')
print(a is b, a == b)

πŸš€ Learn Spring Boot with real-world projects

πŸ’‘ Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
βš›οΈ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
β˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community