Python Core



Admission Enquiry Form


Python Modules

Modules in python

Python module is a file in python that contains python code like methods or functions, statements and classes etc. That code can be reused in any other file of the python by importing the module file.

Example:

Gmodule.py

#this is the module file gmodule.py
def sum():
num1=100
num2=20
res=num1+num2
print("The sum is ",res)
#now defining the second method

 

def subtract(n1,n2):
res=n1-n2
return res
#defining other method
#following method is taking default arguments
def multiply(n1=10,n2=5):
res=n1*n2
return res

Usemodule.py

#This is the usemodule file of python
# will import gmodule file here
import gmodule as gm
gm.sum()
gm.subtract(100,20)
ans=gm.multiply(7,8)
print("The value of ans is ",ans)
ans=gm.multiply()#using default arguments
print("The value of ans is ",ans)

 

ans=gm.multiply(n2=9,n1=5)#keyword arguments
print("The value of ans is ",ans)


Output

The sum is  120
The value of ans is  56
The value of ans is  50



Some built-in modules of python :

OS module

The OS module is given by python to provide you the methods to perform some jobs which can be performed by the operating system.We are using windows operating system so we shall use some windows commands through the OS module of python.

Example of OS module: