0

Is it possible to have an explanation of this code plz ?

A = [1,2,3,4,1] for n in A: A[n] = 0 print (A) Why does it output [0,0,3,0,1] ?

25th Jul 2018, 8:21 PM
Ibrahim Maroquino
Ibrahim Maroquino - avatar
1 Odpowiedź
+ 2
starting A = [1,2,3,4,1] for loop goes through each value of A: #n = A[0] A[1] = 0 # A=[1,0,3,4,1] #n = A[1] A[0]= 0 # A=[0,0,3,4,1] #n = A[2] A[3]= 0 # A=[0,0,3,0,1] #n = A[3] A[0] = 0 # A=[0,0,3,0,1] #n = A[4] A[1]= 0 # A=[0,0,3,0,1] loop terminates
25th Jul 2018, 8:32 PM
JME