ls = [1,2,3,4]
for element in ls:
element+=1
#list isn't modified
for i in range(len(ls)):
ls[i]+=1
#list is modified
ls = [[1,2],[3,4]]
for element in ls:
element[0]=0
#list is modified
How would you teach young people learning programming that int/float/string are 'immutable' and Python passes them automatically by value and otherwise objects are passed by reference in for loops and functions?
Obviously they don't know about reference, mutability, dynamic allocation, and all the advanced stuff that has to do more with computer science than with pure algorithm.
Despite all its greatness, Python is very confusing for beginners, there's too much hidden magic for it to make sense. The list variable being just a reference to the beginning of the list for example, I remember struggling with that before I took a C++ class.