how to print fibonacci series in python code example

Example 1: how to create fibonacci sequence in python

#Python program to generate Fibonacci series until 'n' value
n = int(input("Enter the value of 'n': "))
a = 0
b = 1
sum = 0
count = 1
print("Fibonacci Series: ", end = " ")
while(count <= n):
  print(sum, end = " ")
  count += 1
  a = b
  b = sum
  sum = a + b

Example 2: fibonacci series in python

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

nterms = int(input("How many terms? "))

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

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

Example 3: fibonacci series in python

def iterativeFibonacci(n):
  fibList[0,1]
  for i in range(1, n+1):
    fibList.append(fibList[i] + fibList[i-1])
  return fibList[1:]

########################### Output ##################################

""" E.g. if n = 10, the output is --> [1,1,2,3,5,8,13,21,34,55] """

Example 4: python fibonacci sequence generator

number1 = 0
print('1:', number1)
number2 = 1
for count in range(2, 101):
  print(count, ':', number1 + number2)
  number1 += number2
  number2 = number1 - number2