Modules
Modules
Now, here what we will be doing is to break the complete code into
separate parts and simply create one module for all these operations
or separate modules for each of the operations. And then we can call
these modules in our main program logic.
Here the core idea is to minimize the code, and if we create modules,
it doesn’t mean we can only use it for this program, but we can even
call these modules for other programs as well.
For Example,
To incorporate the module into our program, we will use the import
keyword, and to get only a few or specific methods or functions from
a module, we use the from keyword.
NOTE: When we are using a function from a module, then we use the
following syntax:
module_name.function_name
Now to use the module which we have just created, we are using the
import statement:
For Example,
Output:
Hello, Chirag Goyal to Analytics Vidhya
The module can contain functions, as already described, but can also
contain variables of all types such as arrays, dictionaries, objects, etc.
For Example,
For Example,
Output:
19
We can name the file of the module whatever you like, but we have to
note that it must have the file extension “.py”.
To rename the module name, we can create an alias when you import
a module, with the help of the as keyword:
For Example,
Output:
19
For Example,
Now, we have a module named mymodule that has one function and
one dictionary:
def welcome(name):
print("Hello, " + name +" to Analytics Vidhya")
person1 = {
"name": "Chirag Goyal",
"age": 19,
"country": "India"
"education”: “IIT Jodhpur"
}
Now, Let’s try to Import only the person1 dictionary from the module
named mymodule:
from mymodule import person1
print (person1["age"])
Output:
19
For Example,
Reusability
Simplicity
Scoping
Now, let’s discuss some of the useful and frequently used built-in
modules of Python.
Math Module
Statistics Module
Trigonometric Ratios
For calculating various trigonometric ratios for a given angle, the math
module contains several functions. The trigonometric functions such
as sin, cos, tan, etc. take the angle argument in radians. While we
are used to expressing the angle in degrees. In the math module, we
have two angle conversion functions that help us to convert the angle
from degrees to radians and vice versa:
degrees()
radians()
For Example,
For Example,
In this example, we will find the value of sin, cos, and tan ratios for the
angle of 30 degrees which in radians is equal to 0.5235987755982988
radians.
>>> import math
>>> math.sin(0.5235987755982988)
0.49999999999999994
>>> math.cos(0.5235987755982988)
0.8660254037844387
>>> math.tan(0.5235987755982988)
0.5773502691896257
You may also try some more functions of the math module such as
math.log(), math.log10(), math.pow(). math.sqrt(), math.exp(),
math.ceil(), math.floor(), etc.
Mean
Median
Mode
Standard Deviation
Mean
For Example,
>>> import statistics
>>> statistics.mean([2,5,6,9])
5.5
Median
The median() method returns the middle value of numeric data
present in a list.
For Example,
>>> import statistics
>>> statistics.median([1,2,3,7,8,9])
5.0
>>> statistics.median([1,2,3,8,9])
3.0
Mode
The mode() method returns the most common data point present in
the list.
For Example,
>>> import statistics
>>> statistics.mode([2,5,3,2,8,3,9,4,2,5,6])
2
Standard Deviation
For Example,
>>> import statistics
>>> statistics.stdev([1,1.5,2,2.5,3,3.5,4,4.5,5])
1.3693063937629153
Creating Package
Let’s create a package in Python named mypckg that will contain two
modules mod1 and mod2. To create this module follow the below
steps:
Create a folder named mypckg.
Inside this folder create an empty Python file i.e. __init__.py
Then create two modules mod1 and mod2 in this folder.
Mod1.py
Python3
def gfg():
print("Welcome to GFG")
Mod2.py
Python3
This __init__.py will only allow the gfg and sum functions from the
mod1 and mod2 modules to be imported.
Example 1:
We will import the modules from the above-created package and will
use the functions inside those modules.
Python3
from mypckg import mod1
mod1.gfg()
res = mod2.sum(1, 2)
print(res)
Output:
Welcome to GFG
3