⬅ Previous Topic
Python hex() Function – Convert Integer to HexadecimalNext Topic ⮕
Python input() Function – Get User Input Easily⬅ Previous Topic
Python hex() Function – Convert Integer to HexadecimalNext Topic ⮕
Python input() Function – Get User Input Easilyid()
FunctionThe id() function in Python returns a unique identifier for a given object. This identifier is the object's memory address, which remains constant during the object's lifetime.
id(object)
object
– Any Python object (string, list, number, etc.)id()
with Integersx = 10
print(id(x))
140709661559888
(Output will vary depending on your system)
a = 100
b = 100
print(id(a) == id(b))
True
In CPython, small integers and strings are cached, so variables with the same value may share the same ID.
x = [1, 2, 3]
y = [1, 2, 3]
print(id(x) == id(y))
False
x
and y
are different list objects, even though they have the same content.
id()
?is
)In Python interviews, id()
is often discussed in the context of is
vs ==
comparisons, especially for mutable vs immutable types.
id(object)
returns the unique identity (memory address) of the object.is
keyword.Create two variables with the same value. Check if their IDs are equal. Then change one value and compare again.
a = "hello"
b = "hello"
print("Before change:", id(a) == id(b))
b = b + "!"
print("After change:", id(a) == id(b))
⬅ Previous Topic
Python hex() Function – Convert Integer to HexadecimalNext Topic ⮕
Python input() Function – Get User Input EasilyYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.