Skip to main content

Python Fundamental Data Types And Immutability

What Are Fundamental Data Types?

Before we move into immutability, let us quickly recall the fundamental data types in Python:

  • int (Integer)
  • float (Floating-point)
  • complex (Complex numbers)
  • bool (Boolean)
  • str (String)

These are the building blocks of Python programming, and we have already studied them in detail earlier.

What is Immutability?

Let us first understand the term in a simple and intuitive way.

  • Mutable → Something that can be changed
  • Immutable → Something that cannot be changed

Now, in the context of Python:

An object is said to be immutable if its value cannot be changed after it is created.

Once an object is created, you cannot modify it. If you try to change it, Python creates a new object instead of modifying the existing one.

Are Fundamental Data Types Immutable?

Yes.

All fundamental data types in Python (int, float, complex, bool, str) are immutable.

This means:

  • You cannot change their value after creation
  • Any “modification” actually results in a new object

Understanding Immutability with an Example

Let us take a simple example:

x = 10
print(id(x))

x = x + 1
print(id(x))

Step-by-Step Explanation

  1. When you write:
x = 10
  • Python creates an object 10
  • x points to that object
  1. When you execute:
x = x + 1
  • Python does NOT modify 10
  • Instead, it creates a new object 11
  • Now x points to 11
  1. The original object 10 is left unchanged

Proof Using id()

The id() function gives the memory address of an object. If two values have different id(), they are different objects.

x = 10
print(id(x))

x = x + 1
print(id(x))

You will observe:

  • The first id(x) corresponds to 10
  • The second id(x) corresponds to 11
  • Both are different → which proves a new object was created

What Happens to the Old Object?

When x starts pointing to the new object (11), the old object (10) may no longer have any references. If an object has no references, it becomes eligible for garbage collection. Python’s garbage collector automatically removes such unused objects from memory.

Why Immutability Matters

Understanding immutability is not just academic; it has real-world benefits:

  • Safety: Immutable objects cannot be accidentally modified
  • Thread Safety: Useful in multi-threaded environments
  • Performance Optimization: Python can reuse immutable objects internally
  • Predictability: Code behavior becomes easier to reason about

Why Immutability is Required in Python?

Immutability allows Python to reuse objects efficiently, instead of creating new ones repeatedly.

This concept is known as object reusability, and it plays a central role in Python’s memory management. Immutability ensures that shared objects remain safe and consistent, even when multiple references exist.

##Understanding Object Reusability Let us consider a simple example:

a = 10
b = 10
c = 10

print(id(a))
print(id(b))
print(id(c))

What Do You Expect?

At first glance, you might think:

  • a = 10 → creates one object
  • b = 10 → creates another object
  • c = 10 → creates yet another object

So, total 3 objects.

What Actually Happens?

Python does not create three separate objects here. Instead, it creates only one object 10 and makes all variables (a, b, c) point to the same object.

This means:

  • id(a) == id(b) == id(c)
  • Only one object exists in memory
  • Three references point to it

Why Does Python Do This?

Because:

  • Creating objects is expensive (in terms of memory and CPU time)
  • Reusing existing objects is much faster and memory-efficient

Benefits of Object Reusability

  1. Memory Optimization

Instead of creating multiple identical objects:

  • Only one object is stored
  • Multiple references reuse it

This significantly reduces memory usage.

  1. Performance Improvement

Object creation is a costly operation in any programming language.

By reusing objects, Python avoids unnecessary creation overhead, which improves performance.

  1. Faster Execution Despite Lookup

You might wonder:

“If Python checks whether an object already exists before creating it, doesn’t that slow things down?”

Good question.

  • Search operation → relatively cheap
  • Object creation → expensive

So even if Python performs a lookup, the overall system remains faster because it avoids costly object creation.

Understanding is Operator

The is operator checks whether two variables refer to the same object in memory.

  • a is bTrue → both point to same object
  • a is bFalse → different objects