In Python, a module is a file that contains functions, classes, or variables. It helps us organize code and reuse it in different programs. There are many modules in Python, each designed for specific tasks.
A module is a file (usually with a .py extension) that contains functions, classes, variables, or executable code. It helps us organize related code in one place and reuse it in different programs.
The modules are used to make the code easier to read, maintain, and manage. Python provides three types of modules:
To create a Python module, we write the required code and save it in a file with a .py extension. This file can then be used in other programs.
In the following example, we are creating a Python module.
Explanation:
In the above example, we have created a Python script consisting of some functions that will help us add, subtract, multiply, and divide two numbers. We have saved this file as calculator.py. This user-defined module can be used in other Python programs.
We can import functions and classes from one module into another using the import statement
The syntax for importing a module is shown below:
Using the import keyword followed by the module_name allows us to use all the functions and classes defined in that module. To access them, we use the dot (.) operator.
Note: When we import a module, the entire module is loaded.
In the following example, we are importing a module and using its functions.
Output:
16 + 7 = 23 16 - 7 = 9 16 * 7 = 112 16 / 7 = 2.2857142857142856
Explanation:
In this example, we imported the module calculator. Then we used its functions like add(), subtract(), multiply(), and divide() using the dot operator. The results are stored in variables and printed.
We can import a module by renaming it. This process allows us to use an alias (a shorter reference) for that module.
Let us take a look at the following example for better understanding.
Output:
25 + 9 = 34 25 - 9 = 16 25 * 9 = 225 25 / 9 = 2.7777777777777777
Explanation:
In the above example, we have imported the user-defined module 'calculator' as 'cal' and created two variables. We have then accessed the different functions like add(), subtract(), multiple(), and divide() of the imported module using cal followed by the function name. We stored the results of these functions in different variables and printed their values.
Python allows us to import specific attributes without importing the module as a whole. To understand this, let us see the following example:
Output:
12 - 7 = 5 12 * 7 = 84
Explanation:
In the above example, we have imported the subtract() and multiply() functions from the user-defined module 'calculator'. We have then initialized two variables and called the imported functions to calculate the difference and product of the numbers. At last, we have printed the resultant values.
Similar to importing a certain attributes from the module, we can use the asterisk * symbol to import everything from the module.
It has the following syntax:
Let us take an example to demonstrate how to import all attributes.
Output:
19 * 14 = 266 19 / 14 = 1.3571428571428572
Explanation:
In this example, we have imported everything from the user-defined module 'calculator' using the asterisk * symbol. We have then initialized two variables and called the multiply() and divide() functions to calculate the product and division of the numbers. At last, we have printed the resultant values.
Whenever we import a module into a program, the Python interpreter looks for numerous locations. First of all, it checks whether the imported module is a built-in module; if not, then it looks for a list of directories defined in the sys.path. Python interpreter searches for the module in the following way:
Step 1: Firstly, it searches for the module in the current directory.
Step 2: If the module is not found in the current directory, Python then searches each directory in the shell variable, PYTHONPATH. It is an environment variable that contains a list of directories.
Step 3: In case, the module is not found, Python checks the installation-dependent list of directories configured at the time Python is installed.
In order to check the list of directories, we can use the path variable from the sys module as shown below:
Output:
['/content', '/env/python', '/usr/lib/python311.zip', '/usr/lib/python3.11', '/usr/lib/python3.11/lib-dynload', '', '/usr/local/lib/python3.11/dist-packages', '/usr/lib/python3/dist-packages', '/usr/local/lib/python3.11/dist-packages/IPython/extensions', '/usr/local/lib/python3.11/dist-packages/setuptools/_vendor', '/root/.ipython']
Explanation:
In the above example, we have imported the sys module and used its path variable to return the list of directories for the modules.
Python provides a built-in function called dir() that is used to list all the attributes (functions, variables, and classes) of a module. It helps us see what is available inside a module.
In the following example, we are using the dir() function to get all attributes of a module.
Output:
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'add', 'divide', 'multiply', 'subtract']
Explanation:
In this example, we used the dir() function to list all attributes of the calculator module. It shows user-defined functions like add(), divide(), multiply(), and subtract(). The names starting with underscores are default Python attributes related to the module.
Python provides many built-in modules that offer useful functions for different tasks. We can import these modules using the import statement and use their features in our programs.
In the following example, we are importing the math module and using its functions.
Output:
Pi = 3.141592653589793 Square root of 16 = 4.0 Floor value of 19.5 = 19 Ceiling value of 24.7 = 25 Factorial of 6 = 720
Explanation:
In this example, we imported the math module and used its functions like sqrt(), floor(), ceil(), and factorial().
In the following example, we are importing the random module and using its functions.
Output:
Random Integer between 10 and 50 = 48 Random Floating Point between 0 and 1 = 0.9739271518285192 Random Item from the List = 4
Explanation:
In this example, we imported the random module and used functions like randint(), random(), and choice() to generate random values.
We request you to subscribe our newsletter for upcoming updates.