Turn Messy Text into Structured Insights With AI. Join Greg Live:Β Dec 4th, 2024
Leverage
Glossary

Immutable

Immutable and mutable data types in Python

In Python, immutable means you are not able to edit something. Mutable means you are. This is due to the way the data types are created in Python. Don’t fear, this isn’t a blocker, there is always a solution if you run into a problem.

The common types of immutable data are int, float, bool, str, tuple.

my_string = "Data"
my_string[2] = "T

>>> TypeError: 'str' object does not support item assignment

Python Immutable

If you run into an immutable object, and you need to edit it, then you’ll need to change the data type. For example: change your tuple to a list or dict.

Let’s look at a quick code sample.

Link to code

On this page