Mutable vs Immutable Types in Python
In Python, every value is either mutable (can change) or immutable (locked once created).
Immutable types can’t change the original
But Python can create a new value. I think of immutable types like sticky notes. You write one and stick it on the fridge. If you want to change the message, you don't edit the original note you replace it with a new sticky note. Same kind of note, but a new version.
word = " yay "
loud = word.upper()
quiet = word.lower()
swapped = word.replace("yay", "woohoo")
trimmed = word.strip()
fancy = word.title()
print(f"{word=}")
print(f"{loud=}")
print(f"{quiet=}")
print(f"{swapped=}")
print(f"{trimmed=}")
print(f"{fancy=}")
# Output:
# word=' yay '
# loud=' YAY '
# quiet=' yay '
# swapped=' woohoo '
# trimmed='yay'
# fancy=' Yay 'The string “yay” didn’t change. Python made a new “YAY”.
When we created word = "yay", Python carved out a spot in memory for that string. Then when we called word.upper(), Python didn't touch the original. It made a completely new string "YAY" in a different memory location. The variable loud points to this new string, while word still points to the original, untouched "yay".
This is what immutability means. Once created, the value stays fixed. You have to make new values rather than changing existing ones. Here are the common immutable types you'll work with:
Mutable types can be changed in place
I like to picture it like flowers in a vase in that the flowers change season to season. But the vase stays the same, and you swap out what’s inside.
flowers = ["sunflower", "cherry blossom"]
flowers.append("rose") # vase is the same, flowers changed
print(flowers) # ['sunflower', 'cherry blossom', 'rose']The mutable types commonly used:
The distinction
If it's a collection that holds multiple things (like lists, dictionaries, sets), it's usually mutable and can be changed in place. If it's a single value (numbers, strings), it's usually locked down and immutable.
When you need to work with them, mutable types are more efficient to update without creating new copies, while immutable types give you guarantees that the data won't unexpectedly change.
This distinction matters. Functions with mutable default arguments can behave in surprising ways. When you pass mutable objects to functions, the function might change the original object. Understanding which data types can change and which cannot helps you write more predictable code and avoid confusing bugs as your programs grow.



![A table showing common immutable Python data types: int (integers, example: 42), float (decimal numbers, example: 3.14), bool (True/False values, example: True), str (text strings, example: "hello"), tuple (ordered, fixed-size groups, example: (1, 2, 3)), frozenset (immutable sets, example: frozenset([1, 2])), and bytes (immutable binary data, example: b"data"). A table showing common immutable Python data types: int (integers, example: 42), float (decimal numbers, example: 3.14), bool (True/False values, example: True), str (text strings, example: "hello"), tuple (ordered, fixed-size groups, example: (1, 2, 3)), frozenset (immutable sets, example: frozenset([1, 2])), and bytes (immutable binary data, example: b"data").](https://substackcdn.com/image/fetch/$s_!ZpUm!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbe46ef46-f9a4-4ac4-97d0-d7807660341a_4424x2497.png)
![A table listing mutable data types in Python: list (ordered, changeable sequences, example: [1, 2, 3]), dict (key-value pairs like JSON, example: {"name": "Alice"}), set (unordered collections of unique items, example: {1, 2, 3}), bytearray (mutable version of bytes, example: bytearray(b"data")), and custom classes (objects you create, example: person("Alice")). A table listing mutable data types in Python: list (ordered, changeable sequences, example: [1, 2, 3]), dict (key-value pairs like JSON, example: {"name": "Alice"}), set (unordered collections of unique items, example: {1, 2, 3}), bytearray (mutable version of bytes, example: bytearray(b"data")), and custom classes (objects you create, example: person("Alice")).](https://substackcdn.com/image/fetch/$s_!yt5C!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faa59a0bd-95bb-4e99-9319-c32cb9fd5f8c_4424x2497.png)