The built-in id() function returns an integer that uniquely identifies an object for as long as that object exists. Two names with the same id() refer to the exact same object, which is what the is operator tests.
In CPython, the reference implementation, the value is the memory address of the object, but the language only promises that it is unique and constant during the object's lifetime. Do not treat it as a stable key across runs or after an object is deleted.
id(obj) takes any object and returns an int. The number differs from run to run, so any output below is only an example:
The lists are equal in value (==) but are two different objects, so they have different identities. Equality compares values, identity compares objects. The expression a is b is equivalent to id(a) == id(b), and you should write is in real code.
Assignment never copies an object; it binds another name to it. id() makes that visible:
This is the classic explanation for bugs with mutable default arguments and shared lists. When changing one variable unexpectedly changes another, compare their id() values, or use is, to confirm they are the same object.
For performance, CPython reuses some immutable objects. Small integers (from -5 to 256) are cached, and many short strings that look like identifiers are interned. This is an implementation detail and must not be relied on:
The first line normally prints True. The second result depends on the Python version and on whether both values are compiled in the same code block, so it can be True or False. Because of this, never compare numbers or strings with is. Use == for values, and reserve is for singletons such as None, True and False:
Since Python 3.8, comparing a literal with is produces a SyntaxWarning because it is almost always a bug.
Immutable types such as int, str and tuple cannot change in place, so an operation produces a new object with a new id():
Rebinding the name n makes it point to a new int, while += on a list extends the same list. Watching id() before and after an operation is a simple way to learn whether a type is modified in place.
Good uses of id():
Keep a reference to the objects while you use their ids. Once an object is garbage collected, Python may reuse the same number for a new object, so a stored id() can end up pointing at something unrelated. For caching or persistent identifiers, use your own keys, a uuid.UUID, or a database id instead. The default object.__hash__ is derived from identity, but hash() is not the same as id() and should not be used to read memory addresses.
In CPython, yes, but this is an implementation detail. Other implementations such as PyPy return different numbers, so use id() only to compare identity.
is checks whether two names refer to the same object, while == checks whether the objects have equal values. Use is for None, and == almost everywhere else.
Not at the same time. After an object is deleted, a new object can reuse its id.
CPython may intern or reuse immutable objects. Never depend on it; compare strings with ==.
id() returns a number that identifies an object while it is alive, and is is the idiomatic way to compare identities. Use it to debug aliasing and understand mutability, compare values with ==, use is only for singletons like None, and never store an id() as a long-lived key.