Python id() Function

The id() function in Python returns a unique id of any specified object. For example:

a = [12, 42, 5, 5]
b = (1, 43, 5)
c = "Python Programming"

print(id(a))
print(id(b))
print(id(c))

The snapshot given below shows the sample output produced by above Python program, demonstrating the id() function:

python id function

Note: Every object in Python gets assigned its own unique id, when it is created. Therefore you'll get different id for the same object, each time when you run the program.

Python id() Function Syntax

The syntax of id() function in Python, is:

id(obj)

where obj refers to an object such as list, string, tuple, class etc.

Python id() Function Example

Here is an example of id() function in Python.

num = 100
a = 100

print(id(100))
print(id(num))
print(id(a))

All the three print() statement prints the same output, that looks like:

2245638378960
2245638378960
2245638378960

This is because, id of a particular one remains constant during the lifetime of a program.

Python Online Test


« Previous Function Next Function »