Don't let the Lockdown slow you Down - Enroll Now and Get 3 Course at 24,999/- Only. Explore Now!

General

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.

Example:

x=” Besant Technology”

a=len

Print (“a(x)”)

Output:

17

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

M.sin(45)

From math import  math

sin(45)

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:

Program:

def sqr(a):

 return a*a

IDLE-Execution

>>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

>>sample.sqr(5)

>>25

>>>

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

>>sample.sqr(5)

>>25

>>>

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 *

>>sample.sqr(5)

>>25

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 

>>>math.sin(45)

>>>0.8509035245341184

>>>import collections

>>collections.counter([1,2,3,5,6,1,3,2])

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

>>>import iter tool as it

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

<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

>>>m.sin(45)

>>>0.8509035245341184

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

>>import math as sin

>>>sin.sin(45)

>>>0.8509035245341184

Some of the example import math functions below,

>>> from math import cos

>>> cos (45)

0.52531988177297

>>>from math import tan

>>> factorial (5)

120

>>> acos (0.1)

1.4706289056333368

>>> atan (0.4)

0.380563771123649

>>> asinh (-0.1)

-0.09983407889920758

>>> log (3.33,2)

1.7355221772965377

>>> ceil (5.566)

6.0

>>> radians (4.4)

0.07679448708775051

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:

example.add(4,5.5)
>>9.5

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
>>pi 
3.141592653589793
>>e 
2.718281828459045

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

Scroll Up
Besant Technologies WhatsApp