So this blog explains how to write Python Program to Print the Fibonacci Series using 3 different methods often asked in interviews and exams. Fibonacci Series 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, the 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 the Fibonacci series in Python.
For all the practice Videos and Explanations on Python, please click over here. Python Practice Series.
Print Fibonacci Series in Python
There are various methods to print the Fibonacci Series in python. The most popular is 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 explanations 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
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
The above program is created to print the first 20 numbers in the Fibonacci Series. In Fibonacci’s first two numbers are fixed that is 0 and 1. So we declared two variables and initialized them with values 0 and 1. They 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 have 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 the third number is printed, we will assign the value of the first number second number to variable a, i.a a = b. And the new value in the series will be assigned to the 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 the 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
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 an integer type convertor(int()). Then declare two variables(a and b) with values 0 and 1; these are the first two numbers of the Fibonacci series. Print them using the print statement in python with the end character as space. Declare a variable i with initializing value as 0. This is nothing but a counter of your while loop.
In the while loop, write condition “i < n – 2”; this minus 2 is to compensate for the two numbers that we already printed 0 and 1. Inside the while, we will create the next number in the Fibonacci series, i.e. c = a + b. I hope now you can relate the source code with the definition of the 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.
Real-Life Applications / Usage of Fibonacci Series
The Fibonacci series has many applications, and you’re to explain the work on nature, or you can say the law of nature and the 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 a brief list Fibonacci Sequence’s Application
- Golden Ration
- Petals Ratio
- Fibonacci Spiral