write a python program to define a module to find fibonacci numbers and import the module to another program

In this article you will learn a Python program to define a module to find the fibonacci series and fibonacci of a number and then use this module in your program.

Let's try to learn these two variants of the Python program.

 

 

write a python program to define a module to find fibonacci numbers and import the module to another program



Fibonacci Series

write a python program to define a module to find fibonacci numbers and import the module to another program

Contents of fibonacci.py module

Create a new file named fibonacci.py and then define a function named fibonacci_series() by passing an integer as input to the Python function.

Take a new empty python list to store the fibonacci series up to n terms. Check if number n is negative if so return empty string.

If the number is positive use the else block to iterate up to n terms and calculate the Fibonacci series by  swapping A and B values as shown below

def fibonacci_series(n):
   a = 0
   b = 1
   my_list = []
   if n < 0:
       print("You Entered Negative Number ...")
       return my_list
   else:
       for index in range(n):
           my_list.append(a)
           a, b = b, a + b
       return my_list





Contents of main.py
===========================

In the main python file  import the  user defined fibonacci module.  Ask the user to enter an integer using the input function call and store it into a number variable.

Take a result variable and directly call the function fibonacci_series() function  by passing an even number as input argument.

Use the for loop to iterate all the fibonacci numbers present in the result and print the list item.

import fibonacci

number = int(input("Enter Value of n : "))
result = fibonacci.fibonacci_series(number)

print(f"Fibonacci Series up to {number} Terms")
for item in result:
   print(item, end=" ")




Output
====================

write a python program to define a module to find fibonacci numbers and import the module to another program

 






Extra Learning ....



Fibonacci of N Number
==============================

write a python program to define a module to find fibonacci numbers and import the module to another program

Contents of fibonacci.py module
=================================

This is another variant of this program To calculate the specific Fibonacci number of n.  In the fibonacci.py file  define a function named fibonacci_of_N()  by passing an input integer to it.

Check if  the number is negative or is equals to 0 or 1 print the result appropriately. Use the else block for positive number use the for loop to iterate from 2 to N, Calculate the value of C by adding a and b values and then slap the values of a and b.

def fibonacci_of_N(n):
   a, b = 0, 1
   if n < 0:
       print("Incorrect input")
   elif n == 0 or n == 1:
       return n
   else:
       for _ in range(2, n):
           a, b = b, a + b
       return b






Contents of main.py
===========================

In the main program import the user defined fibonacci module,  take a variable name, do number and ask a  user to enter an integer then call the defined function.

Take a result variable and store the return value of the function and use the print function to bring the result.

import fibonacci

number = int(input("Enter Value of n : "))
result = fibonacci.fibonacci_of_N(number)

print(f"Fibonacci of Number {number} is {result}")






Output
===============

write a python program to define a module to find fibonacci numbers and import the module to another program








 

 

 

Conclusion
=================

The above first python program will define two files fibonacci.py and main.py. fibonacci.py cContains a function to find the Fibonacci series of a given number.

In the main.py file,  will be importing the fibonacci module asking a user to enter an  integer variable and calling the fibonacci function present in the module.

Then finally print the result.



In the extra learning second python program, there also I have created two files fibonacci.py and main.py.

The specialty of this second program is it only prints the fibonacci number of the Nth Index.

Then in the main file we Import the user defined module and  call the required function and print the n fibonacci number.



Post a Comment

0 Comments