LearnViz CompSci
Home / Learn / Programming Fundamentals / Mutability

Intermediate Topic 27 of 54 Programming Fundamentals

Mutability

Whether an object can be changed after creation.

How it works

Mutable objects can be changed in place, while immutable ones cannot, so any change creates a new object. Immutability makes sharing safe, enables caching and hashing, and removes whole classes of concurrency bugs because no thread can alter the value. Mutability is efficient for large, frequently changing data because it avoids copying.

Step-by-step walkthrough

  1. a = [1, 2] creates a mutable list.
  2. b = a makes a second name for the same list.
  3. b.append(3) changes the object itself.
  4. So a shows [1, 2, 3] as well.
  5. Immutable values such as tuples and strings create a new object on every change.

Where it is used

Strings and tuples are often immutable, and lists and dictionaries are mutable. Immutable values are used for dictionary keys, configuration, message passing between threads and functional-style state management.

Common pitfall

Aliasing is the major danger: two names pointing to one mutable object means a change through one is seen by the other. Shallow copies share nested mutable objects. Default mutable arguments and shared class-level lists cause surprising state leaks. Copy deliberately or use immutable types.

Where it fits in the course

Mutability is topic 27 of 54 in Programming Fundamentals, rated Intermediate. It follows Pass by Value vs Reference. The next topic is Error Handling.

Practice and explore

How to study this topic

  1. Read the explanation above and restate the definition in your own words.
  2. Open the interactive lesson and answer its mini quiz.
  3. Read the common pitfall, then write a short example that avoids it.
  4. Check your understanding in the Programming Fundamentals quiz, and revisit the topic if you miss a question.

Frequently asked questions

What is Mutability?
Whether an object can be changed after creation. It belongs to Programming Fundamentals and is an intermediate-level topic.
Where do people use Mutability?
Strings and tuples are often immutable, and lists and dictionaries are mutable.
What is the most common mistake with Mutability?
Aliasing is the major danger: two names pointing to one mutable object means a change through one is seen by the other.
What should I learn before this topic?
Start with Pass by Value vs Reference, the topic just before it in Programming Fundamentals.
How can I practice it?
Answer the mini quiz in the interactive lesson, then take the Programming Fundamentals quiz.
Is Mutability hard to learn?
It is an Intermediate topic. Be comfortable with the Beginner topics in this area first, then read the common pitfall carefully, since it is where most mistakes happen.
How long does it take to learn Mutability?
Most learners can read the explanation and finish the interactive lesson in under an hour. Fluency comes from applying the idea, so plan a few short practice sessions with the quiz.
Which topics are related to Mutability?
Pass by Value vs Reference, Error Handling, Call Stack are closely connected. The full list is in the related topics section of this page.
What should I study after Mutability?
Continue with Error Handling, the next topic in Programming Fundamentals.

Related topics