What students Say?
Here are the stdudents...
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.
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'.
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 returns a unique integer representing the memory address of the object.
x = 100
print(id(x)) # Output: A large unique number like 14072382
| Operator | Checks | Purpose |
|---|---|---|
| == | Value Equality | Checks if the content is the same. |
| is | Reference Equality | Checks 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)
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)
How Python handles memory during updates depends on whether the object is Mutable (can change) or Immutable (cannot change).
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)
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!)
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.
Summary of core memory concepts for quick reference and exam preparation.
| Concept | Meaning |
|---|---|
| Memory | Storage for data/objects during runtime. |
| Variable | A reference (pointer) to a memory address. |
| id() | Function to check memory location. |
| is | Check if two variables share the same memory. |
| Mutable | Objects that can be modified in place. |
| Immutable | Objects that require new memory to "change". |
| Garbage Collection | Automatic cleanup of unused memory. |
Here are the stdudents...
Empower your tech dreams with us. Get in touch!




