Code Crafter IT Innovation
Loading...
  • Bansal Computer Center
  • punjabedu.bansalcenter.com

Python Memory Concepts

What is Memory in Python?

Memory is the physical or virtual space where Python stores data (objects) while a program is running. Every value you create—whether a number, a string, or a list—occupies a specific spot in your RAM.

Variables & References

In Python, a variable does not actually store the value itself. Instead, it stores a reference or the memory address where the value is located.

# When you write:
a = 10

# Internally:
# 1. An object '10' is created in memory.
# 2. Variable 'a' points to the address of '10'.

Memory Address & Identity

Python provides built-in tools to inspect where objects are located in memory and whether two variables point to the same location.

The id() Function

The id() function returns a unique integer representing the memory address of the object.

x = 100
print(id(x))  # Output: A large unique number like 14072382

Identity Operators: 'is' vs '=='

OperatorChecksPurpose
==Value EqualityChecks if the content is the same.
isReference EqualityChecks if the memory address is the same.
a = [1, 2, 3]
b = [1, 2, 3]

print(a == b) # True (Same content)
print(a is b) # False (Different memory locations)

Memory Optimization (Interning)

Python uses a technique called Interning to save memory. For small integers (usually -5 to 256) and certain strings, Python reuses the same memory location for different variables with the same value.

# Same Value -> Same Memory
x = 10
y = 10
print(x is y) # True (Optimization at work)

# Different Value -> Different Memory
m = 500
n = 500
print(m is n) # False (Memory is not shared for larger numbers)

Mutable vs Immutable

How Python handles memory during updates depends on whether the object is Mutable (can change) or Immutable (cannot change).

1. Mutable Objects (Lists, Dicts, Sets)

The value can be changed without changing the memory address.

arr = [1, 2]
print(id(arr)) # Address X

arr.append(3)
print(id(arr)) # Still Address X (Address didn't change)

2. Immutable Objects (Int, Float, String, Tuple)

The value cannot be changed. If you "update" it, Python creates a brand new object in a new memory location.

age = 20
print(id(age)) # Address A

age = age + 1
print(id(age)) # Address B (New memory created!)

Garbage Collection

Python features automatic memory management. When an object in memory no longer has any variables pointing to it, it becomes "garbage."

x = [1, 2, 3] # Object created
x = None      # The list [1, 2, 3] now has 0 references. 
              # It will be removed by Garbage Collection.

Quick Revision Table

Summary of core memory concepts for quick reference and exam preparation.

ConceptMeaning
MemoryStorage for data/objects during runtime.
VariableA reference (pointer) to a memory address.
id()Function to check memory location.
isCheck if two variables share the same memory.
MutableObjects that can be modified in place.
ImmutableObjects that require new memory to "change".
Garbage CollectionAutomatic cleanup of unused memory.

Exam One-Liners:

What students Say?

Here are the stdudents...

#

Contact Us

Empower your tech dreams with us. Get in touch!

  • #
  • #
  • #
  • #
  • #