Mutable and immutable types

Mutable types allow in-place modifications
>>> a = [1, 9, 3.2, 0]
>>> a[2] = 0
>>> a
[1, 9, 0, 0]
Types: list, dictionary, NumPy arrays, class instances
Immutable types do not allow in-place modifications
>>> s = 'some string containing x'
>>> s[-1] = 'y'  # try to change last character - illegal!
TypeError: object doesn't support item assignment
>>> a = 5
>>> b = a   # b is a reference to a (integer 5)
>>> a = 9   # a becomes a new reference
>>> b       # b still refers to the integer 5
5
Types: numbers, strings

previousnexttable of contents