Fibonacci Series in Python Using For-loop & Recursion

Fibonacci Series in Python

The Fibonacci Series in Python is a set of integers named after Fibonacci, an Italian mathematician. It’s simply a string of numbers that begins with 0 and 1 and continues with the addition of the two numbers before it.

Fibonacci Series in Python Example:

Fibonacci Series in python Example: 0, 1, 1, 2, 3, 5

The first two terms of the series are 0 and 1 in the example above. These two terms are directly printed. The third term is obtained by multiplying the first two terms together. In this scenario, 0 and 1 are the only options. As a result, we have 0+1=1. As a result, the third term is printed as 1. The second and third terms are used to generate the next term, rather than the initial term. It’s done till you get the number of words you desire or the user requests. We have utilized only five terms in the preceding example.

Fibonacci Series Program in Python:

The Fibonacci series in python is the simplest to implement in the Python programming language. It can now be implemented in a variety of ways, including:

  • Fibonacci series in python using for loop
  • Fibonacci series in python using recursion

Fibonacci Series in python using for Loop:

In Python, loops allow us to repeat the execution of a set of statements. Let’s go over how to use a for-loop to build the Fibonacci Series in python.

Step 1: Enter ‘n’ value until which the Fibonacci series has to be generated.

Step 2: Initialize sum = 0, a = 0, b = 1 and count = 1.

Step 3: while (count <= n)

Step 4: print sum

Step 5: Increment the count variable.

Step 6: swap a and b

Step 7: sum = a + b

Step 8: while (count > n)

Step 9: End the algorithm.

Step 10: Else

Step 11: Repeat from steps 4 to 7

Simple code of For-Loop in Fibonacci series in python:

u, v = 0, 1

for i in xrange(0, 10):

print u

u, v = v, u + v

The Fibonacci series has been printed between 0 and 10 using a simple for loop, as can be seen. The variables have been given new values within the for a loop. Fibonacci default beginning values of U and v, respectively, are 0 and 1.

As the loop runs, the new u value becomes the old v value, and the new v value becomes the sum of the old u and v values. This process continues until the range values reach the end of the range.

Fibonacci Series in python using Recursion:

The fundamental Python programming concept of recursion is when a function calls itself directly or indirectly. A recursive function is a name given to the related function. Certain issues can be solved quickly using a recursive approach. Let’s look at how to utilize recursion in Python to output the first n integers in the Fibonacci Series.

Below are the steps to use the recursion in Fibonacci Series.

Step 1: If ‘n’ value is 0, return 0

Step 2: Else, if ‘n’ value is 1, return 1

Step 3: Else, recursively call the recursive function for the value (n – 2) + (n – 1)

Simple code of Recursion in Fibonacci series:

#Through recursion

def fibonacci_ser(m):

if(m <= 1):

return m


return(fibonacci_ser(m-1) + fibonacci_ser(m-2))

m = int(input("Enter number of terms:"))

print("Fibonacci sequence:")

for i in range(m):

print fibonacci_ser(i),

To print the Fibonacci sequence, the function “Fibonacci ser” makes a call to itself.

As a result, the procedure is known as “recursion.”

Steps Followed here:

  • The user is prompted to enter the location where the Fibonacci series should be printed up to.
  • The function “Fibonacci ser” runs a number through it.
  • If the length provided is less than 1 or not, the condition is evaluated. If the answer is affirmative, the outcome is displayed right away.
  • If the length is larger than 1, however, recursive calls to “Fibonacci ser” with arguments shorter than 1 and 2 are made, i.e. Fibonacci ser(m-1) and Fibonacci ser(m-2) (m-2).
  • As a result, recursion produces the required result, which can then be printed.

Also Read: About Django Framework – A Complete Guide


After reading the preceding Fibonacci series in python, you should have a solid understanding of Fibonacci numbers and series, with a focus on Python. Once you’ve mastered the logic of the Fibonacci sequence, creating new series, dealing with other numbers, and using other approaches will become second nature to you. The only way to succeed in this is to take a logical approach.


How to calculate Fibonacci Series?

This number string is the foundation for all Fibonacci retracement levels. Dividing one number by the next number provides 0.618, or 61.8 percent, once the sequence has started. The result is 0.382, or 38.2 percent when you divide a number by the number on its right.

Does Fibonacci repeat?

After Every 24 numbers, the Fibonacci series repeats itself.

What was Leonardo’s Fibonacci method for discovering the Fibonacci sequence?

He noticed that the number of pairs of rabbits increased from 1 to 2 to 3 to 5 to 8 to 13, etc, after each monthly generation, and calculated how the sequence progressed by adding the previous two terms (in mathematical terms, Fn = Fn-1 + Fn-2), a sequence that could theoretically go on indefinitely.

Add a Comment

You have to agree to the comment policy.