Python modules are self-contained Python files that include Python statements with definitions and instructions. This module may contain functions and variables that programmers can reuse in their other programs. To reuse the modules, Python needs to import them using the import statement.
Modules in Python are of two types:
In this article, you will learn about the different types of modules. Also, we will learn how to create and use user-defined modules.
Built-in modules in Python are those modules that programmers can use anytime they want. They come as a package with the Python interpreter. Some common examples of built-in modules are
- Random Module
- Sys Module
- OS Module
- Math Module
- Collections Module
- Statistics Module
There are other modules that get loaded automatically as the Python shell starts. All the functions within remain available such as the print() and input() for input-output operations. Again we can deal with the data type conversion functions using the int(), complex(), float(); and compound type conversions like tuple(), list(), set(), etc.
import statistics statistics.mean([4, 3, 6, 7, 9, 12, 15])
Advantages of Modules in Python:
There are various benefits of modules. Some of them are:
- Modularity: We can divide a program into multiple segments, which makes it easy to understand. Also, we can meaningfully divide the code as per the functionality.
- Reusability: If you create modules, you can easily make the code reusable. Also, you can share your code with others and gain portability as well.
- Simplicity: The module targets small proportions of a problem, rather than converging on the entire problem. It makes the code simpler and easy to understand.
- Scoping: Creating modules helps in defining functionality with different operations. Also, this helps in avoiding collisions between identifiers with the same name, hence, lead to module-based scoping.
Create User-defined module:
You can consider Python modules as libraries of other programming languages. Creating a user-defined module in Python is very easy. You have to write a Python program, perform some functions and save the file with the .py extension. You can use that module name to import it into any other program. It will give you the reusability feature.
def function-name(parameter1, parameter2, …., parameterN): function-body
Same the module name as regular Python program. For example, module_name.py
Importing Modules in Python program:
Programmers can import the entire code and definition of any previously-written user-defined module using the import statement. To perform this, programmers have to use the import keyword followed by the module name. Programmers can also import multiple modules at a time using the comma punctuator.
import module_name import module1, module2, ...., module
def funct (mymod, designation): print("Hello, ", mymod)
Save this program as module_work.py
Now, you can write another program with the name prog.py, which will contain:
import module_work module_work.funct(112, "Karlos")
If you run this prog.py, you will see the output Hello, 112
Naming and re-naming modules in Python:
Programmers can give any name to the module file, but the module should have the file extension .py. To rename a module, programmers need to use the alias while importing a module. The 'as' keyword is used for renaming the module with the aliasing concept.
import newmodule as mn sal = mn.data["salary"] print(sal)
Site Package for Modules:
There is another way to efficiently use frequently called modules. "Site-packages" is the popular directory that can hold modules that are built manually using Python. It stores the user-defined modules under this single Python packages. When you build and install Python packages from scratch, and use those modules repeatedly site package is the best place to store those modules. After creating the modules, programmers need to paste that/those modules in the following directory/location/
# For Linux
# For macO
# For Window
Modules play a critical role in software development using Python. Using pre-defined modules is better for programming as they are faster and already known to the Python interpreter. In the case of user-defined modules, they can help put customized operations and functionalities for our development. But they are comparatively slower than pre-defined modules. Therefore, it is always a smart option to choose pre-defined modules over user-defined modules.