id() function in Python (original) (raw)

Last Updated : 3 Apr, 2026

The id() function is used to get the unique identity number of an object in memory. This identity remains the same as long as the object exists. If two variables have the same id(), it means they are pointing to the same object in memory.

Syntax

id(object)

x = 10 y = x print(id(x)) print(id(y))

`

Output

139745147511024 139745147511024

**Note: id() values may differ across Python environments and runs.

**In the above example:

Examples

**Example 1: This example compares two different list objects to show that even with similar values, their memory identity is different.

Python `

x = [1, 2] y = [1, 2]

print(id(x)) print(id(y))

`

Output

128102271441856 128102273731776

**In the above example:

**Example 2: This example uses a custom class to show that each object has its own unique identity.

Python `

class A: pass

p = A() q = A()

print(id(p)) print(id(q))

`

Output

126157094693120 126157093473488

**In the above example:

**Example 3: This example checks whether two strings with the same text refer to the same object in memory.

Python `

s1 = "python" s2 = "python"

print(id(s1)) print(id(s2))

`

Output

139546861085408 139546861085408

**In the above example:

**Example 4: This example shows how tuple objects with the same values may or may not share memory.

Python `

t1 = (1, 2, 3) t2 = (1, 2, 3)

print(id(t1)) print(id(t2))

`

Output

130201030380352 130201030380352

**In the above example:

Checking Object Identity Using is

The is keyword in Python is used to check whether two variables point to the same object in memory.

Python `

x = 10 y = x

print(x is y)
print(id(x) == id(y))

`

**Explanation: