Python Tutorial

Python Variable

Python Operators

Python Sequence

Python String

Python Flow Control

Python Functions

Python Class and Object

Python Class Members (properties and methods)

Python Exception Handling

Python Modules

Python File Operations (I/O)

What is a module, Python modular programming

A module in Python is a single file containing Python code that defines variables, functions, and classes. Modules help you to organize your code and promote reusability by encapsulating related functionalities together. Python supports modular programming, which encourages developers to create separate, independent modules that can be reused, combined, or shared with other developers.

Python Modular Programming

Modular programming is a software design technique that involves dividing a program into separate, independent modules. Each module focuses on a specific aspect or functionality of the program. This approach promotes code reusability, maintainability, and makes it easier to understand and manage large codebases.

In Python, modular programming is achieved using modules and packages:

  1. Modules: A module is a single file containing Python code. You can define functions, classes, and variables in a module, and then import and use them in other modules or scripts.

  2. Packages: A package is a collection of related modules organized in a directory hierarchy. Packages allow you to group and organize related modules together, making it easier to manage and distribute your code.

Creating a Module

To create a module, simply create a Python file (with a .py extension) and define your functions, classes, or variables inside it.

For example, create a file called my_module.py with the following content:

# my_module.py
def hello_world():
    return "Hello, World!"

def add(a, b):
    return a + b

Using a Module

To use the functions defined in a module, you can use the import statement followed by the module name (without the .py extension). Once the module is imported, you can access its functions using the dot notation.

For example, create a script called main.py and import the my_module:

# main.py
import my_module

print(my_module.hello_world())  # Output: Hello, World!
print(my_module.add(3, 4))      # Output: 7

You can also use the from ... import ... statement to import specific functions or classes from a module, so you don't need to use the dot notation:

# main.py
from my_module import hello_world, add

print(hello_world())  # Output: Hello, World!
print(add(3, 4))      # Output: 7

In conclusion, modular programming in Python is achieved using modules and packages, which help you organize, reuse, and share your code more effectively. By following modular programming principles, you can create more maintainable, readable, and scalable code.

  1. Creating and using modules in Python:

    • Description: Create a module by placing Python code in a separate file. Use the import statement to access the module's functions and variables.
    • Code: (example_module.py)
      # example_module.py
      def greet(name):
          return f"Hello, {name}!"
      
      # main_program.py
      import example_module
      
      result = example_module.greet("Alice")
      print(result)
      
  2. Dependencies and imports in Python modules:

    • Description: Modules can depend on other modules. Use the import statement to bring in external functionality. Handle dependencies by importing only what's necessary.
    • Code:
      # main_program.py
      import math
      
      result = math.sqrt(25)
      print(result)
      
  3. Encapsulation and abstraction in Python modules:

    • Description: Modules provide encapsulation by bundling related code together, hiding internal details. Abstraction is achieved by exposing only necessary information through functions and variables.
    • Code: (example_module.py)
      # example_module.py
      _internal_variable = "Hidden variable"
      
      def public_function():
          return "Accessible function"