# The Fibonacci Series/ Sequence in Python – The Easiest Way

|In this Python example, we will discuss the different ways by which we can get the sequence of the Fibonacci Series. Let’s begin.

Table of Contents

## 1. What is Fibonacci Series/ Sequence?

The

– According to WikipediaFibonacci sequenceis one of the simplest and earliest known sequences defined by a recurrence relation, and specifically by a linear difference equation.

In mathematics, the *Fibonacci *Series is a series of numbers that is formed using the sum of the two preceding numbers.

Example:Input :7 #Value of the index till which the user want seriesOutput :1, 1, 2, 3, 5, 8, 13Input :12Output :1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144

Some of the topics which will be helpful for understanding the program implementation better are:

- Operators in Python
- Python I/O
- Recursion in Python
- If…Else… in Python
- While loop in Python
- Function in Python

Let’s implement different ways and understand each of them one by one.

## 2. Python Programs to Print The Fibonacci Series

There are different ways by which we can find the factorial of a number. We will go through each of the methods and see how they can be implemented.

### 2.1. How to Print The Fibonacci Series Using The Iteration Method

In the program, we have defined a function(`fib_series`

) that has certain conditions using the if…else… and the while loop to print the series.

# Python program to get the Fibonacci series def fib_series(n): n1, n2 = 0, 1 count = 0 if n < 0: print("Incorrect input, please try again!") elif n == 0: return 0 elif n == 1 or n == 2: return 1 else: print("Fibonacci sequence till the provided nth-term:") while count < n: print(n1, end=" ") nth = n1 + n2 n1 = n2 n2 = nth count += 1 if count == n: break # Main n = int(input("Enter the nth-term to display the Fibonacci Series : ")) fib_series(n)

OutputEnter the nth-term to display the Fibonacci Series : 12 Fibonacci sequence till the provided nth-term: 0 1 1 2 3 5 8 13 21 34 55 89

### 2.2. How to print N-th Term of Fibonacci using Iterative Method

In this method, we will print the nth term of the Fibonacci series rather than the whole series.

# Python Program to find nth fibonacci term in space efficient manner def fib(n): a1 = 0 a2 = 1 if n < 0: print("Input Incorrect, try again.") elif n == 0: return 0 elif n == 1: return a2 else: for i in range(1, n): a3 = a1 + a2 a1 = a2 a2 = a3 return a2 # Main n = int(input("Enter the nth term: ")) print("The nth term is:", fib(n))

OutputEnter the nth term: 22 The nth term is: 17711

### 2.3. How to print N-th Term of Fibonacci using Recursion method

In this program, we will implement the code to get the nth term using the recursion method

# Python Program to find nth fibonacci term using reursion def fib(n): if n < 0: print("Incorrect input") elif n == 0: return 0 elif n == 1 or n == 2: return 1 else: return fib(n-1) + fib(n-2) # Driver Program n = int(input("Enter the nth term: ")) print("The nth term is:", fib(n))

## 3. Conclusion

In this example, we have discussed the different ways by which we can find the Fibonacci series and the nth term using the recursion and iterative methods.

**Helpful Links**

Please follow the **Python tutorial series** or the **menu in the sidebar** for the complete tutorial series.

Also for examples in Python and practice please refer to **Python Examples**.

Complete code samples are present on Github project.

**Recommended Books**

**An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding**

**Do not forget to share and Subscribe.**

* Happy coding!! *😊