What is the difference between mutable and immutable types in Python?
β‘ 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.
βCoffee Chat Question
Concept Made Simple
βWhat is the difference between mutable and immutable types in Python?β
π§ Mind Map Answer
Remember It Faster
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.
β¨οΈHands-on Keyboard
Learn by Doing
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 idtwo different ids for s; same id for nums
π₯What If?
Think Beyond the Expected
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:
β οΈ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
- 1Which built-in types are hashable and why?
- 2Why can a tuple still 'change' if it holds a list?
- 3How does this relate to the default-mutable-argument trap?
π§©Related Technologies
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.
Was this answer helpful?
β Featured Products
Support our platform by exploring our recommended products.
As an Amazon affiliate, purchases through these links may earn us a small commission β at no extra cost to you. It helps keep Full Stack Interview Guru free.