Python Reference Library →

Python id() Function – Get Memory Address of an Object

Let’s look at id() function – get memory address of an object

  1. Notice

    id() identifies an object during its lifetime. Identity compares objects; it is different from comparing their values.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
items = [3]
other = items
print(id(items) == id(other))
Live state

Top-level scope

items→Object 1
other→Object 1
Object 1 · list[3]
This step

id() identifies an object during its lifetime. Identity compares objects; it is different from comparing their values.

Picture id() function – get memory address of an object

id() identifies an object during its lifetime. Identity compares objects; it is different from comparing their values.

Image brief

Alt text
A diagram for Python id() Function – Get Memory Address of an Object. Instructions: items = [3] other = items print(id(items) == id(other)). Displayed result: True. The final example bindings are items --> object 1 (list); other --> object 1 (list). object 1: [3]
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python id() Function – Get Memory Address of an Object". Teaching focus: id() identifies an object during its lifetime. Identity compares objects; it is different from comparing their values. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS items = [3] other = items print(id(items) == id(other)) | v DISPLAYED RESULT True FINAL BINDINGS items --> object 1 (list) other --> object 1 (list) object 1: [3] Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python id() Function – Get Memory Address of an Object. Instructions: items = [3] other = items print(id(items) == id(other)). Displayed result: True. The final example bindings are items --> object 1 (list); other --> object 1 (list). object 1: [3]

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
items = [3]
other = items
print(id(items) == id(other))
Live state

Top-level scope

items→Object 1
other→Object 1
Object 1 · list[3]
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
True

What do you expect this program to print?

What does this example display?

items = [3]
other = items
print(id(items) == id(other))
What does this example display?
Choose one answer. You can try again.

Answer and explanation

True

Follow the printed values in order. The output is: True

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

id() identifies an object during its lifetime. Identity compares objects; it is different from comparing their values.

id() identifies an object during its lifetime. Identity compares objects; it is different from comparing their values.

More examples and reference

Python id() Function

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

Syntax

id(object)

Parameters:

  • object – Any Python object (string, list, number, etc.)

Returns:

  • An integer that represents the identity of the object.

Example 1: Using id() with Integers

x = 10
print(id(x))
140709661559888

(Output will vary depending on your system)

Example 2: Same Value, Same ID?

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.

Example 3: Different Objects

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.

Why Use id()?

  • To compare if two variables refer to the same object (especially with is)
  • For debugging object references and identity issues
  • To understand Python’s memory model

Important Notes

  • IDs are guaranteed to be unique and constant for the object during its lifetime.
  • Once an object is garbage collected, its ID may be reused.

Interview Tip

In Python interviews, id() is often discussed in the context of is vs == comparisons, especially for mutable vs immutable types.

Summary

  • id(object) returns the unique identity (memory address) of the object.
  • Useful for checking object identity, especially with is keyword.
  • May give the same result for immutable types with the same value.

Practice Problem

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

What to remember

Connect what you noticed in the example with the idea below.

Remember

id() identifies an object during its lifetime. Identity compares objects; it is different from comparing their values.

Watch out for this idea

id() is a stable identifier across all Python runs.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →