Can someone explain to me this code under in steps? Thank you! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Can someone explain to me this code under in steps? Thank you!

a = [0, 1, 2, 3] for a[-1] in a: print (a[-1])

25th Jul 2022, 9:54 AM
Srki
4 Answers
+ 4
this code will iterate through the list four times as there are four items and will print all the numbers except the last number as follow : 0 1 2 2 # not 3
25th Jul 2022, 11:05 AM
Aly Alsayed
Aly Alsayed - avatar
0
And what does line 2 and 3 do?
25th Jul 2022, 11:10 AM
Srki
0
Ahaa now I get it, thank you
25th Jul 2022, 11:11 AM
Srki
0
But why it prints out 2 two times?
25th Jul 2022, 11:12 AM
Srki