Nearby lessons
21 of 108Python - Immutability
Introduction
Python provides several built-in data types to store different kinds of values.
Among them, five data types are called Fundamental Data Types.
These data types are used frequently in Python programs.
Fundamental Data Types
The five fundamental data types in Python are:
intfloatcomplexboolstr
These are the basic data types used to store values in Python.
Character Data Type
Python does not have a separate char data type.
A single character is also stored as a str object.
Example - Character Data Type
main.py
No output captured.
Long Data Type
In Python 2, there was a separate long data type.
In Python 3, the long data type has been removed.
All integer values, including very large numbers, are stored using the int data type.
What is Immutability?
An object is called Immutable if its value cannot be changed after it is created.
If you assign a new value, Python creates a new object instead of modifying the existing object.
Immutable Fundamental Data Types
The following fundamental data types are immutable:
intfloatcomplexboolstr
Once an object of these types is created, its value cannot be changed.
Example 1 - Integer Immutability
main.py
140731502731216 140731502731536
Observation
The memory address changes because Python creates a new object.
The existing integer object is not modified.
Example 2 - String Immutability
main.py
140731503210432 140731503211008
Observation
A new string object is created.
The previous string object remains unchanged.
Example 3 - bytes Immutability
main.py
10 TypeError: 'bytes' object does not support item assignment
Observation
Once a bytes object is created, its values cannot be changed.
The bytes data type is immutable.
Mutable Example
bytearray is almost the same as bytes.
The difference is that bytearray is mutable.
Its elements can be modified after creation.
Example - bytearray
main.py
bytearray(b'\x64\x14\x1e(')Difference Between Immutable and Mutable
| Immutable | Mutable |
|---|---|
| Object cannot be changed after creation. | Object can be changed after creation. |
| A new object is created when the value changes. | The same object is modified. |
Fundamental Data Types Summary
| Data Type | Immutable |
|---|---|
| int | ✔ Yes |
| float | ✔ Yes |
| complex | ✔ Yes |
| bool | ✔ Yes |
| str | ✔ Yes |
Important Notes
- Python has five fundamental data types:
int,float,complex,bool, andstr. - Python does not have a separate
chardata type. - A single character is stored as a
str. - Python 3 does not have the
longdata type. - All fundamental data types are immutable.
Key Points
- Fundamental data types are the basic data types in Python.
- Python stores characters using the
strdata type. - Python 3 uses only the
intdata type for all integer values. - Immutable objects cannot be modified after creation.
- When an immutable object's value changes, Python creates a new object.
bytearrayis mutable, whereasbytesis immutable.
Quick Summary
| Topic | Description |
|---|---|
| Fundamental Data Types | int, float, complex, bool, str |
| Character Type | No separate char data type |
| Long Type | Removed in Python 3 |
| Immutable Types | int, float, complex, bool, str |
| Mutable Example | bytearray |
| Immutable Example | bytes |