II Unit Modular Approach
II Unit Modular Approach
• Improved Organization:
Modules break down a large program into smaller, manageable parts, making it
easier to understand and navigate.
• Code Reusability:
Modules can be imported and used in multiple program parts or in different
projects, reducing code duplication.
• Namespace Management:
Each module has its namespace, preventing naming conflicts between different parts
of the code.
• Enhanced Maintainability:
Changes or bug fixes in one module are less likely to affect other parts of the
program, simplifying maintenance.
• Testability:
Individual modules can be tested independently, making it easier to ensure the
correctness of the code.
• Create Modules:
Divide the program's functionality into logical units and create separate Python files
(.py) for each module.
Within each module, define functions and classes that implement the module's
specific tasks.
IMPORTING MODULES IN PYTHON
Use the import statement to access the functions and classes defined in other
modules. There are several ways to use import:
This imports the entire module, and its contents can be accessed using dot notation.
For example:
import math
print(math.sqrt(25)) # Output: 5.0
This imports only the specified names from the module, and they can be used
directly without dot notation.
For example:
This imports the module with a different name, which can be useful for shortening
long module names or avoiding conflicts
For example:
import pandas as pd
df = pd.DataFrame({'a': [1, 2, 3]})
print(df)
print(pi)
Output
3.141592653589793
Output
3.141592653589793
720
Creating a Module
• Write Python code: Create a new file (e.g., my_module.py) and define the
functions, classes, or variables you want to include in your module.
Save the file: Save the file with a .py extension. This file is now a module.
Using a Module
• Import the module: In another Python script or interactive session, import the
module using the import statement.
Import specific items: You can import specific functions or classes from a module
using the from ... import ... statement.
Rename a module: When importing a module, you can rename it using the as
keyword.
1. Doctests
Doctests allow embedding test cases directly within the docstrings of functions,
classes, or modules. These tests are written like interactive Python interpreter
sessions, making them easy to understand and maintain.
Doctests are particularly useful for testing small code snippets and providing
executable documentation
2. Assert Statements
Assert statements are a simple way to check for expected outcomes within
your code. While not a comprehensive testing framework, they can be strategically
placed to validate assumptions during development.
Assert statements are helpful for catching errors early in the development process.
Using Modules
Modules allow grouping functions into separate files, promoting modularity and
reusability.
These functions can be used in another script after importing the module.
Modifying Strings:
Formatting:
Other Methods:
❖ len(): Returns the length of a string.
❖ str(): Returns a string representation of an object.
❖ encode(): Returns an encoded version of the string.
❖ translate(): Returns a translated string based on a translation table.
❖ maketrans(): Returns a translation table for use with the translate method.
UNDERSCORE IN PYTHON
Last expression result: In the interactive interpreter, the underscore holds the result
of the last evaluated expression
Naming conventions:
• Single leading underscore (_variable): Indicates a variable or method is
intended for internal use within a module or class (non-public). It's a
convention, not enforced by Python.