Python id Function with Examples

The id function in Python provides the identity of an object. This identity is represented as a unique integer. By accepting an object as its argument, this function yields a distinctive integer that signifies the object's identity. It's important to note that two objects that do not share overlapping lifetimes could potentially possess the same id value.

Python id Function Syntax

It has the following syntax:

Example

id (object)

Parameters

  • object: This refers to an object for which the identifier is to be retrieved.
  • Return

It returns a unique integer number.

Different Examples for Python id Function

Let us explore a few instances of the id function to gain a better understanding of its capabilities.

Python id Function Example 1

Let us consider an example to illustrate the functionality of the Python id function.

Example

# Python id() function example

# Calling function

val = id("Example") # string object

val2 = id(1200) # integer object

val3 = id([25,336,95,236,92,3225]) # List object

# Displaying result

print(val)

print(val2)

print(val3)

Output:

Output

139963782059696

139963805666864

139963781994504

Python id Function Example 2

To illustrate the functionality of the Python id function, we can consider the following example.

Example

# Python id() function example

class Student:

    def __init__(self, id, name):

        self.id = id

        self.name = name

student = Student(101,"Mohan")

print(student.id)

print(student.name)

# Calling function

val = id(student) # student class object

# Displaying result

print("Object id:",val)

Output:

Output

101

Mohan

Object id: 140157155861392

Python id Function Example 3

Let’s consider an additional illustration to showcase the functionality of the Python id function.

Example

# Python id() function example

l1 = [1,2,3,4]

l2 = [1,2,3,4]

l3 = [3,5,6,7]

# Calling function

id1 = id(l1)

id2 = id(l2)

id3 = id(l3)

# Displaying result

print((l1==l2),(l1==l3))

# Objects with the same values can have different ids

print((id1==id2),(id1==id3))

# l1 and l2 returns True, while id1 and id2 returns False

Output:

Output

True False

False False

Input Required

This code uses input(). Please provide values below: