Back

Fibonacci series

Q1. Get the following code working:

1.    def fib(n):
2.    """ This prints a Fibonacci series up to n."""
3.        a = 0
4.        b = 1
5.       while a < n:
6.           print (a, end=" ")
7.           a, b = b, a + b
8.       print()
9.  
10.  fib(30)

Q2. Describe what the code does (what is a Fibonacci series)?
Q3. What is line 2 doing? What is it for?
Q4. Where does initialisation occur in the program?
Q5. Explain what is happening on line 7

      a, b = b, a + b

Q6. Replace line 7 with this code:

a = b
b = a + b

Does this still produce a Fibonacci series? Explain your answer.
Q7. Research the Fibonacci series. Why is important? Where does it occur?

Back