Python id() Function: Use, Syntax, and Examples

Python id() function: In this tutorial, we will learn about the id() function in Python with its use, syntax, parameters, returns type, and examples. By IncludeHelp Last updated : June 24, 2023

Python id() function

The id() function is a library function in Python, it is used to get a unique identity number (id) of an object, it accepts an object (like int, float, string, list, etc) and returns a unique id number.

What is an Id?

An Id is a memory address of each object, which is assigned while an object is created, all objects have their own unique identity number, that can be changed to execute the program again.

Consider the below example with sample input/output values:

Input:
a = 10  
    
print("id(a): ", id(a))
    
Output:
id(a):  10455328

Syntax

The following is the syntax of id() function:

id(object)

Parameter(s):

The following are the parameter(s):

  • object – An object like int, float, string, list, tuple etc.

Return Value

The return type of id() function is <type 'int'>, it returns a unique identity number (which is in integer format) of given object.

Python id() Example 1: Get the IDs of various types of objects

# python code to demonstrate example
# of id() function

a = 10  # integer variable
b = 10.23  # float variable
c = "Hello"  # string variable
d = [10, 20, 30, 40, 50]  # list variable

# return type of id()
print("return type of id(): ", type(id(a)))

# getting id of objects

print("id(a): ", id(a))
print("id(b): ", id(b))
print("id(c): ", id(c))
print("id(d): ", id(d))

Output

First run:
return type of id():  <class 'int'>
id(a):  10455328
id(b):  139862812754400
id(c):  139862812332136
id(d):  139862811549960

Second run:
return type of id():  <class 'int'>
id(a):  10455328
id(b):  139836840616416
id(c):  139836840194152
id(d):  139836839411976

Python id() Example 2: Get the ID of the function

def myfunc():
    print("Hello, world!")

# main code
# printing the ID of the function
print(id(myfunc))

Output

139708064300432



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.