>>> def add(a, b):
"""This program adds two
numbers and return the result"""
result = a + b
return result
>>>
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 :
A search path is a list of directories that the interpreter searches before importing a module. For example, to import the module support.py, 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
>>>
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.
from <module_name> import <attribute1,attribute2,attribute3,...attributen>
</attribute1,attribute2,attribute3,...attributen></module_name>
>> def circle(r):
print (3.14*r*r)
return
>>> def square(l):
print (l*l)
return
>>> def rectangle(l,b):
print (l*b)
return
>>> def triangle(b,h):
print (0.5*b*h)
return
>>> from math import pi
>>> print("The value of pi is", pi)
The value of pi is 3.141592653589793
>>>
>>>
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__',
'__text_signature__']
>>>
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))
10
>>> print (math.floor(a))
9
>>> b = 14
>>> print (math.sqrt(b))
3.7416573867739413
>>> print (math.exp(5.0))
148.4131591025766
>>> print (math.log(4.5))
1.5040773967762742
>>> print (math.pow(4.5,5.0))
1845.28125
>>> print (math.sin(0))
0.0
>>> print (math.cos(0))
1.0
>>> print (math.tan(108))
2.468161961582769
>>>