KEMBAR78
Modules in Python Programming | PPTX
MODULES
IN PYTHON PROGRAMMING
MODULES
 A python modules can be defined as a python program file.
 It contains – functions, class or variables.
 Modules in Python provide us the flexibility to organize the code in a logical way.
 Example:
def myModule(name)
print(“Hi!”, name);
 Save the above code with <filename>.py
LOADING A MODULE IN PYTHON CODE
 Python provides 2 types of statements:
 Import Statement
 From-import Statement
IMPORT STATEMENT
 Import statement is used to import all the functionality of one module into another.
 We can import multiple modules with a single import statement, but a module is loaded once regardless
of the number of times.
 Syntax:
import module1, modeule2, . . . . , module n
 Example:
import file
name = input(“Enter your name:”)
file.displayMsg(name)
FROM-IMPORT STATEMENT
 It provides the flexibility to import only the specific attributes of a module.
 Syntax:
from <module-name> import <name1>, <name2> . . .
 Calculation.py
def sum(a,b):
return a + b
def mul(a,b):
return a * b
def div(a,b):
return a/b
EXAMPLE
 Myprogram.py
from calculation import sum #it will import only the sum() from calculation.py
a = int(input(“Enter the first number”))
b = int(input(“Enter the second number”))
print(“Sum = “, sum(a,b))
IMPORT AS STATEMENT [RENAMING A MODULE]
 Python provides us the flexibility to import some module with a specific name so that we can use this
name to use that module in python source file.
 Syntax:
import <module-name> as <specific-name>
 Example:
import calculation as cal;
a = int(input(“Enter first number”))
b = int(input(“Enter second number”))
print(“Sum = “, cal.sum(a,b))
DIR( ) FUNCTION
 The dir( ) function returns a sorted list of names defined in the passed module.
 This list contains all the sub-modules, variables and functions defined in this module.
 Example:
import json
list = dir(json)
print(list)
RELOAD( ) FUNCTION
 If you want to reload the already imported module to re-execute the top-level code, python provides us
the reload( ) function.
 Syntax:
reload(<module-name>)
 Example:
reload(calculation)
PYTHON PACKAGES
 The packages in python facilitate the developer with the application development environment by
providing a hierarichical directory structure where a package contains sub-packages, modules, and sub-
modules.
 The packages are used to categorize the application level code efficiently.
EXAMPLE OF PACKAGE
 Library management system which contains three sub-packages as Admin, Librarian, and student. The
sub-packages contain the python modules.

Modules in Python Programming

  • 1.
  • 2.
    MODULES  A pythonmodules can be defined as a python program file.  It contains – functions, class or variables.  Modules in Python provide us the flexibility to organize the code in a logical way.  Example: def myModule(name) print(“Hi!”, name);  Save the above code with <filename>.py
  • 3.
    LOADING A MODULEIN PYTHON CODE  Python provides 2 types of statements:  Import Statement  From-import Statement
  • 4.
    IMPORT STATEMENT  Importstatement is used to import all the functionality of one module into another.  We can import multiple modules with a single import statement, but a module is loaded once regardless of the number of times.  Syntax: import module1, modeule2, . . . . , module n  Example: import file name = input(“Enter your name:”) file.displayMsg(name)
  • 5.
    FROM-IMPORT STATEMENT  Itprovides the flexibility to import only the specific attributes of a module.  Syntax: from <module-name> import <name1>, <name2> . . .  Calculation.py def sum(a,b): return a + b def mul(a,b): return a * b def div(a,b): return a/b
  • 6.
    EXAMPLE  Myprogram.py from calculationimport sum #it will import only the sum() from calculation.py a = int(input(“Enter the first number”)) b = int(input(“Enter the second number”)) print(“Sum = “, sum(a,b))
  • 7.
    IMPORT AS STATEMENT[RENAMING A MODULE]  Python provides us the flexibility to import some module with a specific name so that we can use this name to use that module in python source file.  Syntax: import <module-name> as <specific-name>  Example: import calculation as cal; a = int(input(“Enter first number”)) b = int(input(“Enter second number”)) print(“Sum = “, cal.sum(a,b))
  • 8.
    DIR( ) FUNCTION The dir( ) function returns a sorted list of names defined in the passed module.  This list contains all the sub-modules, variables and functions defined in this module.  Example: import json list = dir(json) print(list)
  • 9.
    RELOAD( ) FUNCTION If you want to reload the already imported module to re-execute the top-level code, python provides us the reload( ) function.  Syntax: reload(<module-name>)  Example: reload(calculation)
  • 10.
    PYTHON PACKAGES  Thepackages in python facilitate the developer with the application development environment by providing a hierarichical directory structure where a package contains sub-packages, modules, and sub- modules.  The packages are used to categorize the application level code efficiently.
  • 11.
    EXAMPLE OF PACKAGE Library management system which contains three sub-packages as Admin, Librarian, and student. The sub-packages contain the python modules.