Page 452 - CITS - Computer Software Application -TT
P. 452
COMPUTER SOFTWARE APPLICATION - CITS
Modules
In Python, a module is a file containing Python definitions and statements. The file name is the module name
with the suffix .py appended. Within a module, you can define functions, classes, and variables, and you can also
include executable code.
Modules are used to organize code into reusable units. They help in organizing Python code logically and
efficiently. You can think of a module as a library or a toolbox that contains related functions and classes.
Here’s a basic example of a Python module:
In this example, ‘module.py’ is a module containing a ‘greet’ function, an ‘add’ function, and a variable ‘pi.’ To use
these definitions in another Python script or module, you can import them using the ‘import’ statement:
When ‘main.py’ is executed, it imports the ‘module’ module and then uses its functions and variables as needed.
Python also allows importing specific functions or variables from a module, rather than importing the entire module:
This way, only the specified functions and variables are imported into the current namespace.
Additionally, Python provides a special module called ‘__init__.py’ which can be used to initialize packages. This
module can contain initialization code or be left empty. It is automatically executed when the package is imported.
These are the basics of modules in Python. They play a crucial role in structuring and organizing Python code
into reusable and manageable units.
439
CITS : IT&ITES - Computer Software Application - Lesson 120 - 137 CITS : IT&ITES - Computer Software Application - Lesson 120 - 137