Python Modules
Python Modules are used to categorize code in Python into smaller part. Modules are a simple way to organize a program which contains program code, where classes, functions and variables are defined.  A module allows you to logically organize your Python code. Grouping related code into a module makes the code easier to understand and use. 

A file containing Python code, for e.g.:, is called a module and its module name would be example. We use modules to break down large programs into small manageable and organized files. Furthermore, modules provide reusability of code. Type the following and save it as
Basic Syntax : ( )
>>> def add(a, b):
	"""This program adds two
   numbers and return the result"""
	result = a + b
	return result


The import Statement

You can use any Python source file as a module by executing an import statement in some other Python source file. The import has the following syntax :

import module1[, module2[,... moduleN]

A search path is a list of directories that the interpreter searches before importing a module. For example, to import the module, you need to put the following command at the top of the script.

>>> # import statement example
>>> # to import standard module math
>>> import math
>>> print("The value of pi is", math.pi)
The value of pi is 3.141592653589793

Python from...import statement

from..import statement is used to import particular attribute from a module. In case you do not want whole of the module to be imported then you can use from ?import statement.

Syntax :
from  <module_name> import <attribute1,attribute2,attribute3,...attributen>     
Examples :
>> def circle(r):
	print (3.14*r*r)

>>> def square(l):
	print (l*l)
>>> def rectangle(l,b):
	print (l*b)

>>> def triangle(b,h):
	print (0.5*b*h)
>>> from math import pi
>>> print("The value of pi is", pi)
The value of pi is 3.141592653589793

dir() method

dir() is an in-built method used to find all attributes (i.e all available classes, functions, variables and constants ) of the object. As we have already discussed everything in python is object, we can use dir() method to find attributes of the module like this :

dir() returns a list of string containing the names of the available attributes.
>>> from math import factorial
>>> dir (factorial)
['__call__', '__class__', '__delattr__', '__dir__', 
'__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', 
'__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', 
'__lt__', '__module__', '__name__', '__ne__', '__new__', 
'__qualname__', '__reduce__', '__reduce_ex__', '__repr__', 
'__self__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 

Built in Modules in Python :

There are many built in modules in Python. Some of them are as follows :

math, random , threading , collections , os , mailbox , string , time , tkinter etc.. Each module has a number of built in functions which can be used to perform various functions.

Using math module , you can use different built in mathematical functions.

Function Description
ceil(n)Returns the next integer number of the given number
sqrt(n)Returns the Square root of the given number.
exp(n)Returns the natural logarithm e raised to the given number
floor(n)Returns the previous integer number of the given number.
log(n,baseto)Returns the natural logarithm of the number.
pow(baseto, exp)Returns baseto raised to the exp power.
sin(n)Returns sine of the given radian.
cos(n)Returns cosine of the given radian.
tan(n)Returns tangent of the given radian.
>>> import math
>>> a = 9.9
>>> print (math.ceil(a))
>>> print (math.floor(a))
>>> b = 14
>>> print (math.sqrt(b))
>>> print (math.exp(5.0))
>>> print (math.log(4.5))
>>> print (math.pow(4.5,5.0))
>>> print (math.sin(0))
>>> print (math.cos(0))
>>> print (math.tan(108))