+ 3
why the results aren't the same ?
lst =[1,-2,3,-4] for num in lst: if num>0 : num = 0 print( lst ) ......... lst = [ 1, -2, 3, -4] for i in range(len(lst)): if lst [i] >0: lst[i] = 0 print(lst)
2 Antworten
+ 5
The first code does not change anything from the list.
num is a variable on its own, which might be changed in the if-branch, but it's not connected to the list, so no effect
0
thanks;)