16. Python Module


 

Module:


The module is a simple python file. It has multiple variables, multiple functions, and some classes. This python file called a module.

Ex: dmath.py

In this math module contains all types of mathematic operations like adding, subtraction, multiplication, and much more.

Advantages:

For example, we have one python file. we need to write mathematic-based coding. But math.py file already has the same code. Then we just import that filename into the current python file and we can use that mathematic coding. This is called the code reusability concept once write the code and reuse it where we want to use it. This is the main advantage of the module concept.

Example:

import datetime     #it’s a date time  module

time=datetime.datetime.now() # now datetime functions connected with time variable.

print(time)    #now we giving the print command.

Result:

2021-06-02 18:30:20.240823

One more example

Now we need to do some cos(x) based mathematic radius coding. Then we already know that math() module has a cos(x) function and we can use it. Once we did the code we wrote more lines of code. Someone suggested that just use Numpy() module. then we import numpy() module into our code. it has also cos(x) function. Now we have one doubt is which module operates the cos(x) function.

Answer: python always taking the most recent module, not even module, variable, function, classes all are it’s taken most recent only.

In math() module has a lot of functions are there.

Please check the all function on below link:

https://www.w3schools.com/python/module_math.asp

Example of using math() module

from math import *

radius = (int(input('Enter Radius: ')))

print('Area of Circle:', pi*radius**2)

print('Area of Circle:', pi*pow(radius,2))

Result:

Enter Radius: 10

Area of Circle: 314.1592653589793

Area of Circle: 314.1592653589793

Python has contained inbuilt modules. Every module has a group of reusable functions and we can use these functions anytime as per our code requirements. Some of the modules are below.

cgi                   #Helpers for running Python scripts via the Common Gateway Interface.

cgitb               #Configurable traceback handler for CGI scripts.

chunk             #Module to read IFF chunks.

cmath             #Mathematical functions for complex numbers.

cmd                #Build line-oriented command interpreters.

code                #Facilities to implement read-eval-print loops.

codecs             #Encode and decode data and streams.

Python has a lot more modules are there. Please check the below URL.

https://docs.python.org/3/py-modindex.html

These modules will help the programmer, doing the job very less time and effectively. Not even inbuilt modules, third-party modules also doing a great job for python developers.

For example, we import a math module and we don’t know all the functions, but we need to check all these functions than just import math and print(dir(math). Will show all functions on that module.

Code:

import math

print(dir(math))

Result

['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']

import math

print(math.sqrt(20))

print(math.pi)

result:

4.47213595499958

3.141592653589793

One more option there in this program. For programmer-friendly no need to add (math.xx) like every time. We can write like math as 'm' or math as 'k' we can use. Check the below example. 

import math as m

print(m.sqrt(20))

print(m.pi)

result:

4.47213595499958

3.141592653589793

Once we applied as short-form we can’t use it as an original name. if we trying to type the original name it will give an error.

One more option

Before examples, we are import modules directly. And we can use all functions. But now which one we want the function that functions only import form the math module, check below

Code:

from math import sqrt       (or)     from math import sqrt  as s

print(sqrt(16))

4.0 # Result

One rule:

Which function we are importing that function only works. For other functions, we need to import.


0 Comments

Post a Comment

Post a Comment (0)

Previous Post Next Post