+ 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.

22nd Nov 2019, 6:03 AM
Sreeraj V A
Sreeraj V A - avatar
3 Réponses
+ 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
22nd Nov 2019, 6:49 AM
GAWEN STEASY
GAWEN STEASY - avatar
+ 3
Thank you so much... Great help
22nd Nov 2019, 7:27 AM
Sreeraj V A
Sreeraj V A - avatar
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.
27th Oct 2021, 12:23 PM
Michael Smyth