Tutorial Study Image

Python id()

The id() function returns the unique id, which shows the identity of the object and it is an integer always. This identity will be unique for the object and constant throughout its lifetime.


id(object) #Where object can be int, float, str, list, dict, tuple, etc.

id() Parameters:

Takes only one parameter. An object may be in a type int, float, str, list, dict, tuple, etc.

Parameter Description Required / Optional
object The object whose identity needs to be returned. Required

id() Return Value

The id() method also returns the identity of a variable or object. All variables and literal values are objects, so they all have different id() values.

Input Return Value
variable or literal  a unique integer number.

Examples of id() method in Python

Example 1: How id() works?


class Foo:
    b = 5

dummyFoo = Foo()
print('id of dummyFoo =',id(dummyFoo))
 

Output:

id of dummyFoo = 140343867415240

Example 2:Get Id of Literal Values


print("Id of 10 is: ", id(10))
print("Id of 10.5 is: ", id(10.5))
print("Id of 'Hello World' is: ", id('Hello World'))
print("Id of list is: ", id([1, 2, 3, 4, 5]))
 

Output:

Id of 10 is: 8791113061
Id of 10.5 is: 3521776
Id of 'Hello World' is: 60430408
Id of list is: 5466244

Example 3:The identity of the two same values is the same


num = 10

print("Id of num is: ", id(num))
print("Id of 10 is: ",id(10))
 

Output:

Id of i is: 8791113061696
Id of 10 is: 8791113061696