Online Tutorials & Training Materials | STechies.com
Register Login

Id() function in Python

|| 0

Id() function in Python
Stechies

id() function in Python is an inbuilt function that returns a unique integer identity of an object. This identity has to be 100% unique and constant for this object during its lifetime, although two objects may have the same id() value if they have non-overlapping lifetimes.

CPython implementation note: Id() is the address of the object in memory.

Important points

  • id() function is a Python inbuilt function
  • The id() function returns a unique id (identity) for specified object.
  • Every Python objects have its unique id.
  • id is assigned to the object at the time when it's created.
  • Two objects may have similar id if they have non-overlapping lifetimes.
  • id() act as an address of an object in the memory.
  • Most of the time id() function is used internally in Python.

Syntax

count(object)

Parameter

Id() function takes object as a parameter. It can be any object, String, Integer, List, etc.

Return Value

Id() function returns unique identity for specified object.

Example of id() In Python

1) Declaring and Initializing variables with an integer value 

#Program to demonstrate the working of `id` Function in Python

#Declaring and Initializing variables with integer value 
a = 10
b = 15
c = 10
d = 15

#Printing the id values of the variables
print("ID of variable a = ", id(a))
print("ID of variable b = ", id(b))
print("ID of variable c = ", id(c))
print("ID of variable d = ", id(d))

#Checking the id of same integer value variables
if(id(a) == id(c)):
    #If, if condition is true
    print("variable `a` and variable `c` has the same id valuen Because they are referring to the same object");

if(id(b) == id(d)):
    #If, if condition is true
    print("variable `b` and variable `d` has the same id valuen Because they are referring to the same object");

Output

ID of variable a =  10105376
ID of variable b =  10105536
ID of variable c =  10105376
ID of variable d =  10105536
variable `a` and variable `c` has the same id value
 Because they are referring to the same object
variable `b` and variable `d` has the same id value
 Because they are referring to the same object

2) Declaring and Initializing variables with a string value 

#Program to demonstrate the working of `id` Function in Python

#Declaring and Initializing variables with string value 
a = "Stechies"
b = "Stechies"
c = "Python"
d = "Python"
e = "Tutorial"
f = "Tutorial"

#Printing the id values of the variables
print("ID of variable a = ", id(a))
print("ID of variable b = ", id(b))
print("ID of variable c = ", id(c))
print("ID of variable d = ", id(d))
print("ID of variable e = ", id(e))
print("ID of variable f = ", id(f))

#Checking the id of same string value variables
if(id(a) == id(b)):
    #If, if condition is true
    print("variable `a` and variable `c` has the same id valuen Because they are referring to the same object");

if(id(c) == id(d)):
    #If, if condition is true
    print("variable `b` and variable `d` has the same id valuen Because they are referring to the same object");
    
if(id(e) == id(f)):
    #If, if condition is true
    print("variable `e` and variable `f` has the same id valuen Because they are referring to the same object");

Output

ID of variable a =  139952820286576
ID of variable b =  139952820286576
ID of variable c =  139952820901400
ID of variable d =  139952820901400
ID of variable e =  139952820286704
ID of variable f =  139952820286704
variable `a` and variable `c` has the same id value
 Because they are referring to the same object
variable `b` and variable `d` has the same id value
 Because they are referring to the same object
variable `e` and variable `f` has the same id value
 Because they are referring to the same object

3) Declaring and Initializing the tuple

#Program to demonstrate the working of `id` Function in Python
#Declaring and Initializing the tuple

tuple1 = ('stechies','python','tutorial')
#printing the id of tuple 1
print("ID of tuple-1 : ",id(tuple1))
tuple2 = ('stechies','python','tutorial')
#printing the id of tuple 2
print("ID of tuple-2 : ",id(tuple2))
tuple3 = ('Stechies','Python','Tutorials')
#printing the id of tuple 3
print("ID of tuple-3 : ",id(tuple3))

Output

ID of tuple-1 :  140294853059136
ID of tuple-2 :  140294853059352
ID of tuple-3 :  140294853059424

4) Declaring and Initializing the dictionary

#Program to demonstrate the working of `id` Function in Python
#Declaring and Initializing the dictionary - 1
dictionary1 = {"productPrice" : 255.39, "productQuantity": 20, "purchaseQuantity":7}
#printing the Id value of dictionary - 1
print("ID of dictionary - 1 : ",id(dictionary1));
#Declaring and Initializing the dictionary - 2
dictionary2 = {"productPrice" : 255.39, "productQuantity": 20, "purchaseQuantity":7}
#printing the Id value of dictionary - 2
print("ID of dictionary - 2 : ",id(dictionary2));

Output

ID of dictionary - 1 :  140055778432776
ID of dictionary - 2 :  140055778432904

5) id Function in Python using Custom Objects

#id Function in Python with custom objects

#creating a custom class
class productSale:
    totalProductQuantity = 250
    productPrice = 799.99
    productSold = 120
    totalRevenue = productPrice * productSold

#creating a custom object - 1 of custom class
productSaleObject1 = productSale()

#creating a custom object - 2 of custom class
productSaleObject2 = productSale()

#Printing the ID of both the objects
print("ID of first object : ",id(productSaleObject1))
print("ID of first object : ",id(productSaleObject2))

Output

ID of first object :  140422682283704
ID of first object :  140422682283760

6) id Function in Python with Lists

#program to demonstrate working of `id` Function with Lists Python 

#Declaring the list - 1
productName = ["Study Table", "Study Chair", "Table Lamp", "Fan", "Samsung A30", "Nokia 7 Plus", "Nokia 8.1 Plus", "IPhone xs"]
#Printing the id of list - 1
print("Id of list - 1 is : ",id(productName))
#Declaring the list - 2
productName1 = ["Study Table", "Study Chair", "Table Lamp", "Fan", "Samsung A30", "Nokia 7 Plus", "Nokia 8.1 Plus", "IPhone xs"]
#Printing the id of list - 1
print("Id of list - 2 is : ",id(productName1))

#######################################################
##But if we get the id of list elemets they are same###
#######################################################
#Printing the id of first element of both the lists
print("Id of first Element of List - 1 is : ",id(productName[0]))
print("Id of first Element of List - 2 is : ",id(productName1[0]))

Output

Id of list - 1 is :  139821012230728
Id of list - 2 is :  139821012224136
Id of first Element of List - 1 is :  139821012223152
Id of first Element of List - 2 is :  139821012223152