Read Python Interview Questions and Answers for Beginners and Experienced

How to write Python Fibonacci Series with or without recursion

In this series of Python examples, we will study about Fibonacci series in Python.  we will see that how we can write Fibonacci series in Python. before proceeding further, let’s understand what is Fibonacci  series.

 

What is Fibonacci Series

Fibonacci is sequence of integers, where first and second integers are 0 and 1 and rest numbers are the sum of its previous two numbers. Let’s understand it by taking example

example: 0,1,1,2,3,5,8,13,21,….. and so on.

  • 0 and 1 are initial two numbers.
  • 3rd number 1 which is sum of its previous two numbers which are 0 and 1.
  • 4th number is sum of its previous two numbers 1 and 1 which is 2
  • similarly 5th number is 1+2=3  and so on other numbers.

 

I hope you understand now, let’s see how we can write this Fibonacci series in Python.

 

Fibonacci series in Python

There are couple of ways to print Fibonacci series in Python.

 

Fibonacci series in Python using recursion

Print Fibonacci series without using recursion

 

Fibonacci series in Python using recursion

In recursion Method, function calls itself again and again to solve problem.

 

Here We will also create Python recursion way to solve Fibonacci problem.

 


def fibonacci(n):
if n <= 1:
return n
else:
return(fibonacci(n-1) + fibonacci(n-2))

# take input from the user
items = int(input("How long you want to print fibonacci.\n Please enter only postive Integers:"))

print("Fibonacci sequence is:")
for i in range(items):
print(fibonacci(i))

 

Output

 

How long you want to print fibonacci.
Please enter only postive Integers:8
Fibonacci sequence:
0
1
1
2
3
5
8
13

Explanation:

  • In above example, we have created one function name fibonacci() which will take n as argument, n is the number till user want to print the fibonacci series.
  • items is variable which will take user input to ask from user how long fibonacci series will print.
  • After that, we have used Python for loop which will run till the number which user inputs and will call the fibonacci function.

 

Print Fibonacci series without using recursion

In above example, we learnt to print fibonacci series using recursion method. Here we will learn to print fibonacci without using recursion. In this example, we will print fibonacci series using for loop.

 


# Python Fibonacci series Program without using recursion

# Fibonacci series will start at 0 and travel upto below number
Number = int(input("Please Enter the Range for fibonacci series: "))

# Initializing First and Second number
n1 = 0
n2 = 1

# print Fibonacci series

if(Number ==0):
print(Number)
else:
for num in range(0, Number):
if (num == 1):
Next = num
else:
Next = n1 + n2
n1 = n2
n2 = Next
print(Next)

 

Output

 

Please Enter the Range for fibonacci series: 6
1
2
3
5
8

 

Explanation

  • n1 and n2 are first two numbers which are initialized with 0 and 1.
  • This program take the input from user to ask the range for fibonacci series.
  • If user enter 0 or 1 ,program will return 0 or 1 respectively.
  • if user enters other than  0 and 1 , else part will be executed which takes the logic of fibonacci series.