You are currently viewing MUTABLE AND IMMUTABLE TYPES IN PYTHON

MUTABLE AND IMMUTABLE TYPES IN PYTHON

Hello!! My all dear Visitors!!, again, you’re most welcome in this domain (www.bittutech.com). I hope you all are very well and have enjoyed my previous article–

More links -:

This is the new interesting article for you to learn about a new Python Programming Language concept – Mutable and Immutable types.

So Let’s  again dive into the python pool.

The Python data objects can be broadly categorized into two –  Mutable and Immutable types, in simple words changeable or modifiable and non- modifiable types. Types

Immutable Types–:

The immutable types are those that can never change their value.

In Python, the following types are immutable:-

  • Integers
  • Floating point numbers
  • Booleans
  • Strings
  • Tuples

Mutable Types–:

The Mutable types are those whose value that can be changed.

Only three types are Mutable in Python:-

  • Lists
  • Dictionaries
  • Sets

👉 We should know about variables in python and also knowing about how variables works in Python Programming Language.

Python variables are not storage container rather Python variables are like memory references. They refer to the memory address is stored.

Depending upon the mutability/immutablitiy of the data type, a variable behave differently.

A variable is referring to an Immutable type then any change in its value, will also change the memory address it is referring to. If a variable is referring to Mutable type then any change in the value of mutable type will not change the memory address of the variable.

The Python id( ) function–:

Every object in Python has a distinct ID connected to the object’s memory address.    Use the id( ) function to read the special ID in Python.

Example–:

Num=10
Print (id(Num))

Output:-

Immutability in Python–:

As we already know, immutable types variables will never change its memory address while changing its values.

Let’s see an example to clarify that–:

Code–:

# Python program to check if x and y have the same i'ds after changing the value  
 
x = 3 
y = x 
 
# Changing the value of x 

x = 13 
 
# Checking if x and y still point to the same memory location 

print("x and y have the same ids: ", id(x) == id(y)) 

Output–:

x and y have the same ids :  False

As we can see here, ‘x’ and ‘y’ points different memory address.

Mutability in Python–:

As we know, Mutable types variables will change its memory address while changing its value.

Let’s again take an example to understand that–:

Code–:

# Python program to create two variables having the same memory reference  
 
# Initializing a variable 
x = 3 
 
# Storing it in another variable 
y = x 
 
# Checking if two have the same id reference
print("x and y have same id:", id(x) == id(y)) 

Output–:

x and y have same id :  True

Here, ‘x’ and ‘y’ both points the same memory address.

Examples of Immutable Data Types in Python–:

1. Integers–:

Code–:

# Python program to show that int is an immutable data type  
 
int_ = 25 
print("The memory address of int before updating:",
id(int_)) 
 
# Modifying an int object by giving a new value to it
int_ = 35 
print("The memory address of int after updating:",id(int_))

Output–:

The memory address of int before updating:  11531680
The memory address of int after updating:  11532000

2. Floating point numbers–:

Code–:

# Python program to show that float is an immutable data type  
 
float_ = float(34.5) 
print("The memory address of float before updating:", id(float_)) 
 
# Modifying the float object by giving a new value to it
float_ = float(32.5) 
print("The memory address of float after updating:",id(float_))

Output–:

The memory address of float before updating:  139992739659504
The memory address of float after updating:  139992740128048

3. Strings–:

Code–:

# Python program to show that a string is an immutable data type  
 
# Creating a string object 
string = "hello techies"
 
# Modify the string object 
string[0] = "X"
print (string)

Strings in Python are immutable data, that’s why we cannot add or edit any data.

Output–:

4. Tuples–:

Code–:

# Python program to show that a tuple is an immutable data type  
 
# Creating a tuple object 
tuple_ = (2, 3, 4, 5) 
 
# Trying to modify the tuple object 
tuple_[0] = 'X'
print(tulple_) 

Tuples in Python are immutable type data, so we are unable to add or modify any of their contents.

Output–:

Examples of Mutable Data Types in Python–:

1. Lists–:

Code–:

# Python program to show that a list is a mutable data type  

# Creating a list
list1 = ['Python', 'Java', 23, 'False', 5.3]
print("The original list: ", list1)

# Changing the value at index 2 of the list
list1[2]='changed'
print("The modified list: ", list1)

Output–:

The original list: [‘Python’, ‘Java’, 23, ‘False’, 5.3]
The modified list: [‘Python’, ‘Java’, ‘changed’, ‘False’, 5.3]

2. Dictionaries–:

Code–:

# Python program to show that a dictionary is a mutable data type  

# Creating a dictionary
dict_ = {1: "a", 2: "b", 3: "c"}
print("The original dictionary: ", dict_)

# Changing the value of one of the keys of the dictionary
dict_[2]= 'changed'
print("The modified dictionary: ", dict_)

Output–:

The original dictionary: {1: ‘a’, 2: ‘b’, 3: ‘c’}
The modified dictionary: {1: ‘a’, 2: ‘changed’, 3: ‘c’}

3. Sets–:

Code–:

# Python program to show that a set is a mutable data type  

# Creating a set
set_ = {1, 2, 3, 4}
print("The original set: ", set_)

# Updating the set using the update function
update_set = {'a', 'b', 'c'}
set_.update(update_set) print("The modified set: ", set_)

Output–:

The original set: {1, 2, 3, 4}
The modified set: {1, 2, 3, 4, ‘b’, ‘a’, ‘c’}

Cube of number 15 is 3375
Cube of number 16 is 4096
    

Last Words–: Thank you techies for your love and support. I hope you all are very happy and enjoy this blog post and also very excited to get something new. Don’t worry, we will meet very soon with a new and interesting blog post that absolutely enhance your technical skills and knowledge.

Share post

Prajjwal Singh

Tech Blogger || Web developer || Computer Networking Enthusiast || Microsoft SQL Database Management Expert || Software Debugger || Learned DOS OS Structure

Leave a Reply