What is the defference between foo = (1,2,3) and foo = [1,2,3] in python Can any body explain me the difference between them more clearly.
2 Answers
The first is a tuple which is an immutable type.
>>> foo = (1,2,3)
>>> foo[0] = 42
Traceback (most recent call last): File "", line 1, in TypeError: 'tuple' object does not support item assignment
The second is a list, which is mutable.
>>> foo = [1,2,3]
>>> foo[0] = 42
>>> foo
[42, 2, 3]
There are other very important differences between lists and tuples. Please see this question and its answers: