# Fibonacci Sequence In Python

## Iterative Approach and Recursive Approach to Solve Fibonacci Sequence

Hello, Python Enthusiasts!

In the last section of this Python Tutorial Series, we talked about *Recursive Approach To Solve A Problem In Python* and understood them with some examples. In this article, we will go a step further. We are going to deal with the Fibonacci Sequence in the Python programming language. It’s a very famous problem in the programming world. If you are an absolute beginner then it’s okay. We are going to understand the Fibonacci Sequence from a very beginner level. If you are following this Python Tutorial Series then it will be easy for you to understand.

# Fibonacci Sequence

Fibonacci Sequence is a sequence of integers. The first and second numbers in the sequence are 0 and 1. A subsequent term in the sequence is computed as the sum of immediately preceding two terms. It looks something like this:

`0,1,1,2,3,5,8,13,21,...`

## Iterative Approach To Find the nth Term Of Fibonacci Sequence

`def fibonacci(n):`

assert n>0

secondLast = 0

Last = 1

if n == 1:

print(secondLast)

elif n == 2:

print(Last)

else:

for i in range(3, n+1):

result = secondLast + Last

secondLast = Last

Last = result

print(result)

fibonacci(8)

fibonacci(7)

fibonacci(4)

The output of the code is:

`13`

8

2

In the function Fibonacci, we take *n* as a parameter. This n should be greater than 0. We all know that the last term of the Fibonacci sequence is the sum of two preceding terms. Keeping that in mind, we assign two variables, *secondLast* and *Last. *The Fibonacci sequence starts with 0 then 1, and so on. So We are using this logic in the if-elif-else control structure used above in the code. For the first two numbers of the sequence, we have 0 and 1. Next, from 3 to n+1, the else condition is going to take care of our input. Let suppose, we want to know the 8th term of the Fibonacci series. The for loop will iterate from 3 to 9(9 is excluded) times. The variable result is the summation value of the *secondLast* and *Last* variable’s values. Next, the value of the *Last* variable is assigned to the *secondLast* variable. Next, the value of the *result* is stored in the *Last* variable. And after successfully iterating over the for loop. We will print the *result*.

## Recursive Approach To Find the nth Term Of Fibonacci Sequence

def fibonacci(n):

assert n>0

if n == 1:

return 0

elif n == 2:

return 1

else:

return fibonacci(n-1) + fibonacci(n-2)print(fibonacci(8))

print(fibonacci(7))

print(fibonacci(4))

The output of the above code is the same as the output of the previous code:

`13`

8

2

In this Fibonacci function, we take n as a parameter. n should be greater than 0. If the input value of n is 1 and 2 then the output of the code is 0 and 1 respectively. Now in the *else condition *we just return the summation of the function itself with different parameter values. Understand this by letting a value of n. Let suppose the value of n is 4. Then the else condition returns the summation of fibonacci(3) and fibonacci(2). Again the value of fibonacci(3) is divided into two parts: fibonacci(2) and function(1). The value of fibonacci(1) is 1. And in this way, the whole problem breaks itself down into a simpler problem and then computes itself. After you run your code. You will get the same output. The best part of the recursive approach is that we have used fewer lines of codes as compared to the iterative approach to solving the same problem.

That’s it for this article. If I missed something then let me know in the comment section.

If this article sounds informative to you then make sure to follow and clap. Share it with your geek community.

Thanks for reading.

*Recursive Approach To Solve A Problem In Python*

*An Introduction to Dictionary in Python*

*An Introduction To Tuples In Python For Beginners*

*Built-in Functions on Python Strings*

*Break, Continue, and Pass Statements with Examples*

*Python while Loop with Examples*

*Python If-Elif-Else Conditional Statements with Examples*

*How to Use If and If-Else Statements in Python*

Hello, My Name is Rohit Kumar Thakur. I am open to freelancing. I build React Native projects and I am currently working on Python Django. Feel free to contact me at (freelance.rohit7@gmail.com).