Python Programming - Creating a Module

Python Programming – Creating a Module

In this section, we will demonstrate the creation of a module. A module is created as a script file, which contains function definitions that can be called in two ways

  • From the interpreter
  • From another script file or from another function

Now, we shall discuss both the above ways of module importing in detail. As we know that factorial is the most frequently used operation and needs to be computed repeatedly in many programs. Here, we will create a module named fact.py, which computes the factorial of a number. Consider the program given in Code 7.1. This program contains only the definition of the function factorial( ), where n is passed as an argument.

Code: 7.1.
llustration of module fact containing definition of factorial()

# This program illustrates the designing/creation of a module

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

Python Tutorial

Leave a Reply

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