Python Fibonacci Module
write a python program to define a module to find fibonacci numbers and import the module to another program
Sure! Here's an example Python program that defines a module for generating Fibonacci numbers, and then imports that module into another program to use:
First, let's create a file called fibonacci.py and define our module in it:
def fibonacci(n):
//Returns the n-th Fibonacci number.
if n <= 0:
return 0
elif n == 1:
return 1
else:
return fibonacci(n-1) + fibonacci(n-2)
This module defines a single function, fibonacci(n), which takes an integer n as input and returns the n-th Fibonacci number. The implementation uses a recursive algorithm to generate the sequence.
Next, let's create another file called main.py, which will import the fibonacci module and use it to generate some Fibonacci numbers:
import fibonacci
# Generate the first 10 Fibonacci numbers
for i in range(1, 11):
print(fibonacci.fibonacci(i))
This program imports the fibonacci module and uses it to generate the first 10 Fibonacci numbers. The fibonacci.fibonacci(i) function call returns the i-th Fibonacci number, which is then printed to the console.
To run this program, simply execute the main.py file using the Python interpreter:
python main.py
This should output the following sequence of numbers:
1
1
2
3
5
8
13
21
34
55