0

loop

Please explain to me, how the output of this: A=[1,2,3,4,1] for n in A: A[n]=0 print(A) is [0,0,3,0,1] ?

24th Feb 2019, 7:34 PM
Сима Гаджихалилова
Сима Гаджихалилова - avatar
4 Answers
+ 7
In first iteration, A[1] becomes 0. In second iteration, A[0] becomes 0. In third iteration, A[3] becomes 0. In 4th iteration, A[0] becomes 0. However, it was 0 before too. In 5th iteration, A[1] becomes 0. However, it was 0 before too. At Last, list A is like : [0,0,3,0,1]
24th Feb 2019, 8:08 PM
Arushi Singhania
Arushi Singhania - avatar
0
I’m sorry, I don’t get it :( Could you please explain this in other way?
24th Feb 2019, 8:30 PM
Сима Гаджихалилова
Сима Гаджихалилова - avatar
0
Посмотри вот этот код. Добавил выводы для наглядности. Проследи за тем, как меняется массив после каждой итерации A=[1,2,3,4,1] for n in A: print(n) print(A[n]) A[n]=0 print(A[n]) print(A)
24th Feb 2019, 10:34 PM
etogus
etogus - avatar
0
спасибо большое, я разобралась!)
24th Feb 2019, 10:35 PM
Сима Гаджихалилова
Сима Гаджихалилова - avatar