Skip to content

Immutable data types in Python | Basics

  • by

Python Immutable data types are objects that cannot be modified and altered. This means after creating an object you can’t add new elements, remove an element, or replace an element.

Here is a list of immutable data types in Python are:

  • Tuple
  • Int
  • Float
  • Complex
  • Stringfrozen set [note: immutable version of the set]
  • Bytes

Example Immutable data types in Python

Simple example code immutable object can’t be changed after it is created.

Tuples are immutable

tup1 = (1, 2, 3)

tup1[0] = 4
print(tup1)

Output:

Immutable data types in Python

Strings are immutable

greeting = "Welcome to EyeHunts"

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

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

Do comment if you have any doubts or suggestions on this Python Immutable object topic.

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

Your email address will not be published. Required fields are marked *