I am new in python, and I just find something strange:
[3, 2, 1]
Strings are immutable. So you aren't actually changing
test. You are actually getting the
return of the
replace string method. To use this modified string, you have to create a new string, or simply replace the existing string with the new value.
>>> some_string = "abcd" >>> new_string = some_string.replace('a', 'x') >>> new_string xbcd
>>> some_string = "abcd" >>> some_string = some_string.replace('a', 'x') >>> some_string xbcd
The second example, the list is mutable, and you are performing an in place manipulation of the list. If you actually do this:
res = your_list.reverse()
res will actually be
None, because it doesn't return anything, it actually does it in place, which is why
test list will hold the new manipulation you performed.
Read this on immutable vs mutable types in Python.
Also, refer to the documentation here on the Data Model to further your understanding as well.