I know that tuples are immutable but the following code is not giving me any error:
tup=(1,2,3,4)
tup=()
print(tup)
Please can anyone help me understand why the statement 2 is not giving me any error?
CodePudding user response:
The reason why it's not giving you an error is because the variable tup
is a reference to the tuple (1, ,2, 3, 4)
and then becomes a reference to an empty tuple.
The tuple values are not modified.
CodePudding user response:
It's because line 2 isn't mutating the first tuple tup
. Instead, it's creating a completely new tuple, then assigning it to the variable name tup
. The first tup isn't being changed, it's being replaced.