Why this output? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Why this output?

I can't understand why this result. https://code.sololearn.com/cQhUHQIMBnZb/?ref=app

18th Oct 2021, 5:05 AM
Martin Raj Kumar
Martin Raj Kumar - avatar
2 Answers
+ 3
"for i in list" means assigning "i" to every element in a list on each loop. Therefore "for a[-1] in a" means assigning "a[-1] = a[element]". In your code the list changes at every loop: 1st loop: [0,1,2,3,0] 2nd [0 1 2 3 1] 3rd [0 1 2 3 2] 4th [ 0 1 2 3 3] 5th [0 1 2 3 3] So at final loop "a[-1]" is 3
18th Oct 2021, 5:53 AM
Carlos
Carlos - avatar
+ 1
the a[-1] in the for loop becomes your variable for holding values in a
18th Oct 2021, 6:07 AM
Prabhas Koya