Differences Between Python's Mutable and Immutable Types (Overview)
As a Python developer, you’ll have to deal with mutable and immutable objects sooner or later. Mutable objects are those that allow you to change their value or data in place without affecting the object’s identity. In contrast, immutable objects don’t allow this kind of operation. You’ll just have the option of creating new objects of the same type with different values.
In Python, mutability is a characteristic that may profoundly influence your decision when choosing which data type to use in solving a given programming problem. Therefore, you need to know how mutable and immutable objects work in Python.
In this video course, you’ll:
- Understand how mutability and immutability work under the hood in Python
- Explore immutable and mutable built-in data types in Python
- Identify and avoid some common mutability-related gotchas
- Understand and control how mutability affects your custom classes
00:00 Welcome to this Real Python course, Python’s Mutable vs Immutable Types. My name is Steven, and I’ll be guiding you through this topic over the coming lessons.
00:10 Perhaps you’ve heard the terms “mutable” and “immutable” already, or maybe it’s the first time you’re coming across these words. Either way, understanding the difference between mutable and immutable types and how to use these data types is an important part of Python programming.
00:26 In fact, it’s an important part of programming in any language because these are concepts that are common in other languages as well.
00:33
For example, when you first learned about tuples, have you ever asked yourself why you need tuples in Python when they’re so similar to lists? Here you can see a list called team
with a number of names, and then a tuple called team
.
00:47 And you can treat lists and tuples in similar ways. Not identical, but similar ways. So can’t you use lists every time and should you use lists every time?
00:57 And the answer is no. And in this course, we’ll see why sometimes you need to use tuples and your program requires you to use tuples.
01:07
And when you’ve compared string and list methods, you may have noticed some difference in their behavior. For example, if you have the same list team
as before, and then you append a new team member, let’s say Trevor has joined the team and you say, team.append("Trevor")
.
01:27
The list team
has changed. Trevor is added to the list. But then you try something similar with the string greeting
. In this case, it’s a string that says “Hello, Pythonistas”.
01:40
You then call greeting.upper()
. Notice the pattern—in the first example, you called team.append()
. In this case, it’s greeting.upper()
that follows the same pattern.
01:50
However, the variable greeting
did not change. Even though greeting.upper()
shows us the string in uppercase, it’s actually showing us a copy of the string.
02:02
The string itself has not changed. And you might think, why do these two methods I’m using them in the same way, why do they behave in a different way? One of them changes team
, team.append()
, the other one does not change greeting
, greeting.upper()
. The reason is mutability and immutability.
02:21 And at the end of this course, you’ll understand why these methods work in the way they do.
02:27 So before we get started, here’s what you can expect from this course. You’ll understand the difference between mutability and immutability. You’ll explore mutable, immutable built-in data types.
02:39 Python has several built-in data types. Some of them are mutable, some of them are immutable, and we’ll see which ones are which and why they behave in the way they do.
02:49 And you’ll also identify, and importantly avoid, some common gotchas you get when dealing with mutability. Let’s get started.
Become a Member to join the conversation.