· A kgtotonne( ) to convert kg to tonnes
· A tonnetokg() to convert tonne to kg
· A kgtopound( ) to convert kg to pound
· A poundtokg()to convert pound to kg
In this article,we are going to create a module MassConversion.py that store function for mass conversion. We will store 4 different mass conversion function in it. What are they ?
They are as follows :
· A kgtotonne( ) to convert kg to tonnes
· A tonnetokg() to convert tonne to kg
· A kgtopound( ) to convert kg to pound
· A poundtokg()to convert pound to kg
What is module ?
A module is independent grouping of code and data. Data may be variables,definitions,statements and functions. We can use same module in another programs to perform the specific task. One module can be depend on other modules.
Python comes with some predefined modules that modules are called as standard library modules. For example,math,pandas,numpy,random are standard library modules. We can create our own module by simply creating functions for various task and store them in . py file
How to import entire modules?
We can import our newly created modules with the help of import statements. So import entire module,import statement can be used as follows:
Syntax:
import module1,module2,……….
What is the use of import statements?
The import statement internally execute the code of module of file and then make it available to your program. After importing a module, we can access all function definition and methods of imported modules.
Now lets see how to create MassConversion.py module
We simply open python text editor and write user defined function for each task given above. We will create 4 functions fro performing 4 task given above. After writing code, we will save this file as MassConversion.py
Here is a complete source code for MassConversion.py module
kg_in_tonnes = 0.001
kg_in_pound = 2.20462
def kgtotonnes(z):
kt = z*kg_in_tonnes
print(z,"kg is equal to",kt,"tonnes")
def tonnestokg(z):
tk = z/kg_in_tonnes
print(z,"tonnes is equal to",tk,"kg")
def kgtopound(z):
kp = z*kg_in_pound
print(z,"kg is equal to",kp,"pounds")
def poundtokg(z):
pk = z/kg_in_pound
print(z,"pounds is equal to",pk,"kg")
Now, we will import this module by using import statement
JUst open the python shell and type following statements

Below is snapshot of Source code

How to find details of user defined modules?
We can easily find all information of imported module by simply typing help() in focus shell in python
It tell us details information about imported module like function name,functions definitions present in modules,variables or data. It also provides details file path of imported module
