Types of objects in Python | with examples

Python is an Object-oriented programing language so everything in Python is an object. There are two types of objects in python i.e. Mutable and Immutable objects.

The difference between both types of objects is Simple, a mutable object can be changed after it is created, and an immutable object can’t.

Types of objects in Python

Understand with example.

Immutable Objects

The immutable object can’t be changed after it is created like Objects of built-in types like (int, float, bool, str, tuple, Unicode) are immutable.

These are quicker to access and are expensive to change because it involves the creation of a copy.

Check tuples are immutable or not?

tuple1 = (1, 2, 3)
tuple1[0] = 4

Output: TypeError: ‘tuple’ object does not support item assignment

Check strings are immutable

msg = "Hello"
msg[0] = 'Bye'


Types of objects in Python

Mutable Objects :

Custom classes are generally mutable for example list, dict, set. The use of mutable objects is recommended when there is a need to change.

Let’s see lists are mutable or not?

al = ["A", "B", "C"]

al[0] = "D"
al[-1] = "E"


[‘A’, ‘B’, ‘C’]
[‘D’, ‘B’, ‘E’]

Do comment if you have any doubts and suggestions on this Python object tutorial.

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.