- Fibonacci series in Python [Complete program with 13 different examples]
- Python program to print fibonacci series using recursion
- Fibonacci series in python using for loop
- Python program to print fibonacci series until ‘n’ value using recursion
- Program to print first 50 fibonacci numbers in python
- Python program to print fibonacci series in python using a list
Fibonacci series in Python [Complete program with 13 different examples]
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
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.
The above code, we can use to print fibonacci series 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
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.
The above code we can use to print fibonacci series using recursion in Python.
Fibonacci series in python 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 in Python
Below is an example of fibonacci series in python using for loop.
num = int(input("Enter the Range Number: ")) First_val = 0 Second_val = 1 for n in range(0, num): if(n
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.
The above code, we can use to print fibonacci series using for loop in Python.
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.
n = int(input("Enter the n terms: ")) n1, n2 = 0, 1 count = 0 if n
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
This is the Python program to print fibonacci series up to n terms.
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.
n1,n2 = 0,1 print(n1) while 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.
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.
def fib(number): count = 0 first = 0 second = 1 temp = 0 while count
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.
The above Python code, we can use to print fibonacci series using iteration in Python.
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.
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.
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 defrecursion_fib(num)
- Here, if num 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.
def recursion_fib(num): if num
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.
The above code we can use to print fibonacci series until ‘n’ value using recursion in Python.
print fibonacci series in python 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.
Num = int(input("Enter the Range Number: ")) i = 0 F_val = 0 S_val = 1 while(i < Num): if(i
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.
The above code, we can use to print fibonacci series using while loop in Python.
fibonacci series in python 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
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.
Program to print first 50 fibonacci numbers in python
Is anyone asking you to write a python program to get the fibonacci series between 0 to 50? Let’s see the program to print the first 50 fibonacci numbers in python.
- In this example, I have used the function defFibonacciNum(n)
- We have initialized the n1 to 0 and n2 to 1.
- Here, if n < 1then 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.
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.
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.
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.
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
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.