Nearby lessons
28 of 159Python - Immutability
- Define immutability and what makes an object immutable
- Identify the five fundamental data types in Python
- Observe how reassignment creates new objects using id()
- Compare immutable types like bytes with mutable types like bytearray
- Explain why Python 3 has no separate char and long data types
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
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
Observation
The memory address changes because Python creates a new object.
The existing integer object is not modified.
Example 2 - String Immutability
Observation
A new string object is created.
The previous string object remains unchanged.
Example 3 - bytes Immutability
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
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 |
- An immutable object's value cannot be changed after creation
- The five fundamental data types are int, float, complex, bool, and str
- When an immutable value changes, Python creates a new object
- id() shows the memory address of an object
- bytearray is mutable, whereas bytes is immutable