Interview Prep/Python for DevOps/Language essentials & functions

What is the difference between mutable and immutable types? Give examples.

BeginnerConceptLanguage essentials & functions
Answer

A mutable object can change in place after creation; an immutable one cannot, so any "change" creates a new object.

  • Mutable: list, dict, set, bytearray, most user-defined objects.
  • Immutable: int, float, bool, str, tuple, frozenset, bytes.

It matters because names are references. If two variables point at the same list, appending through one is visible through the other. Only hashable (in practice, immutable) objects can be dict keys or set members.

All Python for DevOps questions Roadmap: Python for automation

More on Language essentials & functions