Some objects are Immutable in Python. An immutable object’s value can’t change is possible over time. These are of in-built types like int, float, bool, string, Unicode, tuple data type are Immutable in Python.
In simple words, an immutable object can’t be changed after it is created.
Example immutable object in Python
Simple example code check that tuples are immutable. Immutable objects are quicker to access and are expensive to change because it involves the creation of a copy.
tup1 = (1, 2, 3)
tup1[0] = 4
print(tup1)
Output: Tuple object can’t be changed by seeing the below output you get a clear understanding.
What built-in objects are immutable in Python
Answer: Objects of the built-in type that are immutable are:
- Numbers (Int, Rational, Float, Decimal, Complex & Booleans)
- Strings
- Tuples
- Frozen Sets
- User-Defined Classes (depends upon the define the characteristics)
Do comment if you have any doubts or suggestions on this Python basics 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.