Here, in this post, I will be showing you how to write and program the Fibonacci Series also known as the Fibonacci Sequence in Python. But, before that, why not go through the basics of Fibonacci Sequence. What is it? How it came into existence? And then the ways to program it in Python.

Fibonacci Sequence — What is it??

The Fibonacci series is a sequence of numbers, where each subsequent number is the addition of the previous two numbers, starting with zero(0) and one(1).

Thus, the Fibonacci Sequence is like : 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55…

As a rule, the expression goes like this :

==> Xn = Xn-1 + Xn-2

How did it come into existence? —> The Fibonacci Spiral & Golden Ratio

The Fibonacci series or sequence is generally visualized in a graph. Each square depicts the area of the subsequent number in the series.  Hence, the Fibonacci spiral is then drawn inside the squares by connecting the various corners of the boxes.

Fibonacci Sequence in Python

The various squares fit together perfectly as the ratio between the different numbers in the Fibonacci sequence is quite very close to the amazing Golden Ratio, which is approximately equal to around 1.618034.

A fact to note, here, is the larger the numbers in the Fibonacci sequence, the closer is the ratio to the amazing Golden Ratio.

The spiral and resulting rectangle are also known as the Golden Rectangle

Now, that we have got to know what a fibonacci sequence or series is then, let’s move on to writing a Fibonacci Sequence in Python.

Fibonacci Sequence in Python

We already know that Fibonacci Sequence is a series in which each subsequent number is the sum of the previous 2 numbers. By default, the first two numbers of a Fibonacci series are zero(0) and one(1).

fibonacci deries in Python

SAMPLE INPUT:

### For a single line of output having Fibonacci series until 7 values

10

SAMPLE OUTPUT:

### Therefore, the output shall be :

0 1 1 2 3 5 8 13 21 34

Using a While Loop

  • First, input the value of ‘n’ for the number of terms for which Fibonacci sequence is to generate.
  • Then, initialize sum = 0, a = 0, b = 1 & count = 1.
  • while (count <= n)
  • Then, print sum
  • Now, increment the count variable
  • Then, swap a and b
  • Calculate sum = a + b
  • while (count > n)
  • Now, end the algorithm
  • Else
  • Continue to Repeat from steps 4 to 7

Python program for Fibonacci series until ‘n’ terms

/*  The Python program to generate and print the Fibonacci series until 'n' terms  */

n = int(input("Enter the number of terms: "))
a = 0
b = 1
sum = 0
count = 1
print("The required Fibonacci Series is : ", end = " ")
while(count <= n):
  print(sum, end = " ")
  count = count + 1
  a = b
  b = sum
  sum = a + b

Input:

Enter the number of terms :  10

Output:

The required Fibonacci Series is :

0 1 1 2 3 5 8 13 21 34

Now, Using Recursion

  • If the value of ‘n’ is 0, then, return 0
  • Else if, the value of ‘n’ is 1, then, return 1
  • Or else, then, recursively call the recursive function for the value of (n – 2) + (n – 1) for subsequent terms

Python program for Fibonacci series until ‘n’ terms using Recursion

/*    This program is used to generate a Fibonacci series using Recursion for 'n' terms   */

def Fibonacci_Sequence(n):
    if(n == 0):
        return 0
    elif(n == 1):
        return 1
    else:
        return (Fibonacci_Sequence(n - 2) + Fibonacci_Sequence(n - 1))

n = int(input("Enter the number of terms for Fibonacci Series : "))
print("The required Fibonacci Series is : ", end = ' ')
for n in range(0, n):
  print(Fibonacci_Sequence (n), end = ' ')

Input:

Enter the number of terms for Fibonacci Series : 8

Output:

The required Fibonacci Series is : 0 1 1 2 3 5 8 13

Finally, Using Lists

  • First, input the terms for which you want the Fibonacci Sequence for.
  • Then, call the function as many times.
  • Initialize an array ‘a’ .
  • If ‘n’ = 0 or n = 1, then, return 1
  • Otherwise, Initialize a[0] as 0 and a[1] as 1
  • Run the for loop in the range [2, n]
  • And, compute the value as a[i] = a[i-1] + a[i-2]
  • Finally, what you get is an array of Fibonacci numbers computed till ‘n’ terms.

Fibonacci series until ‘n’ terms using Lists

/*    This program is used to generate a Fibonacci series using Lists for 'n' terms   */

def fib(n):
    a = [0, 1]
    print("The required Fibonacci Series is : ",end= ' ')
    if n == 1:
        print('0')
    elif n == 2:
        print('[0,','1]')
    else:
        while(len(a) < n):
            a.append(0)
        if(n == 0 or n == 1):
            return 1
        else:
            a[0]=0
            a[1]=1
            for i in range(2, n):
                a[i] = a[i-1] + a[i-2]
            print(a)
            return a[n - 2]

num = int(input("Enter the number of terms for Fibonacci Series : "))
fib(num)
# Input
Enter the number of terms for Fibonacci Series : 12

# Output
The required Fibonacci Series is : 0 1 1 2 3 5 8 13 21 34 55 89

=> COMING TO A CONCLUSION

Through this article, I have tried to provide you with knowledge about what exactly is a Fibonacci Series or Sequence and how to write a program to print Fibonacci Sequence in Python. So, in writing this, I suppose I have made myself pretty clear. But, in case, you persist with any doubts, then, please feel free to write to me in the comments section and I am as always ever-ready to help you out with your queries and problems.

Fibonacci Sequence in Python

Until then bidding you Good-Bye !!! Ok, wait ….. before you go, you may check out my various other posts. Also, for the simple reason, that is, to enhance your knowledge on various other topics of importance. Where ??? Here……

Categorized in: