Python Program to Display Memory Address of Variables or Objects

Table of Contents

Built-in function id() returns the identity of an object which is the object's memory address.

Valur returned by id() is guaranteed to be unique among simultaneously existing objects.

Since in python every entity is an object, variables are named memory location and are also an object of class like int, float, ... etc.

Python Source Code: Memory Address of Variables or Objects


# Variable & Memory Address

number = 13

name = 'Hello there!'

my_list = [3,'apple',67.8]


print('-----------------')
print('Address of number in decimal: ', id(number))
print('Address of number in hexadecimal:  ', hex(id(number)))
print('-----------------')
print('Address of name in decimal: ', id(name))
print('Address of name in hexadecimal:  ', hex(id(name)))
print('-----------------')
print('Address of my_list in decimal: ', id(my_list))
print('Address of my_list in hexadecimal:  ', hex(id(my_list)))

Python Output: Addresses

-----------------
Address of number in decimal:  140732606227216
Address of number in hexadecimal:   0x7ffedd00a310
-----------------
Address of name in decimal:  2700928973936
Address of name in hexadecimal:   0x274dbe97c70
-----------------
Address of my_list in decimal:  2700927947208
Address of my_list in hexadecimal:   0x274dbd9d1c8