The Fibonacci Sequence is a series of numbers named after Italian mathematician, known as Fibonacci. It is simply the series of numbers which starts from 0 and 1 and then continued by the addition of the preceding two numbers. In this article, you will learn how to write a Python program to implement the Fibonacci series using multiple methods. Below pointers will be discussed:
Let’s begin.
If you wish to learn Python and gain expertise in quantitative analysis, data mining, and the presentation of data to see beyond the numbers by transforming your career into Data Scientist role, check out our interactive, live-online Python Training.
What is Fibonacci Series?
Fibonacci series is a series of numbers formed by the addition of the preceeding two numbers in the series.
Example of Fibonacci Series: 0,1,1,2,3,5
In the above example, 0 and 1 are the first two terms of the series. These two terms are printed directly. The third term is calculated by adding the first two terms. In this case 0 and 1. So, we get 0+1=1. Hence 1 is printed as the third term. The next term is generated by using the second and third term and not using the first term. It is done until the number of terms you want or requested by the user. In the above example, we have used five terms.
Next, let’s write a Python program to implement it.
Python Program to implement Fibonacci Sequence
Implementing Fibonacci sequence in Python programming language is the easiest! Now there are multiple ways to implement it, namely:
- Using Loop
- Using Recursion
Let’s see both the codes one by one.
Fibonacci Series using Loop
Loops in Python allow us to execute a group of statements several times. Let’s write a python program to implement Fibonacci Series using a loop.
# Enter number of terms needed #0,1,1,2,3,5.... a=int(input("Enter the terms")) f=0 #first element of series s=1 #second element of series if a<=0: print("The requested series is ",f) else: print(f,s,end=" ") for x in range(2,a): next=f+s print(next,end=" ") f=s s=next
Output: Enter the terms 5 0 1 1 2 3
Another way to program the Fibonacci series generation is by using recursion. Let’s dig deeper into it.
Python Program to Write Fibonacci Sequence Using Recursion
Recursion is the basic Python programming technique in which a function calls itself directly or indirectly. The corresponding function is called a recursive function. Using a recursive algorithm, certain problems can be solved quite easily. Let’s see how to use recursion to print first ‘n’ numbers of the Fibonacci Series in Python.
Find out our Python Training in Top Cities/Countries
India | USA | Other Cities/Countries |
Bangalore | New York | UK |
Hyderabad | Chicago | London |
Delhi | Atlanta | Canada |
Chennai | Houston | Toronto |
Mumbai | Los Angeles | Australia |
Pune | Boston | UAE |
Kolkata | Miami | Dubai |
Ahmedabad | San Francisco | Philippines |
Python Code:
def FibRecursion(n): if n <= 1: return n else: return(FibRecursion(n-1) + FibRecursion(n-2)) nterms = int(input("Enter the terms? ")) # take input from the user if nterms <= 0: # check if the number is valid print("Please enter a positive integer") else: print("Fibonacci sequence:") for i in range(nterms): print(FibRecursion(i))
Output: How many terms: 5
0 1 1 2 3
Explanation: In the above Python program, we use recursion to generate the Fibonacci sequence. The function FibRecursion is called recursively until we get the output. In the function, we first check if the number n is zero or one. If yes, we return the value of n. If not, we recursively call fibonacci with the values n-1 and n-2.
This brings us to the end of this ‘Fibonacci Series in Python’ article. We have learned how to programmatically print the Nth Fibonacci number using either loop statements or recursion.
In Edureka’s live Python certification course, you will get to learn and use libraries like Pandas, Numpy, Matplotlib, Scipy, Scikit, Pyspark and master the concepts like Python machine learning, scripts, sequence, web scraping and big data analytics leveraging Apache Spark. The training comes with 24*7 support to guide you throughout your learning period.
Got a question for us? Please mention it in the comments section of this blog and we will get back to you as soon as possible or join our Master in Python programming course.