Industry Ready Java Spring Boot, React & Gen AI — Live Course
PythonVariable Storage and DataTypes

Variable Storage


In this lecture, we dive deeper into variables in Python, exploring how they are stored in memory, how Python manages variable references, the concept of garbage collection, and how to work with constants and data types.

Variables and Memory Address

Python treats variables as references, not containers. Instead of storing the value inside the variable, a variable simply points to an object stored in memory.

  • In Python, every variable points to a memory location where the actual data is stored.
  • To check the memory address of a variable, we use the built-in id() function.
a = 5
  • Python creates an integer object with value 5.
  • The variable a is a reference (label/tag) pointing to the memory location of that object.
  • Use id() to confirm memory address.
a = 5
print(id(a))   # Example: 140726264022056

Multiple variables referencing the same object

When assigning the value of one variable to another, both variables may point to the same memory location if their values are the same.

a = 5
b = 5

print(id(a))   # 140726264022056
print(id(b))   # Same as a

Python_Variables

Reassignment and New Memory Address

If a variable is reassigned a new value, it points to a new memory location.

b = 6
print(id(b))   # 140726264022088 (new address)

Python_Variables

  • a still point to 5
  • b now points to a new object 6
k = 5
print(id(k))   # 1407262640220568 (same as a)

Python_Variables

  • a and 'k' still point to 5
  • b now points to a new object 6

Another reassignment:

b = 9
print(id(b))   # 14072624022184 (another new address)

Python_Variables

  • b now points to 9
  • The old 6 object has no references

Assigning one variable to another copies the reference, not the object.

a = 10
b = a

id(a) == id(b)   # True

Both labels point to the same memory address.

If one is reassigned:

a = 20

A new object is created for a, but b still points to the original object.


Garbage Collection

Python uses garbage collection to automatically remove objects that are no longer referenced by any variable.

Example:

b = 6   # b → 6
b = 9   # b → 9, and 6 has no references now

Now the 6 object is unreferenced → Python’s garbage collector will eventually clean it.


String Interning (Memory Optimization)

Python stores some strings in a string pool to save memory.

Small, frequently used strings share the same memory:

name = "Muskan"
name1 = "Muskan"

id(name) == id(name1)   # True

Long strings may not be interned:

a = "My fav color is black"
b = "My fav color is black"

id(a) == id(b)   # Often True (interned) but not guaranteed

Python decides when to reuse string objects to balance memory vs. performance.

Large Numbers Do NOT Share Memory

Similarly, Python does not intern larger integers.

a = 1000
b = 1000

print(id(a))  # different
print(id(b))  # different

Each variable gets its own object in memory.


Constants in Python

  • Variables in Python can change values, but constants are intended to remain the same.
  • By convention, constants are written in uppercase letters.
PI = 3.14
print(PI)        # 3.14

# Though not enforced, changing it is possible:
PI = 3.15
print(PI)        # 3.15

Python does not enforce immutability of constants. The uppercase naming is just a convention.


Summary

  • Variables in Python are references to objects stored in memory.
  • Immutable objects like integers and strings may be shared due to optimization.
  • Reassigning a variable creates a new object in memory.
  • Unreferenced objects are automatically removed by Python’s garbage collector.
  • String interning and small integer caching reduce memory usage.
  • Constants are not enforced, only followed by naming convention.

Written By: Muskan Garg

How is this guide?

Last updated on