0% found this document useful (0 votes)
84 views6 pages

5

Python programs comprise three main components: modules, packages, and libraries. A module is a .py file containing functions and other Python code. A package is a directory containing one or more modules. A library is a collection of packages, and conceptually there is no difference between a package and library. The key aspects are: 1. Modules are individual .py files containing reusable Python code 2. Packages are directories containing modules 3. Libraries are collections of packages, and include both the Python standard library and additional third-party libraries

Uploaded by

YOUR's boy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views6 pages

5

Python programs comprise three main components: modules, packages, and libraries. A module is a .py file containing functions and other Python code. A package is a directory containing one or more modules. A library is a collection of packages, and conceptually there is no difference between a package and library. The key aspects are: 1. Modules are individual .py files containing reusable Python code 2. Packages are directories containing modules 3. Libraries are collections of packages, and include both the Python standard library and additional third-party libraries

Uploaded by

YOUR's boy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

A Python program comprises three main components:

1. Module
2. Package
3. Library

Module is a file containing Python definitions, functions, variables,


classes and statements

Python package is simply a directory of Python module(s)

Library is a collection of various packages. Conceptually there is no


difference between package and Python library. In Python a library is
used to loosely describe a collection of core or main modules

Module:
-It is a file which contains python functions/global
variables/clases etc. It is just .py file which has python executable code
/ statement.

To import entire module

import <module name>


Example: import math

To import specific function/object from module:

from <module_name> import <function_name>


Example: from math import sqrt

import * : can be used to import all names from module into current
calling module

To use function/constant/variable of imported module we have to


specify module name and function name separated by dot(.). This format
is known as dot notation.
<module_name>.<function_name>
Example: print(math.sqrt(25))

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

from math import sqrt


print(sqrt(25))
print(log10(100)) #It will not work

Create new python file(.py) and type the following code:


import math # this is first module
mynum=100

def area_rect(length,breadth):
return length*breadth

def area_sqr(side):
return side*side

def area_circle(rad):
return math.pi*rad*rad

Save this file as area.py


Execute the following code to import and use your own module
help() is used to get detailed information of any module:
Namesapces

In Python terms, namespace can be thought of as a named environment


holding logical group of related objects.

For every python module(.py), Python creates a namespace having its


name similar to that of module‟s name. That is, namespace of module
AREA is also AREA.

When 2 namespace comes together, to resolve any kind of object name


dispute, Python asks you to qualify the name of object as
<modulename>.<objectname>

Defined functions and variables in the module are now available to


program in new namespace created by the name of module

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()

When we issue from module import object command:


The code of imported module is interpreted and executed. Only the
asked function and variables from module are now available in the
current namespace i.e. no new namespace is created that’s why we can
call object of imported module without qualifying the module name

For example:
from math import sqrt
print(sqrt(25))

However if the same function name is available in current namespace


then local function will hide the imported module’s function same will be
apply for

from math import * method


Defining package
Step 1: Create a new folder which you want to act as package. The name
of folder will be the name of your package (“mypackage”)
Step 2: Create modules (.py) and save it in mypackage folder
(area.py and numcheck.py  mypackahge)
#area.py
import math # my module
mynum=100
def area_rect(length,breadth):
return length*breadth
def area_sqr(side):
return side*side
def area_circle(rad):
return math.pi*rad*rad

# 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

import mypackage.numcheck as mpack


n=int(input("Enter number"))
if(mpack.isprime(n)):
print("Number is prime")
else:
print("Number is composite")

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.

“Additional libraries” refer to those optional components that are


commonly included in Python distributions.

The Python installers automatically add the standard library and some
additional libraries.

The additional library is generally provided as a collection of packages.


To use such additional library we have to use packaging tools like
easyinstall or pip to install such additional libraries.

You might also like