Home » Python

id() function with example in Python

Python id() function: Here, we are going to learn about the id() function in Python with example.
Submitted by IncludeHelp, on April 03, 2019

Python id() function

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 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.

Syntax:

    id(object)

Parameter(s):object – an object like int, float, string, list, tuple etc.

Return value: float – it returns a unique identity number (which is in integer format) of given object.

Example:

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

Python code to get id(s) of various type 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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.