How to Print Python Fibonacci series

In this python tutorial, you will learn about the Python Fibonacci series, we will see the Python program to print Fibonacci series and also we will check:

  • Python program to print fibonacci series
  • Python program to print fibonacci series using recursion
  • Python program to print fibonacci series using for loop
  • Python program to print fibonacci series up to n terms
  • Python program to print fibonacci series between 0 to 50
  • Python program to print fibonacci series using iteration
  • Python program to print fibonacci series without using recursion
  • Python program to print fibonacci series until ‘n’ value using recursion
  • Python program to print fibonacci series using while loop
  • Python program to print fibonacci series using function
  • Program to print first 50 fibonacci numbers in python
  • Python program to print fibonacci series in python using a list

Python program to print fibonacci series

Now, we will see python program to print fibonacci series.

  • In this example, I have used the function def fib(n)
  • We have initialized the first term to 0 and the second term to 1.
  • The for loop is used to iterate the values till the given number
  • At last, it will print fibonacci series

Example:

def fib(n):
    a = 0
    b = 1
    if n == 1:
        print(a)
    else:
        print(a)
        print(b)
        for i in range(2,n):
            c = a + b
            a = b
            b = c
            print(c)
fib(10)

To get the output, I have used print(c) to get the Fibonacci series. You can refer to the below screenshot for the output.

Python program to print fibonacci series
Python program to print fibonacci series

The above code, we can use to print fibonacci series in Python.

You may like, How to create a variable in python and Comment lines in Python.

Python program to print fibonacci series using recursion

Here, we will see python program to print fibonacci series using recursion.

  • In this example, I have used the function def Fibonacci(number)
  • Here, if (number == 0) check whether the given number is 0 or not. If it is true, the function returns the value zero.
  • elif (number == 1) check whether the given number is 1 or not. If it is true, the function returns the value one.
  • A recursive function Fibonacci() is used to calculate the n term sequence.
  • A for loop is used to iterate and calculate each term recursively.
  • We will take the input from the user

Example:

def Fibonacci(number):
           if(number == 0):
                      return 0
           elif(number == 1):
                      return 1
           else:
                      return (Fibonacci(number - 2)+ Fibonacci(number - 1))
number = int(input("Enter the Range Number: "))
for n in range(0, number):
           print(Fibonacci(n))

To get the output, I have used print(Fibonacci(n)) to get the fibonacci series using recursion. You can refer to the below screenshot for the output.

Python program to print fibonacci series using recursion
fibonacci series in python using recursion

The above code we can use to print fibonacci series using recursion in Python.

You may like, Python dictionary append with examples and Check if a list is empty in Python.

Python program to print fibonacci series using for loop

Let’s see python program to print fibonacci series using for loop

  • Firstly, we will allow the user to enter any positive integer.
  • We have initialized the First_val to 0 and the second_val to 1.
  • Here, for loop is used to iterate the number from 0 to user-specified number.
  • At last, print(next) is used to get the fibonacci series

Example:

num = int(input("Enter the Range Number: "))
First_val = 0
Second_val = 1
for n in range(0, num):
           if(n <= 1):
                      next = n
           else:
                      next = First_val + Second_val
                      First_val = Second_val
                      Second_val = next
           print(next)

To get the output, I have used print(next) to get the fibonacci series using for loop. You can refer to the below screenshot for the output.

Python program to print fibonacci series using for loop
Python program to print fibonacci series using for loop

The above code, we can use to print fibonacci series using for loop in Python.

Also read, Python Program to Check Leap Year.

Python program to print fibonacci series up to n terms

Here, we will see python program to print fibonacci series up to n terms

  • Firstly, we will allow the user to enter n terms
  • We have initialized n1 to 0 and n2 to 1.
  • If the number of terms is more than 2, we will use a while loop to find the next term in the sequence by adding the preceding two terms.
  • After that, we interchange the variable and continue with the process.

Example:

n = int(input("Enter the n terms: "))
n1, n2 = 0, 1
count = 0
if n <= 0:
   print("Please enter a positive integer")
elif n == 1:
   print("Fibonacci sequence upto",n,":")
   print(n1)
else:
   print("Fibonacci sequence:")
   while count < n:
       print(n1)
       r = n1 + n2
       n1 = n2
       n2 = r
       count += 1

To get the output, I have used print(Fibonacci sequence) to get the fibonacci series up to n terms. You can refer to the below screenshot for the output

Python program to print fibonacci series up to n terms
Python program to print fibonacci series up to n terms

This is the Python program to print fibonacci series up to n terms.

Also, read, How to convert list to string in Python and Python square a number.

Python program to print fibonacci series between 0 to 50

Now, we will see python program to print fibonacci series between 0 to 50

  • We have initialized n1 to 0 and n2 to 1.
  • Every next number is found by adding up the two numbers before it.

Example:

n1,n2 = 0,1
print(n1)
while n2<50:
    print(n2)
    n1,n2 = n2, n1+n2

To get the output, I have used print to get the fibonacci series between 0 to 50. You can refer to the below screenshot for the output.

Python program to print fibonacci series between 0 to 50
Python program to print fibonacci series between 0 to 50

The above Python code, we can use to print fibonacci series between 0 to 50 in Python.

Python program to print fibonacci series using iteration

Now, we will see python program to print fibonacci series using iteration.

  • In this example, I have used the function def fib(number).
  • We have initialized first as 0 and second as 1.
  • The while loop is used to find the next term in the sequence by adding the preceding two terms.
  • And then update the value of the first and second.

Example:

def fib(number):
   count = 0
   first = 0
   second = 1
   temp = 0
   while count <= number:
      print(first)
      temp = first + second
      first = second
      second = temp
      count = count + 1
fib(6)

To get the output, I have used print to get the fibonacci series using iteration. You can refer to the below screenshot for the output.

Python program to print fibonacci series using iteration
Python program to print fibonacci series using iteration

The above Python code, we can use to print fibonacci series using iteration in Python.

You may like to read, What is a Python Dictionary and Python print without newline.

Python program to print Fibonacci series without using recursion

Let’s see python program to print fibonacci series without using recursion.

  • Firstly, the user will enter the first two numbers of the series and the number of terms to be printed from the user.
  • Then print the first two numbers
  • The while loop is used to find the sum of the first two numbers and then the fibonacci series
  • Now, print the fibonacci series till n-2 is greater than 0.

Example:

n1=int(input("Enter the first number of the series: "))
n2=int(input("Enter the second number of the series: "))
n=int(input("Enter the number of terms needed: "))
print(n1,n2,end=" ")
while(n-2):
    t=n1+n2
    n1=n2
    n2=t
    print(n2,end=" ")
    n=n-1

To get the output, I have used print to get the fibonacci series without using recursion. You can refer to the below screenshot for the output.

Python program to print fibonacci series without using recursion
Python program to print fibonacci series without using recursion

The above Python code we can use to print fibonacci series without using recursion in Python.

Python program to print fibonacci series until ‘n’ value using recursion

Let see python program to print fibonacci series until ‘n’ value using recursion.

  • In this example, I have used the function def recursion_fib(num)
  • Here, if num <= 1 then return num
  • The other terms are obtained by adding the preceding two terms. The n term is the sum of (num-1) and (num-2) terms.
  • A recursive function recursion_fib is used to calculate the n term sequence.
  • A for loop is used to iterate and calculate each term recursively.

Example:

def recursion_fib(num):
   if num <= 1:
       return num
   else:
       return(recursion_fib(num-1) + recursion_fib(num-2))
n = 9
if n <= 0:
   print("Plese enter a positive integer")
else:
   print("Fibonacci sequence:")
   for i in range(n):
       print(recursion_fib(i))

To get the output, I have used print(recursion_fib(i)) to get the fibonacci series until ‘n’ value using recursion. You can refer to the below screenshot for the output.

Python program to print fibonacci series until 'n' value using recursion
Python program to print fibonacci series until ‘n’ value using recursion

The above code we can use to print fibonacci series until ‘n’ value using recursion in Python.

Python program to print fibonacci series using while loop

Now, we will see python program to print fibonacci series using while loop.

  • Firstly, the user will enter any positive integer.
  • We have initialized F_val as 0 and S_val as 1.
  • We have declared three integer variables like i, F_val, and S_val.
  • The while loop makes sure that the loop starts from 0 and is less than the user given number.
  • Within the while loop, we have used if and else.
  • And at last fibonacci series will get printed in the output.

Example:

Num = int(input("Enter the Range Number: "))
i = 0
F_val = 0
S_val = 1
while(i < Num):
           if(i <= 1):
                      Next = i
           else:
                      Next = F_val + S_val
                      F_val = S_val
                      S_val = Next
           print(Next)
           i = i + 1

To get the output, I have used print(Next) to get the fibonacci series using a while loop. You can refer to the below screenshot for the output.

Python program to print fibonacci series using while loop
Python program to print fibonacci series using while loop

The above code, we can use to print fibonacci series using while loop in Python.

Also read, Python For Loop with Examples and Python While Loop Example.

Python program to print fibonacci series using function

Here, we will see python program to print fibonacci series using function

  • In this example, we have used the function as def fib(n)
  • We have initialized the n1 to 0 and n2 to 1.
  • if n == 1 then print(n1)
  • The for loop is used to iterate the values till the given number
  • At last, it will print fibonacci series

Example:

def fib_series(n):
    n1 = 0
    n2 = 1
    if n == 1:
        print(n1)
    else:
        print(n1)
        print(n2)
        for i in range(2,n):
            t = n1 + n2
            n1 = n2
            n2 = t
            print(t)
fib_series(5)

To get the output, I have used print(t) to get the fibonacci series using function. You can refer to the below screenshot for the output.

Python program to print fibonacci series using function
Python program to print fibonacci series using function

Program to print first 50 fibonacci numbers in python

Let see program to print first 50 fibonacci numbers in python

  • In this example, I have used the function def FibonacciNum(n)
  • We have initialized the n1 to 0 and n2 to 1.
  • Here, if n < 1 then return
  • The for loop is used to iterate the values till the given number.
  • At last, call the function FibonacciNum(50) to get the first 50 fibonacci numbers in the output.

Example:

def FibonacciNum(n):
    n1 = 0
    n2 = 1
    if (n < 1):
        return
    print(n1, end=" ")
    for i in range(1, n):
        print(n2, end=" ")
        next = n1 + n2
        n1 = n2
        n2 = next
FibonacciNum(50)

You can refer to the below screenshot for the output of first 50 fibonacci numbers in python.

Program to print first 50 fibonacci numbers in python
Program to print first 50 fibonacci numbers in python

Python program to print fibonacci series in python using a list

Now, we will see python program to print fibonacci series in python using a list

  • Firstly, the user will enter any positive integer.
  • The for loop is used to iterate the values till the given number
  • At last, print(fib) to get the fibonacci series in the list.

Example:

n = int(input("Enter the number:"))
if n==0:
    fib=[0]
elif n==1:
    fib=[0,1]
else:
    fib=[0,1]
    for i in range(2,n):
        fib.append(fib[i-1]+fib[i-2])
print(fib)

To get the output, I have used print(fib) to get the fibonacci series using function. You can refer to the below screenshot for the output.

Python program to print fibonacci series in python using a list
Python program to print fibonacci series in python using a list

The above code we can use to to print fibonacci series using function in Python.

You may like the following tutorials:

In this Python tutorial, we have learned about the Python program to print Fibonacci series or Fibonacci Series in Python. Also, we covered these below topics:

  • Python program to print fibonacci series
  • Fibonacci series in python using recursion
  • Python program to print fibonacci series using for loop
  • Python program to print fibonacci series up to n terms
  • Python program to print fibonacci series between 0 to 50
  • Python program to print fibonacci series using iteration
  • Fibonacci series in python without recursion
  • Python program to print fibonacci series until ‘n’ value using recursion
  • Python program to print fibonacci series using while loop
  • Python program to print fibonacci series using function
  • Program to print first 50 fibonacci numbers in python
  • Python program to print fibonacci series in python using a list