Fibonacci Series in Python – With Video

So this blog explains how to write Fibonacci Series in Python using 3 different methods often asked in interview and exams. Once is using for loop, while loop and one via using recursion.

We want to explain to you everything about the Fibonacci Series. But if you just want to understand the logic and how to write the code for Fibonacci Series below video is the best explanation for you.

And don’t worry if you want to understand more you can always scroll down to learn more about printing Fibonacci series in Python.

For all the practice Videos and Explanation on Python please click over here. Python Practice Series.

Video Explanation to Print Fibonacci Series in Python

Code: To Print Fibonacci Series in Python

There are various method to print the Fibonacci Series in python. The most popular being using for loop. But other methods include the Fibonacci series using a while loop and using recursion.

The above video explains only one method to print the Fibonacci series. But you can find source code and explanation of different methods over here.

Method 1: Fibonacci Series using For Loop in Python

Source Code and Output

#initlize two variables , with value 0
a,b = 0,1
print(a,b,end=' ')

for i in range(18):
  c = a + b
  print(c,end = ' ')
  a = b
  b= c
  

Output:

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181

Code Explanation Method 1: Fibonacci Series using For Loop

Above program is created to print first 20 numbers in the Fibonacci Series. In Fibonacci first two numbers are fixed that is 0 and 1.

So we declared two variables and initialize them with values 0 and 1. The printed the values and gave the end character as space. So that we can print the series horizontally.

Then start the for loop with the range as 18. Though we need to print 20 numbers, we need a loop to run only 18 times. Because the first two numbers in the Fibonacci series are fixed and we have printed them. Hence we just to worry about the 18 numbers afterwards.

Inside the for loop to calculate the 3rd Number of series we will sum up the first two numbers i.e c = a +b. Then print this new number.

Since third number is printed, we will assign the value of first number second number to variable a, i.a a = b

And the new value in the series will be assign to old second number. i.e. c = b.

Well now the loop will continue for 18 times and it will print 18 numbers. But we have already printed first two numbers which are fixed , so in total we got 20 numbers.

Method 2: Fibonacci Series using While Loop

Source Code and Output

n = int(input("Number of Fibonacci Series to be Printed"))
a,b = 0,1
print(a,b,end=' ')
i = 0
while(i < n-2):
  c = a + b
  print(c,end = ' ')
  a = bd
  b= c
  i = i + 1
  

Output

Number of Fibonacci Series to be Printed : 10
0 1 1 2 3 5 8 13 21 34 

Code Explanation Method 2: Fibonacci Series using While Loop in Python

The above code prints the Fibonacci Series using a while loop. Well, the logic is the same for every programming language. There could be small changes in syntax that differ from language to language.

In the second method, to print the Fibonacci series using a while loop in python we did a small change. In the earlier example, we just have written a program to print 20 Fibonacci series. But now we will write a program to print n numbers of the Fibonacci series. This means depending upon the input from the users.

So first we will accept the input from the users and then convert it into an int, using integer type convertor(int())

Then declare two variables(a and b) with value 0 and 1, these are the first two number of the Fibonacci series. Print them using the print statement in python with end character as space.

Declare a variable i with initialize value as 0. This is nothing but counter of your while loop.

In the while loop write condition “i < n – 2” , this minus 2 , is to compensate the two numbers that we already printed 0 and 1.

Inside the while we will create a the next number in Fibonacci series i.e c = a + b. I hope now you can relate the source code with the definition of Fibonacci Series.

Then print the new number of the series. Now we change the old numbers i.e a and b with the new numbers i.e b and c, using the assignment operators b = a and b = c.

And finally don’t forget to increment the counter , otherwise you will end up in the infinite loop and you have to shut down your kernel or restart the editor.

Invention/History of Fibonacci Series

Real-Life Applications/Usage of Fibonacci Series

Fibonacci series has many applications and are your to explain the working on nature or you can say law of nature and science behind it.

But the most important thing about Fibonacci Series is the Golden Ration. . Apart from the Fibonacci Series is used in Music and it is to explain the other aspects of nature.

Here is the brief list Fibonacci Sequence’s Application

  • Golden Ration
  • Petals Ratio
  • Fibonacci Spiral
  • Music

Facebook Comments
(Visited 1 times, 1 visits today)

Leave A Comment

Your email address will not be published. Required fields are marked *