We Offer 100% Job Guarantee Courses (Any Degree / Diploma Candidates / Year GAP / Non-IT / Any Passed Outs). Placement Records
Hire Talent (HR):+91-9707 240 250


Python Modules

Python Modules

Python Modules

Modules allude to a File containing Python statements and definitions. A File containing Python code, for Example, example.py, is known as a module and its module name would-be model.We use modules to separate vast projects into little reasonable and sorted out Files. Besides, modules give reusability of code.We can characterize our most utilized capacities in a module and import it, rather than replicating their definitions into various projects.

Python Modules is an object which can be defined as the variable name, class name, Functions and which is related to one group of code. Modules can be used in different level in using a built-in functions, which can be assigned to variables and we can use as a function for an example if the user needs to find the length of a string  “Besant Technology”  using module we can be done by below format.


x=” Besant Technology”


Print (“a(x)”)



This is done with len function assigned to a variable and it acts as a module and “a” is In-build in function which will have a memory with some value and which will point the len function and whenever you are accessing the “a” function which will return len functions.

Python Module can be even done by using from ,import , as this kind of keywords,For an example “from math import sin”, which will can access the sin function in your program. Or else you can use as “import math” as m through “m” you can access the sin function by using m.sin function.which will be done by the below  formats.

Import math as m


From math import  math


Modules can be done by object and references of the object the above example is the best one which is done by both object module and references of the object module. The Object module is done by calling through the import function and the references of the object  module are done by calling a .py file and calling the functions of .py files.

When you are calling the .py file which will retrieve all methods and all functions in .py file which will be done by import keyword and which will access by class name or building function.you can access both but the class should be public class and private class will not be accessed through modules.

For example:


def sqr(a):

 return a*a


>>def sqr (a)

return a*a

Save this above program as sample.py and now you can use it as a module in other programs as Name module to sample with keyword of import.which will perform the building function of sample.py and sqr function can used in other programs by import keyword and through import keyword you can access the  sample.py function. The below execution will shows the name module.

IDLE Execution

Then select the method from drop down and given some value. Which will act as sqr function it is pointing the sample.py file of sqr function ,memory pointer will pointing the sample.py file and the given value will perform through the memory pointer of the sample.py file and return the value the given below example shows the value.

>>import sample




After performing the function you will get value from import NAME MODULE function.Also, you can perform the Import-Module in different ways using FROM.

>from sample import sqr




In this you can access only particular function of sqr, but If you need to access all function in sample module you need to use import *.

from sample import *



Also you can Import the In-build library function using import Module, For example, the below we are using import math, import OS,  import collections, import data, import itertools.

>>>import math 



>>>import collections


>>>counter ({1:2,2:2,3:2,5:1,6:1})

>>>import iter tool as it


<itertools.combinations object at 0x02AFA060>

>>>print list (it.combinations([1,2,3,4],3))

[(1,2,3) , (1,2,4) , (1,3,4) ,  (2,3,4)]

For import math we can use all math functions and all and also we can use in different way

>>import math as m



Even we can import only one function with directly by from math import sin

>>import math as sin



Some of the example import math functions below,

>>> from math import cos

>>> cos (45)


>>>from math import tan

>>> factorial (5)


>>> acos (0.1)


>>> atan (0.4)


>>> asinh (-0.1)


>>> log (3.33,2)


>>> ceil (5.566)


>>> radians (4.4)


The above example shows through import keyword and from keyword with math library.

Python Module precedent

This program includes two numbers and returns the outcome

def add(a, b):
result = a + b
return result

How to import modules in Python?

We can import the definitions inside a module to another module or the intuitive mediator in Python. We utilize the import catchphrase to do this. To import our recently characterized module precedent we type the accompanying in the Python brief.

import model

This does not enter the names of the capacities characterized in model straightforwardly in the present image table. It just enters the module name precedent there.

Utilizing the module name we can get to the capacity utilizing dab (.) task. For instance:


Python import statement

We can import a module utilizing import statement and access the definitions inside it utilizing the spot administrator as depicted previously. Here is a precedent.

 import statement precedent

 To import standard module math

import math
print("The estimation of pi is", math.pi)

Import with renaming

We can import a module by renaming it as pursues.

 import module by renaming I

import math as m
print("The estimation of pi is", m.pi)

Python from…import statement

We can import explicit names from a module without bringing in the module all in all. Here is a precedent.

 import just pi from math module

from math import pi
print("The estimation of pi will be", pi)

We imported just the property pi from the module.

In such case we don’t utilize the speck administrator. We could have imported different qualities as pursues.

from math import pi, e

Import all names

We can import all names(definitions) from a module utilizing the accompanying develop.

 import all names from the standard module math

from math import *
print("The estimation of pi will be", pi)

Click Here-> Get Python Training with Real-time Projects

Besant Technologies WhatsApp