The id() function returns the memory address of an object within  the current execution of a Python program.

The function takes one argument which is the object whose address we want, and returns an integer representing the address. The memory address can change during the lifetime of the object, and it will certainly vary across different program executions.

Syntax:
id(object)

The memory address returned by the function is  not guaranteed to be the physical address of the object in memory. Instead, it represents a unique identifier for the object within the current program execution.

ExampleEdit & Run
print( id(1) )

a = "Hello, World!"

print( id(a) )
Output:
140558163023944140558148791216[Finished in 0.010718745877966285s]

The id() function provides a way to distinguish between objects by comparing their memory addresses. However, it is important to note that the function is not intended to determine object identity with certainty. Two different objects may have the same memory address at different points in time if one object's memory is de-allocated and then reallocated for a different object.

ExampleEdit & Run
str1 = "Hello"
str2 = "Hello"

print(id(str1), id(str2))

print(id(str1) == id(str2))
Output:
139975256386800 139975256386800True[Finished in 0.01017928496003151s]

Since the variables point to the same memory address, we can conclude that the same object is being referenced.

ExampleEdit & Run
list1 = [1, 2, 3]
list2 = [1, 2, 3]

print(id(list2), id(list1))

print(id(list1) == id(list2))
Output:
139986797082624 139986799537984False[Finished in 0.010384643916040659s]

Since the two variables point to different memory addresses, we can conclude that different objects are being referenced.

It's worth noting that the id() function is primarily used for low-level operations or for debugging purposes. In most general programming scenarios, you can rely on other means of object comparison and identity, such as the == operator or is keyword, instead of directly using the id() function.