Python: Mutable vs. Immutable

Everything in Python is an object . You have to understand that Python represents all its data as objects. An object’s mutability is determined by its type. Some of these objects like lists and dictionaries are mutable , meaning you can change their content without changing their identity. Other objects like integers, floats, strings and tuples are objects that can not be changed.

Strings are Immutable

Strings are immutable in Python, which means you cannot change an existing string. The best you can do is create a new string that is a variation on the original.

example
message = "strings immutable" message[0] = 'p' print(message)

Instead of producing the output "ptrings immutable", this code produces the runtime error:

TypeError: 'str' object does not support item assignment

Why are Python strings immutable?

Which means a string value cannot be updated . Immutability is a clean and efficient solution to concurrent access. Having immutable variables means that no matter how many times the method is called with the same variable/value, the output will always be the same. Having mutable variables means that calling the same method with the same variables may not guarantee the same output, because the variable can be mutated at any time by another method or perhaps, another thread, and that is where you start to go crazy debugging.

List is mutable

Mutable example
my_list = [10, 20, 30] print(my_list)
output
[10, 20, 30]
continue...
my_list = [10, 20, 30] my_list[0] = 40 print(my_list)
output
[40, 20, 30]

Tuple is immutable

Immutable example
my_yuple = (10, 20, 30) print(my_yuple)
output
(10, 20, 30)
continue...
my_yuple = (10, 20, 30) my_yuple[0] = 40 print(my_yuple)
output
Traceback (most recent call last): File "test.py", line 3, in < module > my_yuple[0] = 40 TypeError: 'tuple' object does not support item assignment
If you want to write most efficient code, you should be the knowing difference between mutable and immutable in python. Concatenating string in loops wastes lots of memory , because strings are immutable, concatenating two strings together actually creates a third string which is the combination of the previous two. If you are iterating a lot and building a large string, you will waste a lot of memory creating and throwing away objects. Use list compression join technique. Python handles mutable and immutable objects differently. Immutable are quicker to access than mutable objects. Also, immutable objects are fundamentally expensive to "change", because doing so involves creating a copy. Changing mutable objects is cheap.

Looking for a Python job ?

Chances are you will need to prove that you know how to work with Python. These Python Interview Questions have been designed especially to get you acquainted with the nature of questions you may encounter during your interview for the subject of Python Programming . Here are the top objective type sample Python Interview questions and their answers are given just below to them. These sample questions are framed by our experts team who trains for Python training to give you an idea of type of questions which may be asked in interview. Go to... Python Interview Questions