Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Modules

• A module to be the same as a code library or a file that contains


a set of functions that you want to include in your application.
• Create a Python Module
# calc.py
# importing module calc.py
def add(x, y):
import calc
return (x+y)
print(calc.add(10, 2))
def subtract(x, y):
return (x-y)
import math
# functions.py import random
def display( ) : import functions # use function.py of previous program
print(‘VIT’) a = 100
b = 200
def show( ) : print(math.sin(0.5))
print(‘SCOPE') print(math.cos(0.5))
print(random.random( ))
print(random.randint(30, 45))
functions.display( )
functions.show( )
import math, random
If we wish, we can import specific names from a module

from math import sin, cos, tan


from functions import display # imports only display function
from functions import * # imports all functions
rename a module while importing it

import functions as fun


fun.display( )
#two.py

#one.py import one


one.printer("scope")
def printer(st): one.add(5,5)
print(f"The string is {st}") #one.py

def add(a,b): def printer(st):


print(a+b) print(f"The string is {st}")

printer("VIT") def add(a,b):


add(3,5) print(a+b)
if __name__='__main__’:
printer("VIT")
add(3,5)
The special variable, __name__ with "__main__", is the entry
point to your program. When Python interpreter reads the if
statement and sees that __name__ does equal to "__main__",
it will execute the block of statements present there.
Types of Python Modules

• There are two types of Python modules:


1.In-Built modules in Python
To display a list of all of the available modules in Python
help('modules')
2.User-Defined Modules in Python

You might also like