In this article, we’ll look at how to print a Fibonacci series in Python. To understand this example, you must be familiar with key Python programming concepts like Python while Loop and Python if…else Statement. For a bright career in software development, enrol in the leading Python training in Chennai with IBM Certification at SLA.
Fibonacci Series starts with the first two terms: 0 and 1. The two words before them are added to produce all other terms. This is to state the nth term is the sum of (n-1)th and (n-2)th term. The methods for iterating the Fibonacci series in Python are listed below.
1Fibonacci Series in Python – Using while loop
For printing the Fibonacci series in Python, a while loop will be used.
Step 1: Enter the number of values from which the Fibonacci sequence Python should be generated.
Step 2: Set the count to 0, the n 1 to 0, and the n 2 to 1.
Step 3: If n terms is less than 0
Step 4: print “error” since the number is invalid for the series.
Step 5: The value of n 1 will be printed if n terms = 1.
Step 6: While count n terms
Step 7: Print (n 1
Step 8: nth = n 1 + n 2
Step 9: Up to the needed term, we shall update the variable by changing n 1 to n 2, n 2 to nth, and so on.
1.1Example
An example of printing Fibonacci series in Python using while loop is shown below.
n_terms = int(input (“How many terms must be printed?”))
# First two terms
n_1 = 0
n_2 = 1
count = 0
# Now, we will determine whether the number of terms is valid or not
if n_terms <= 0:
print (“Please enter a positive integer, the given number is not valid”)
# if there is only one term, it will return n_1
elif n_terms == 1:
print (“The Fibonacci sequence of the numbers up to”, n_terms, “: “)
print(n_1)
# Then we will generate Fibonacci sequence of number
else:
print (“The fibonacci sequence of the numbers is:”)
while count < n_terms:
print(n_1)
nth = n_1 + n_2
# At last, we will update values
n_1 = n_2
n_2 = nth
count += 1
1.2Output
How many terms is it to print? 13
The Fibonacci sequence of the numbers is:
0
1
1
2
3
5
8
13
21
34
55
89
144
2Fibonacci Series in Python – Using Dynamic Programming
An example of printing Fibonacci series in Python using Dynamic Programming is shown below.
2.1Example
# number – Dynamic Programming
# Taking 1st two fibonacci numbers as 0 and 1
FibArray = [0, 1]
def fibonacci(n):
# Check is n is less
# than 0
if n < 0:
print(“Incorrect input”)
# Check is n is less
# than len(FibArray)
elif n < len(FibArray):
return FibArray[n]
else:
FibArray.append(fibonacci(n – 1) + fibonacci(n – 2))
return FibArray[n]
# Driver Program
print(fibonacci(9))
# This code is contributed by Saket Modi
2.2Output
34
3Fibonacci Series in Python – Using Space Optimized
An example of printing Fibonacci series in Python using Space Optimi0zed is shown below.
3.1Example
# Function for nth fibonacci
# number – Space Optimisation
# Taking 1st two fibonacci numbers as 0 and 1
def fibonacci(n):
a = 0
b = 1
# Check is n is less
# than 0
if n < 0:
print(“Incorrect input”)
# Check is n is equal
# to 0
return 0
# Check if n is equal to 1
elif n == 1:
return b
else:
for i in range(1, n):
c = a + b
a = b
b = c
return b
# Driver Program
print(fibonacci(9))
# This code is contributed by Saket Modi
# Then corrected and improved by Himanshu Kanojiya
3.2Output
34
4Fibonacci Series in Python – Using Cache
An example of printing Fibonacci series in Python using Cache is shown below.
4.1Example
from functools import lru_cache
# Function for nth Fibonacci number
# lru_cache will store the result
# so we don’t have to find
# fibonacci for same num again
@lru_cache(None)
def fibonacci(num: int) -> int:
# check if num is less than 0
# it will return none
if num < 0:
print(“Incorrect input”)
return
# check if num between 1, 0
# it will return num
elif num < 2:
return num
# return the fibonacci of num – 1 & num – 2
return fibonacci(num – 1) + fibonacci(num – 2)
# Driver Program
print(fibonacci(9))
# This code is contributed by Sayedul Haque Sarker
4.2Output
34
5Fibonacci Series in Python – Using Backtracking
An example of printing Fibonacci series in Python using Backtracking is shown below.
5.1Example
def fibonacci(n, memo={}):
if n <= 0:
return 0
elif n == 1:
return 1
elif n in memo:
return memo[n]
else:
memo[n] = fibonacci(n-1) + fibonacci(n-2)
return memo[n]
># Driver Program
print(fibonacci(9))
5.2Output
34
Time complexity: O(n)
Auxiliary Space: O(n)
6Bottom Line
The Fibonacci Sequence is an odd set of numbers that originated in classical mathematics and has since been used in advanced mathematics, biology, statistics, computer science, and agile development. Join our Python Training in Chennai at SLA to earn your IBM Certification and Best Practices certification.