id() function in Python

Introduction
id() is an inbuilt function in Python.
Syntax:

id(object)

As we can see the function accepts a single parameter and is used to return the identity of an object. This identity has to be unique and constant for this object during the lifetime. Two objects with non-overlapping lifetimes may have the same id() value. If we relate this to C, then they are actually the memory address, here in Python it is the unique id. This function is generally used internally in Python.

Examples:

The output is the identity of the 
object passed. This is random but 
when running in the same program, 
it generates unique and same identity. 

Input : id(1025)
Output : 140365829447504
Output varies with different runs

Input : id("geek")
Output : 139793848214784
filter_none

edit
close

play_arrow

link
brightness_4
code

# This program shows various identities
str1 = "geek"
print(id(str1))
  
str2 = "geek"
print(id(str2))
  
# This will return True
print(id(str1) == id(str2))
  
# Use in Lists
list1 = ["aakash", "priya", "abdul"]
print(id(list1[0]))
print(id(list1[2]))
  
# This returns false
print(id(list1[0])==id(list1[2]))

chevron_right


Output:

140252505691448
140252505691448
True
140252505691840
140252505739928
False


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

4


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.