EasyExperience level: 0-2 years · 3-5 years2 min read

What is the difference between mutable and immutable types in Python?

Reviewed by Gurusankar M.

Asked inInfosysTCSAccentureCognizant
#mutable#immutable#data types#python basics#id
Report issue

Short Answer

Mutable objects can be changed in place after creation (list, dict, set); immutable objects cannot (int, float, str, tuple, bool, frozenset). Mutating an immutable 'value' actually creates a new object with a new identity. This affects how arguments behave and what can be a dict key.

On this page

Concept Made Simple

Coffee Chat Question

“What is the difference between mutable and immutable types in Python?”

Remember It Faster

Mind Map Answer

Immutable
int, float, str, tuple, bool, frozenset
Mutable
list, dict, set, most custom objects
Why it matters
hashing, dict keys, function args

Only immutable (hashable) objects can be dict keys or set members. Reassigning a variable is not the same as mutating the object it points to.

Learn by Doing

Hands-on Keyboard

python
s = "cat"
print(id(s))
s += "s"          # builds a NEW string
print(id(s))      # different id

nums = [1, 2]
print(id(nums))
nums.append(3)    # same list, changed in place
print(id(nums))   # same id
Output
two different ids for s; same id for nums

Think Beyond the Expected

What If?

Since strings are immutable, what does `s += 'x'` really do?

It doesn't modify the original string — Python creates a brand-new string object and rebinds `s` to it. The old string is left unchanged (and garbage-collected if nothing else references it). That's why building a big string in a loop with `+=` is slow; use ''.join(list) instead.

Real World

The mutable/immutable line explains countless 'why did my variable change?' bugs — sharing one list across objects mutates them all, while ints/strings feel safe because every change makes a new object.

Interviewer's Expectation

Keywords they're listening for:

  • immutable can't change in place
  • list/dict/set mutable
  • immutables are hashable
  • reassign ≠ mutate
  • affects dict keys & args

Common Mistakes

  • Thinking `+=` on a string edits it in place
  • Using a list as a dict key
  • Assuming assignment copies the object

Best Practices

  • Use immutables (tuple/frozenset) as keys/constants
  • Build strings with ''.join(), not += in loops
  • Be careful sharing mutable objects between instances

Follow-up Questions

  1. Which built-in types are hashable and why?
  2. Why can a tuple still 'change' if it holds a list?
  3. How does this relate to the default-mutable-argument trap?

Continue Learning with AI

Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.

Plain-language foundations

I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner.

Topic: Data Types (Python)
Interview question: "What is the difference between mutable and immutable types in Python?"

Please:
1. Explain the core idea in simple, plain language, using an everyday analogy.
2. Define any technical terms you use.
3. Walk through one small, concrete example.
4. Finish with a single sentence I can easily remember.

Keep the tone friendly and assume I'm new to this topic.
Open inChatGPTGeminiClaude

Was this answer helpful?

Affiliate links

Equipment for a comfortable, productive setup.

As an Amazon Associate I earn from qualifying purchases.