Skip to content

Mutable in Python | Object Basics

  • by

Mutable in Python means objects to change their values. Some of the mutable data types in Python are list, dictionary, set, and user-defined classes. Mutable objects are easy to change.

Example Mutable in Python

Simple example code to test that lists are mutable. The use of mutable objects is recommended when there is a need to change the size or data of the object.

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

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

Output:

Mutable in Python

What Objects are the built-in type that is mutable?

Answer: These are of a type and custom classes are generally mutable.

  • Lists
  • Sets
  • Dictionaries
  • User-Defined Classes (It purely depends upon the user to define the characteristics)

Note: The tuple itself isn’t mutable but contains items that are mutable.

Do comment if you have any doubts or suggestions on this Python basic tutorial.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

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

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading