Immutable data types in Python | Basics

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, replace an element.

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

  • Tuple
  • Int
  • Float
  • Complex
  • Stringfrozen set [note: immutable version of 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 and suggestions on this Python Immutable object 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.