What are mutable and immutable data types in Python?

Python Mutable data types are those whose values can be changed in place whereas Immutable data types are those that can never change their value in place.

Here are Mutable data types :

  1. Lists
  2. Dictionaries
  3. Sets

And Immutable data types in Python:

  1. Integers
  2. Floating-Point numbers
  3. Booleans
  4. Strings
  5. Tuples

Python Mutable and immutable objects are handled differently. Immutable objects are quicker to access and expensive to change because it involves the creation of a copy. Whereas mutable objects are easy to change.

Examples of mutable and immutable data types in Python

Simple example code.

Mutable Data Types

The value assigned to a variable can be changed. Use it when there is a need to change the size of the data of the object.

color = ["Red", "Green", "Blue"]
print(color)

color[0] = "Black"
color[-1] = "White"
print(color)

Output:

mutable and immutable data types

Immutable Data Types:

The value assigned to a variable cannot be changed.

greeting = "Welcome to EyeHunts"

greeting[0] = 'Hello'
print(greeting)

Output:

What are mutable and immutable data types in Python

Do comment if you have any doubts and suggestions on this Python data type topic.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.