Modules and Packages

Modules and Packages


Everyone gets confused between all these Functions, Modules, and Packages.


Just see this Image…….We understand that Package contains a collection of modules and a module contains a collection of Functions.

Thus, we can say Functions are the subset of modules and Modules are the subset of Packages.

Modules

A module is a Python file with a .py extension that can be imported into another Python program.


The name of the Python file becomes the module name.

The module contains:

1) definitions and implementation of classes 

2) variables 

3) functions that can be used inside another program.

Advantages of modules :

  • Reusability: Working with modules makes the code reusable.
  • Simplicity: provides a simple way to organize program components into a system; grouping similar forms of code into a module makes code easier to understand.


To create a function — A function is defined using the def keyword

1. Creating a Module

No alt text provided for this image


In this program, we have created 4 functions for adding, multiplying, subtracting, and division. Saving this file as Operations.py

2. Importing a Module

Importing the function using the import statement and typing the name of the module

No alt text provided for this image


To view or add a comment, sign in

Insights from the community

Others also viewed

Explore topics