How to Fibonacci Series In Python

each number is the sum of the two previous numbers. The first two numbers in the Fibonacci series are 0 and 1. Fibonacci series satisfies the following conditions −

Fn = Fn-1 + Fn-2

The beginning of the sequence is thus:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ……..

display the Fibonacci sequence up to n-th term

Example 1:

# Program to display the Fibonacci sequence up to n-th term

nseries = int(input("How many series? "))

# first two series
n1, n2 = 0, 1
count = 0

# check if the number of series is valid
if nseries <= 0:
print("Please enter a positive integer")
elif nseries == 1:
print("Fibonacci sequence upto",nseries,":")
print(n1)
else:
print("Fibonacci sequence:")
while count < nseries:
print(n1)
nth = n1 + n2

n1 = n2
n2 = nth
count += 1

Results

How many series? 7
Fibonacci sequence:
0
1
1
2
3
5
8

Python Fibonacci Series program Using While Loop

Example 2:

Number = int(input("\nPlease Enter the Range Number: "))

i = 0
First_Value = 0
Second_Value = 1

while(i < Number):
if(i <= 1):
Next = i
else:
Next = First_Value + Second_Value
First_Value = Second_Value
Second_Value = Next
print(Next)
i = i + 1


Fibonacci Series program Using For Loop

Number = int(input("\nPlease Enter the Range Number: "))

First_Value = 0
Second_Value = 1

for Num in range(0, Number):
if(Num <= 1):
Next = Num
else:
Next = First_Value + Second_Value
First_Value = Second_Value
Second_Value = Next
print(Next)

Python Fibonacci Series program Using Recursion

Example 3:

def ExampleOfFiboDemo_series(Number):
if(Number == 0):
return 0
elif(Number == 1):
return 1
else:
return (ExampleOfFiboDemo_series(Number - 2)+ ExampleOfFiboDemo_series(Number - 1))


Number = int(input("\nPlease Enter the Range Number: "))

for Num in range(0, Number):
print(ExampleOfFiboDemo_series(Num))

Sum of Fibonacci Numbers

Examples :

Input : n = 3
Output : 4
Explanation : 0 + 1 + 1 + 2 = 4

Input : n = 4
Output : 7
Explanation : 0 + 1 + 1 + 2 + 3 = 7

Python 3 Program to find sum of Fibonacci numbers

def calculateSum(n) :
if (n <= 0) :
return 0

fibo =[0] * (n+1)
fibo[1] = 1

# Initialize result
sm = fibo[0] + fibo[1]

# Add remaining series
for i in range(2,n+1) :
fibo[i] = fibo[i-1] + fibo[i-2]
sm = sm + fibo[i]

return sm


# Driver program to test
# above function
n = 4
print("Sum of Fibonacci numbers is : " ,
calculateSum(n))

# This code is contributed
# by Nikita tiwari.

I hope you get an idea about fibonacci series in python using list.


#py. #python 

How to Fibonacci Series In Python
1.00 GEEK