+ 2
a = [1,1,2,3,5,8] for a[1] in a: Â Â pass print(a) result :[1, 8, 2, 3, 5, 8] want to know how index-1 is replaced as 8.
3 Answers
+ 12
Sreeraj V A
initially a is :[1, 1, 2, 3, 5, 8] ,
a iterates over a[1 ]and in this way a[1] value is changed at every iteration , so a is first replaced by 1, so result :[1, 1, 2, 3, 5, 8] ,
then a[1] is replaces by 2
:[1, 2, 2, 3, 5, 8] ,
, then it is replaces by 3
:[1, 3, 2, 3, 5, 8] ,
, then it is replaces by 5
:[1, 5, 2, 3, 5, 8] ,
then it is replaces by 8
:[1, 8, 2, 3, 5, 8] ,
; and it stays 8 after the loop has ended
So final result is :[1, 8, 2, 3, 5, 8] ,
a[::-1] is used to access last element in the list a so last element is 8 so it is give 8 as output
Do you want to know anything else
+ 3
Thank you so much... Great help
0
Took me awhile to work this one out, especially with it being fibinacci numbers, thought I was missing something there. the pass statment is just a red herring for a lot of programmers still starting out.