DEV Community

Cover image for Python !!! Mutable and Immutable objects
sanin_mohd
sanin_mohd

Posted on

1

Python !!! Mutable and Immutable objects

Points To Remember

  1. Objects are stored in memory
  2. Variable are used to refer object
  3. Int, Float, String, Tuple, Set, list etc are inbuilt classes
  4. Int object stores integer type data

Example

  x = int(1) # int class constructor
  x = 1 
  # Both expressions are same
  # x is a pointer and 1 is an object
Enter fullscreen mode Exit fullscreen mode

Image description

Different types of objects in python

  1. Mutable objects

A mutable object is an object whose state can be modified
after it is created

List, dictionary, set and user-defined classes are mutable objects.

Example

Let's create a list

   x = [1,2,3,4,5]
Enter fullscreen mode Exit fullscreen mode

now, we print address id of x pointing

   print(x)
   print("Object address id x pointing : ",id(x))
Enter fullscreen mode Exit fullscreen mode

Run Program And See Output

[1, 2, 3, 4, 5]
Object address id of x : 140169509393728

now, we change first element of the list i.e

   x[0] = 9
Enter fullscreen mode Exit fullscreen mode

now, we print address id of x pointing

   print(x)
   print("Object address id of x : ",id(x))
Enter fullscreen mode Exit fullscreen mode

Run Program And See Output

[1, 2, 3, 4, 5]
Object address id of x : 140169509393728
[9, 2, 3, 4, 5]
Object address id of x : 140169509393728

Since list is mutable object, We can see that, before and after changing object, Both address ids are same._

  1. Immutable Objects

A immutable object is an object whose state can't be modified
after it is created
Int, Float, String, Tuple, decimal, bool and range are Immutable objects

Example

Let's create an String object "Hello"

 x = "Hello"
Enter fullscreen mode Exit fullscreen mode

"Hello" is an object of type string
x is a pointer to the object "Hello"

Now, let's see

 print(x[0]) #this code will print 0th index value "H"
Enter fullscreen mode Exit fullscreen mode

Run Program And See Output
H

Now, we change 0th index value "H" to "A"
let's see what happens

  x[0] = "A"     
Enter fullscreen mode Exit fullscreen mode

Run Program And See Output

H
Traceback (most recent call last):
File "main.py", line 3, in <module>
x[0]="A"
TypeError: 'str' object does not support item assignment

Here programs throws an error ,Telling that TypeError: 'str' object does not support item assignment, which means string is Immutable.
we can only create and delete immutable objects, we can't change immutable objects

One tip to boost up your knowledge

let's create another string object

  x = "HAPPY"
Enter fullscreen mode Exit fullscreen mode

now, we print address id of x pointing

   print(x)
   print("Object address id x pointing : ",id(x))
Enter fullscreen mode Exit fullscreen mode

Run Program And See Output

 HAPPY
 Object address id x pointing :  140210683580720
Enter fullscreen mode Exit fullscreen mode

here, we change x i.e

   x = "hAPPY"
Enter fullscreen mode Exit fullscreen mode

now, we print address id of x pointing

   print(x)
   print("Object address id of x : ",id(x))
Enter fullscreen mode Exit fullscreen mode

Run Program And See Output

 HAPPY
 Object address id x pointing :  140210683580720
 hAPPY
 Object address id of x :  140210683580592
Enter fullscreen mode Exit fullscreen mode

We can see that ,Both address ids are different, Which means here "HAPPY" is not changed to "hAPPY", But 2 separate string objects are created.

One Last Tip

let's create 2 variables with

  x = "HAPPY"
  y = "HAPPY"
Enter fullscreen mode Exit fullscreen mode

now, we print address id of x,y pointing

   print(x)
   print("Object address id x pointing : ",id(x))
   print(y)
   print("Object address id y pointing : ",id(y))
Enter fullscreen mode Exit fullscreen mode

Run Program And See Output

HAPPY
Object address id x pointing : 140210683580720
HAPPY
Object address id y pointing : 140210683580720

Here, both x and y address ids are same, which means both x,y are pointing towards the same object

Points To keep in mind

  1. Objects are stored in memory
  2. Variable are used to refer object
  3. Int, Float, String, Tuple, Set, list etc are inbuilt classes
  4. Int object stores integer type data

Quadratic AI

Quadratic AI – The Spreadsheet with AI, Code, and Connections

  • AI-Powered Insights: Ask questions in plain English and get instant visualizations
  • Multi-Language Support: Seamlessly switch between Python, SQL, and JavaScript in one workspace
  • Zero Setup Required: Connect to databases or drag-and-drop files straight from your browser
  • Live Collaboration: Work together in real-time, no matter where your team is located
  • Beyond Formulas: Tackle complex analysis that traditional spreadsheets can't handle

Get started for free.

Watch The Demo πŸ“Šβœ¨

Top comments (0)

PulumiUP 2025 image

PulumiUP 2025: Cloud Innovation Starts Here

Get inspired by experts at PulumiUP. Discover the latest in platform engineering, IaC, and DevOps. Keynote, demos, panel, and Q&A with Pulumi engineers.

Register Now

πŸ‘‹ Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay