+ 7
Why is int immutable and list mutable?
I was reading the book "Learning Python" and it says- 1. age = 42 2. age = 43 In the above the code, we are not changing the value of age, we are only changing the place where the name "age" points to. Reason given: "42" is of type int which is immutable. In the second line, we are creating a new object "43" of type int and assigning this to "age". It means that when we change the 'value' of int, it only changes assignment. But in the following code: 1. a = [1, 2, 3, 4] 2. a[0] = 5 Aren't we just changing the assignment of a[0]? Then why do we say that lists are mutable? Is there any other reason for calling them mutable or do they work differently (that is, lists don't point to int objects but do something different to refer to them)?
24 ответов
+ 17
when we say lists are mutable, it means that lists support assignment and we can change their contents.
for example:
a = [1, 2, 3, 4]
a[0] = 5
so "a" will be changed to:
a = [5, 2, 3, 4]
but you can't do this with tuples, string or integers.
some examples:
b = (1, 2, 3, 4)
b[0] = 5
print(b)
the output will be an error:
'tuple' object does not support item assignment
c = 'Hello'
c[2] = 'i'
print(c)
the output will be an error:
'str' object does not support assignment
d = 2345
d[1] = 4
print(d)
the output will be an error:
'int' object does not support assignment
and you say about age = 42 and age = 43:
when you say
age = 42
and then
age = 43
you are changing the assignment of age.
you are not changing the content of 42, you are changing the content of age.
if you say:
age[1] = 3
it causes an error because integers don't support assignment but when you say
age = 43
you are changing the content of age and assigning it to another number.
+ 4
Really good discussion here... If I might add...
The variable "a" is assigned a pointer reference to a memory location for the integer object containing the value 42. It is this object value in this memory address that is immutable.
In fact, any expression that evaluates to 42 will all end up pointing to the same shared memory space.
The same applies to other integer values.
https://code.sololearn.com/cfESf4Z0FVg2/?ref=app
+ 3
One advice: tag python.
Many great pythonistas here only answer questions with python tag.
You can't miss services of them ever!
Now, coming to your question.
Firstly value of int was 42, then you replaced it to 43. It don't means - it changed only 2 to 3 and 4 was as it was. It is 'replaced'.
Now, for list, you are changing the value of only first element. Except it list was as it was - it is changed. It isn't replaced by new list [5,2,3,4]
+ 3
For instance,
a = 42
b= a
Now both a and b are equal.
But if we change a, b isn't changed since ints are immutable.
a=3
print(b) #Output -> 42
This is not the case in lists.
a= [1, 2]
b=a
a[0] = 3
print(b) #Output -> [3, 2]
This is because lists are mutable.
The basic difference arises on how their values are updated. Updating an int is equivalent to assigning a new int value to the variable and hence the variable is made to point at a new memory location.
However, lists can be updated in place. Hence while updating a section of the list, the changes are made in the same memory location and the reference that links the variable and the memory isn't changed. This is why if multiple variables refer to the same list and one of them is updated (not reassigned), the others get updated as well.
Tuples are immutable and hence cannot be updated in place whereas dictionaries can be and hence are mutable.