5
5
1. Module
2. Package
3. Library
Module:
-It is a file which contains python functions/global
variables/clases etc. It is just .py file which has python executable code
/ statement.
import * : can be used to import all names from module into current
calling module
import math
print (math.sqrt(25))
print (math.log10(100))
OR
from math import *
print(sqrt(25))
print(log10(100))
OR
from math import *
print(sqrt(25)) #It will work
def area_rect(length,breadth):
return length*breadth
def area_sqr(side):
return side*side
def area_circle(rad):
return math.pi*rad*rad
For example, if the imported module is area, now you want to call the
function area_circle(), it would be called as area.area_circle()
For example:
from math import sqrt
print(sqrt(25))
# numcheck.py
import math
def even(num):
if num%2==0:
return 1
else:
return 0
def isprime(num):
for i in range (2,int(math.sqrt(num)+1)):
if num%i==0:
return 0
return 1
def palindrome(num):
mynum=num
n=0
while num!=0:
r=num%10
n=n*10+r
num = num//10
if mynum ==n:
return 1
else:
return 0
Importing package and modules in another python program (xyz.py)
import mypackage.numcheck
n=int(input("Enter number"))
if(mypackage.numcheck.isprime(n)):
print("Number is prime")
else:
print("Number is composite")
OR
Library
It is a collection of various packages. Conceptually, there is no
difference between package and python library. In Python, a library is
used loosely to describe a collection of the core modules.
‘Standard library ‘of Python language comes bundled with the core Python
distribution are collection of exact syntax, token and semantics of the
Python language. The python standard library lists down approx more
than 200 such core modules that form the core of Python.
The Python installers automatically add the standard library and some
additional libraries.