From where this 2 came in output? Fibonacci series using python Lists
From where this 2 came in output? Fibonacci series using python Lists If you do not want to read this text, I have explained the problem in this Youtube video: https://youtu.be/Ekkkgjf0F_s Following is the code that I am using to generate Fibonacci series using python lists. list1 = [0, 1] x=1 while x <=2: length = len(list1) first =list1[length-2] second =list1[length-1] third = first + second list1.append(third) x+=1 print list1 When the while loop runs for the 1st iteration, it generates the upcoming element in series and stores in list exactly what it should do. The list will now become: list1 = [0,1,1] But what confusing me is when the second iteration is made by while loop. If you dry run the code, you will see that the code is outputting 2 (according to fibonacci series sequence, it is right) but if we dry run the code, the 4th element should be 3 instead of 2 2nd interation, length = 3>> Dry RUN below : 3-2=1 3-1=2 1+2=3 list1 should be: [0,1,1,3] Bu