id () function in python

Python Methods and Functions

Introduction
id () is a built-in function in Python. 
Syntax :

id(object)

As we can see, the function takes one parameter and is used to return the object ID.  This identity must be unique and persistent for this object throughout its life. Two objects with non-overlapping lifetimes can have the same id () value. If we associate this with C, then it is actually a memory address, here in Python it is a unique identifier. This function is commonly 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 

# This program shows different personalities

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 ]))

Output:

 140252505691448 140252505691448 True 140252505691840 140252505739928 False 




Tutorials