As it is said that the data in a variable is changeable in a program, on the other hand, in data structure the strings, for example, are immutable. So here it is a contradiction between variable and data structure. Can anyone help me in solving this matter?
Asked
Active
Viewed 90 times
2 Answers
1
>>> name = 'E.RafatiNasr' # your name assigned to a 'name' variable
>>> name[0] = 'A' # I tried changing 'E' in your name to 'A'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
Oops! What happened?

Bikramjeet Singh
- 681
- 1
- 7
- 22
-
if you try to change straightly,it will be changed. for example you. we assign a value to a variable the we reassign it to different a value with the previous we did. so it gives the new output with a new given value and will not give an error. – E.RafatiNasr Apr 07 '19 at 05:51
-
Sorry, but how straightly? Do you mean this: >>> name = 'abc' and then, >>> name = 'def' – Bikramjeet Singh Apr 07 '19 at 05:57
-
"we assign a value to a variable the we reassign it to different a value with the previous we did". Yes, this is called Variable Assignment and is a concept different from string immutability. Please refer to: https://stackoverflow.com/questions/9097994/arent-python-strings-immutable-then-why-does-a-b-work – Bikramjeet Singh Apr 07 '19 at 06:14
-
so what is string immutability?can you give an example? – E.RafatiNasr Apr 07 '19 at 06:37
-
can you give an example about updating a variable and string immutability? – E.RafatiNasr Apr 07 '19 at 06:52
-
String Immutability simply means that once a variable (e.g. name), is assigned to a string (e.g. 'E.RafatiNasr'), that is: name = 'E.RafatiNasr', the elements of that string cannot be changed as long as this assignment, that is: name = 'E.RafatiNasr', is intact. The code example is in my answer above. On the other hand, name = 'A.RafatiNasr' means that 'name' variable is now assigned to new string value 'A.RafatiNasr' while the value 'E.RafatiNasr' is lost. The 'name' now points to location of 'A.RafatiNasr' in memory instead of 'E.RafatiNasr''s. Hope this clarifies. – Bikramjeet Singh Apr 07 '19 at 06:56
-
I think you mean assigning variable is valid to change several times in one memory but if we want to change a string according above example is not valid and we should consider another variable based on the previous one right? and assigning variable is different with updating a string right? – E.RafatiNasr Apr 07 '19 at 07:11
0
There is no contradiction. A variable isn't the value. To say that an object is mutable means that it exposes mutator methods. Variables are mutable when they can be reassigned.

juanpa.arrivillaga
- 88,713
- 10
- 131
- 172
-
I know the meaning of this statement " strings are immutable" . It means a string value cannot be updated. but why does the value of a variable is changed during a program. for example we assign a number to a variable then later we can change it with a string. what does it mean? – E.RafatiNasr Apr 07 '19 at 05:35
-
Again, I'm not sure what exactly you mean by the last sentence as there is no example provided by you but I assume you are confusing immutability with Variable Assignment? https://www.pluralsight.com/guides/python-basics-variables-assignment – Bikramjeet Singh Apr 07 '19 at 06:03
-
I can't see the link you sent to me so can you give an example about updating a variable and string immutability? – E.RafatiNasr Apr 07 '19 at 06:54
-
@E.RafatiNasr **because a variable is not a value**. When you re-assign a variable, the object may very well still exist, and it hasn't been mutated. – juanpa.arrivillaga Apr 07 '19 at 17:51