Python is a versatile and widely-used programming language, but some of its core concepts can be tricky for beginners to grasp. One such concept is immutability, which plays a crucial role in how Python handles certain types of data, like strings, numbers, and tuples. In this article, we’ll explore what immutability means, how it affects your code, and why it matters, using a simple example to illustrate the concept in action.
What Are Immutable Objects?
In Python, an object is considered immutable if its value cannot be changed after it is created. Think of it like a piece of paper with something written on it in permanent ink—you can’t erase or modify the writing; you can only create a new piece of paper with a different value. In Python, the following types are immutable:
- Numbers (integers, floats, etc.)
- Strings (e.g.,
"Hello") - Tuples (e.g.,
(1, 2, 3))
On the other hand, objects like lists, dictionaries, and sets are mutable, meaning you can change their contents after creation (e.g., appending an item to a list).
When you try to “change” an immutable object, Python doesn’t actually modify the original object. Instead, it creates a new object with the updated value and updates the variable to point to this new object. This behavior can lead to some surprising results if you’re not aware of it, as we’ll see in the example below.
A Simple Example: Immutability with Strings
Let’s look at a short piece of Python code to see immutability in action:
name = "Bob"
another_name = name
name = "Robert"
print(another_name)
# Output: Bob (another_name still refers to the original "Bob")
Breaking Down the Code
Step 1: name = "Bob"
- Python creates a string object with the value
"Bob"and assigns the variablenameto point to it. You can think ofnameas a label that points to the string"Bob"in memory.
Step 2: another_name = name
- The variable
another_nameis set to point to the same string object thatnameis pointing to. At this point, bothnameandanother_nameare referring to the same"Bob"object in memory. There’s only one"Bob", but two variables are pointing to it.
Step 3: name = "Robert"
- Here’s where immutability comes into play. You might think this line changes the string
"Bob"to"Robert". However, because strings are immutable, Python cannot modify the original"Bob"string. Instead, it creates a new string object with the value"Robert"and updates the variablenameto point to this new object. The original"Bob"string remains unchanged in memory, andanother_nameis still pointing to it.
Step 4: print(another_name)
- When we print
another_name, it still points to the original"Bob"string becauseanother_namewas never updated to point to the new"Robert"string. As a result, the output isBob.
Why Does This Happen?
The key to understanding this behavior lies in the concept of immutability. Since strings are immutable, any operation that appears to “change” a string actually creates a new string object. In this case, when we assign "Robert" to name, Python creates a new string object and updates name to point to it, leaving another_name pointing to the original "Bob".
A Real-World Analogy
To make this clearer, let’s use an analogy. Imagine you have a piece of paper with the word “Bob” written on it in permanent ink, and you stick a label called name on that paper. Then, you stick another label called another_name on the same paper. Now, you decide to “change” the name to “Robert.” Since the paper is immutable (you can’t erase or modify the writing), you get a new piece of paper, write “Robert” on it, and move the name label to this new paper. The another_name label is still stuck on the original paper with “Bob” written on it. When you check what another_name is pointing to, you see “Bob.”
What Happens to the Original Object?
In the example above, after name is updated to point to "Robert", the original "Bob" string is still in memory because another_name is still referring to it. However, if there were no variables pointing to "Bob", Python’s garbage collector would eventually clean it up. Garbage collection is Python’s mechanism for freeing up memory by removing objects that are no longer needed. In this case, since another_name is still pointing to "Bob", the original string isn’t cleaned up yet.
Why Does Immutability Matter?
Understanding immutability is important for several reasons:
- Predictable Behavior: Knowing that immutable objects can’t be changed helps you predict how your code will behave. In the example above, if you expected
another_nameto print"Robert", you might be surprised by the actual output. Understanding immutability clarifies why this happens. - Memory Efficiency: Python can optimize memory usage with immutable objects. For example, if you create multiple variables with the same string value (e.g.,
"Bob"), Python can reuse the same object in memory rather than creating duplicates. - Avoiding Bugs: If you’re working with mutable objects like lists, you might accidentally modify an object in a way that affects other parts of your code. Immutable objects prevent this because they can’t be changed.
Immutability in Action: A Common Pitfall
Here’s another example to highlight a common mistake:
text = "Hello"
text[0] = "J" # This will raise an error!
You might think this code will change "Hello" to "Jello" by replacing the first character. However, because strings are immutable, you can’t modify them in place. This code will raise a TypeError with a message like 'str' object does not support item assignment. To achieve the desired result, you’d need to create a new string:
text = "Hello"
text = "J" + text[1:] # Creates a new string "Jello"
print(text)
# Output: Jello
Conclusion
Immutability is a fundamental concept in Python that affects how you work with certain data types like strings, numbers, and tuples. When you “change” an immutable object, Python creates a new object rather than modifying the original, which can lead to behavior that might seem surprising at first. By understanding immutability, you can write more predictable and bug-free code.
In the example we explored, we saw how assigning a new value to a variable pointing to an immutable string creates a new object, leaving other variables pointing to the original unchanged. This behavior is a direct result of immutability and Python’s memory management. As you continue your Python journey, keeping immutability in mind will help you avoid common pitfalls and make the most of Python’s design.
So, the next time you’re working with strings or other immutable objects, remember: you’re not changing them—you’re creating new ones!

Leave a comment