Python Programming – Importing Module in Another Script

Let us assume, we need to compute the sum of the following series

\(\frac{1}{1 !}\)+\(\frac{2}{2 !}\)–\(\frac{3}{3 !}\)+…+\(\frac{n}{n !}\)

In this series,-the programmer needs to compute the factorial n times. Thus, we will reuse the already created module fact.py and import it inside another script that computes the sum of the above series. The program of the module is given in Code 7.2. It slightly differs from Code 7.1., in that, it returns the value of the computed factorial to the script written in Code: 7.3., where it has been imported. Note that this module is given in Code. 7.3. must be saved in the same directory where module fact.py is saved.

Code: 7.2. The illustration of module fact, which returns the value of computed factorial.

Code: 7.2. The Illustration of module fact, which returns the value of computed factorial.

#This program illustrates the designing/creating of a module

def factorial (n):
“This module computes factorial”|
f=1;
for i in range (1, n+1):
f=f*i;
return f

Code: 7.3. The computation of given series by importing fact module.

# To compute a series 1/1 !+2/2!+3/3!+…+n/n!

import fact
n=input(‘enter number:’)
n=int(n)
sum=0.0
for i in range (1, n+1):
sum=sum+(i/fact. factorial(i)) print( ‘ sum= ’, sum)

Output

enter number: 7
sum= 2.7180555555555554

In code 7.3., we see that the fact module is imported by using the import statement. Subsequently, fact.factorial(i) statement is used to call the factorial() function written inside the fact.py module. Finally, the computed sum is displayed by using the print statement.

Another example of a module is given in Code 7.4., which is used to display a Fibonacci series. Two functions have been defined in this module. First function fib() prints the values of the computed Fibonacci series. However, other function fibo() returns the computed Fibonacci series as a list to the calling script. The import in interpreter mode is displayed in Fig. 7.2. We can see that the Fibonacci module is imported by using the import command.

Then function fib() is called three times with arguments 1000, 100, and 10, and the intended result is obtained. The Python script importing and calling the module Fibonacci is given in Code: 7.5. In script mode, we see that the number n up to which the user wishes to display the Fibonacci series is asked from the user. Then n is passed as an argument to the appropriate function defined inside the module Fibonacci using the statement Fibonacci.fibo(n).

The Fibonacci module is imported in the script by the import statement. From the output, we see that the intended result is obtained.

It is to be noted that in the Fibonacci module two functions are defined fib() and fibo(). The function fib() is called in the interpreter mode to achieve the result, whereas fibo() is called in script mode. The user can call any of the defined function fib() and fibo() either in interpreter mode, i.e., at Python prompt or in script mode.

Code: 7.4. The illustration of module Fibonacci, used to print Fibonacci series upto n.

# To compute a Fibonacci series 0 1 1 2 3 5 8 11

def fib(n):
‘this prints the fibonacci series upto n’
a=0
b=1
c=0
while(c<n):
print(c, end=”)
c=a+b
a=b
b=c
return

deffibo(n):
‘ ‘this returns the fibonacci series as a list upto n’
a=0
b=1
c=0
fib series=[ ]
while(c<n):
fib_series.append(c)
c=a+b
a=b
b=c
return fib series

Python Programming - Importing Module in Another Script chapter 7 img 1

Python Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *