Python Objects, Values, Types, and Identity
Build the core Python data-model mental model: every value is an object with a type and identity, and learn how equality, mutability, and hashability fit together.
Intermediate
Python programs work with objects. Every object has an identity, a type, and a value. Keeping those three ideas separate makes many Python behaviors predictable.
One object, three properties
The identity answers “which object is this?”, the type answers “what kind of object is it?”, and the value is the data the object represents. The built-ins id() and type() expose identity and type information.
Output
Ready to run.
Identity is not equality
Use is when the question is whether two names refer to the very same object. Use == when the question is whether two objects compare as equal. Two separate lists can contain equal values without being the same list.
For ordinary application code, do not use is as a replacement for ==. A common identity test is value is None because None is the singleton object representing the absence of a value.
Output
Ready to run.
Types determine behavior
An object’s type determines which operations it supports. type(value) returns the object’s type. Types are themselves objects, and class objects participate in Python’s type system; class creation and metaclasses are taught separately.
Mutability: can the object itself change?
A mutable object can change its contents while remaining the same object. A list is mutable. A string and a tuple are immutable: operations that appear to “change” them instead produce another value. This is about the object’s state, not whether a name can be rebound.
Output
Ready to run.
Hashability connects objects to dictionaries and sets
A hashable object has a hash value that remains consistent during its lifetime and can therefore be used as a dictionary key or a set element, subject to Python’s equality and hashing rules. The built-in hash() returns an integer hash when an object is hashable.
Immutable built-in values such as strings, numbers, and tuples whose elements are hashable can be hashable. A list is not hashable because it is mutable. Hashability is not simply another word for immutability, so treat it as its own property.
Output
Ready to run.
How these ideas fit together
Names bind to objects; the name itself is not the object.
Identity distinguishes one object from another; equality asks whether two objects compare as equal.
Mutability describes whether an object can change its state.
Hashability determines whether an object can participate as a dictionary key or set element.
Later lessons can customize equality and hashing with special methods, but this lesson owns the underlying concepts.
These concepts are the foundation for Python collections, classes, iteration, and data-model protocols. When you are ready, take the Python Objects, Values, Types, and Identity quiz — then complete the coding exercise.
Thanks for your feedback.
Mark this learning resource complete to track your learning progress.