Python Guide Sidebar

Modules In Python

Introduction to Modules in Python

In Python, a module is simply a file containing Python code — functions, classes, or variables — that can be reused across different programs. As a result, modules help organize large programs into smaller, manageable files, making the codebase easier to read and maintain.

What is a Module in Python?

A module is a Python file with a .py extension that contains reusable code such as functions, variables, and classes. In other words, by using modules, developers can break down a large program into smaller parts and import only the required functionality into other programs.

How to Create a Module

Any Python file can act as a module. To create one, simply create a Python file and define functions, classes, or variables inside it.

For example:

# mymodule.py
def greet(name):
    print(f"Hello, {name}!")

Importing Modules in Python

To use a module, you need to import it into your script using the import statement.

import mymodule
mymodule.greet("Alice")

Built-in Modules in Python

Python comes with a rich standard library of built-in modules that provide useful functions and utilities. For instance:

  • math for mathematical operations
  • datetime for working with dates and times
  • os for interacting with the operating system

Here’s an example using a built-in module:

import math
print(math.sqrt(16))

Benefits of Using Modules

  • First of all, modules improve code reusability since you can write a function once and use it across multiple programs.
  • Moreover, they enhance organization, allowing large programs to be split into smaller, logical files.
  • In addition, they promote maintainability — any changes made in a module are automatically reflected in all programs that import it.
  • Finally, modules facilitate collaboration, as different team members can work on separate modules simultaneously, improving productivity.

Types of Modules

  • User-defined Modules: Created by the developer (e.g., mymodule.py).
  • Built-in Modules: Provided by Python (e.g., math, os).
  • Third-party Modules: Installed using pip (e.g., numpy, pandas).

Example Program Using Modules

# calculator.py (user-defined module)
def add(a, b):
    return a + b

def subtract(a, b):
    return a - b
# main.py (main program using calculator.py)
import calculator

print("Addition:", calculator.add(5, 3))
print("Subtraction:", calculator.subtract(5, 3))

Best Practices for Using Modules

  • To begin with, use meaningful names for modules to describe their functionality.
  • Next, group related functions together in a single module.
  • Also, avoid circular imports, which occur when two modules try to import each other.
  • Finally, use packages (folders with an __init__.py file) to organize related modules.

Comparison with Packages

Common Interview Questions

What is a module in Python? Explain with an example.

A module is a Python file containing reusable code such as functions or classes.

For example, a math_utils.py file with utility functions.

Companies: TCS, Infosys, Capgemini


How do you import a specific function from a module?

Use the from module import function syntax.

Companies: Wipro, Cognizant, HCL


What is the difference between a module and a package in Python?

A module is a single Python file, while a package is a collection of related modules within a directory.

Companies: Accenture, Amazon, Oracle


    Additional Resources

    Conclusion

    In summary, modules in Python allow developers to structure code into smaller, manageable, and reusable components. By learning how to create and use modules effectively, you can write cleaner, more maintainable Python programs while also promoting code reuse across projects.