Updated April 4, 2023
Introduction to Python Modules
A module is where the code is logically organized and where particular grouping is done, which makes the code easier to understand. So, what content is organized or grouped? The answer to that would be; any runnable code including different functions, classes, and variables. Here, we can define all the required code in a module, and we can import the same wherever required. Let’s see how we can define and use them. In this topic, we are going to learn about Python Modules.
Different Python modules
Basically, we can have two different types of modules. One is the default, and the other; user-defined. As the name already suggests, default modules are those which are already in-built in python.
For the user-defined modules, below are a few examples and how to use them.
Suppose we write a python code containing two different functions, one for printing out a statement and the other for multiplying two different numbers, like:
def first():
print("I am printing this as a first function")
def second(a,b):
c=a*b;
print("After multiplying: " ,c)
first()
second(2,3)
The output we get after calling the functions are:
And save this file (without calling the functions) as my_first_module.py
Now, we created a new file named my_new_file.py and import our module.
import my_first_module as my
my.second(2,5)
Then went to my python command prompt and executed my_new_file.py; python file. Find the output as below:
Now, let us call the first function also and see the output. After making the required changes in the python file, save it and then run it to get the updated output.
Output as:
In this way, we can create our functions and import them into any other file to reuse them and make our work easier.
If I had to reuse any of these functions, we can simply import this module into our programs and use any of these functions. Now, how to import in python?
We have different methods for this, and obviously, each having its advantage. We can use,
- Import module_1
- From module_1 import function_1
- From module_1 import *
We can import a specific function from that particular module from the second way of importing the module. And through the last method, we can completely import the whole module. If we import the whole module, we can know the list of functions defined under that module with the command dir.
Now, we can try to import our module using the statement mentioned in the second point above.
from my_first_module import second
second(2,5)
Save it and run to find the output as below:
Here, if we try to call the first function also? Can we get the output? Let’s check below:
The output would be:
As an exercise, can you try using the third form of import and check the output?
Now, let us see different built-in modules in python.
- Math
This is one of the most used modules, and these deal with all the mathematical functions and calculations. Let us check different functions we can write using these modules.
import math
math.sqrt(64)
math.cos(60)
math.pi
math.factorial(6)
math.floor(7.98)
math.pow(3,4)
Outputs of all these can be obtained as:
- Random
Random is another module where we can get and assign random values. Below are a few examples where we can use the random function.
import random
print(random.random())
print(random.randint(0,10))
new_list = [1, “happy” ,2, “learning”]
print(random.choice(new_list)
Output as:
Here, in the first example, the random function in the random module gives us any random floating-point number between 0 and 1.
The second example gives any random integer between 0 and 10.
And as the third example, we then created a list with different items in it. So, using the choice function would randomly pick a value from the given list.
This random function is mainly used if we need to generate some vague data for testing or teaching purposes.
- Pandas
This is also one of the most used and important modules of python. This is generally used for data cleaning and data analysis platform. The main function is for reading the csv or any type of We can generate predictions on the unknown data through this module.
- NumPy
As the name suggests, it can be used in complex mathematical operations with multi-dimensional arrays. Many array-related functions can be used here, like slicing, array indexing and many basic operations in the matrix can be easily done.
- SciPy
This is an extension of the NumPy module and used for machine learning which can solve linear algebra expressions, performs differentiation and integrations, etc.
- Matplotlib
This module can help us in representing the data analyzed in the form of graphs. We can create a bar graph, hist graph, or scatter plot in displaying our data visually.
We also have to notice that some modules like the above-mentioned pandas, SciPy, etc., are not in default present in the python. We first need to install them and then can import and use them. We can install these using the pip or pip3 command based on the version of python that is being used.
Not only these, but we have many other modules like io, itertools, sqllite3, string, symbol, time, and many more.
Conclusion
Python is now a widely-used programming language that is used in the process of data cleaning and data analysis. And the use of modules here is that it has many predefined functions built inside them to reduce our work and easy evaluation of many operations and for maintaining the data. We can even create our modules and can easily import them. So, keep playing with all these different modules and enjoy learning python.
Recommended Articles
This is a guide to Python Modules. Here we discuss the definition of all the required code in a module and two different types of Python Modules. You may also have a look at the following articles to learn more –