0

Hi can you please explain to me this code line by line

def fib(n): """This is documentation string for function. It'll be available by fib.__doc__() Return a list containing the Fibonacci series up to n.""" result = [] a = 1 b = 1 while a < n: result.append(a) tmp_var = b b = a+b a = tmp_var return result print(fib(10) the result: (1,1,2,3,5.8.13) now i know this has been asked before but could anyone kind enough add a comment line by line what does it mean because i cant comprehend what does tmp_variable=b and then b=a+b and a=tmp_variable means. Could someone please tell me i need guidance on that

4th Apr 2019, 2:51 PM
Dwika Haryo Radithya
Dwika Haryo Radithya - avatar
1 Réponse
+ 6
Actually, tmp_variable = b means that tmp_variable will be pointing to what b is pointing. If b is pointing 1, it will also be pointing 1. b = a + b means that we are changing the value of b. Now, the value of b will be the value of a + the previous value of b. Note that the value of tmp_variable is the old value of b only. And a = tmp_variable means that now a is pointing to the value to which b was pointing before.
4th Apr 2019, 2:56 PM
Arushi Singhania
Arushi Singhania - avatar