Modules in Python

A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it.

Module is an object with arbitrarily named attributes which can be used in binding and referencing.

We can define a function, classes and variables inside a module; it can also have the runnable code.

Example:There is a function called “FLOOR” which is used to round the value for the given input to it. This function is inside a module that allows us to use it.

Let us check if we are able to use this function or not:

Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> floor(17.5)
Traceback (most recent call last):
 File "<pyshell#0>", line 1, in <module>
 floor(17.5)
NameError: name 'floor' is not defined
>>>

Here from the above we can see that the error as this function is not present on my Python because we have not imported the Module which is holding this function.

Let us import the module i.e. “MATH” which has “FLOOR” function.

>>> import math

Now let us use the “FLOOR” function again and check if we are able to use it:

>>> floor(17.5)
Traceback (most recent call last):
 File "<pyshell#2>", line 1, in <module>
 floor(17.5)
NameError: name 'floor' is not defined

But still we see the same error because we cannot use the function like the above statement, in order to use we must provide the module name at the beginning and then the function name to it as like below:

>>> math.floor(17.5)
17.0
>>>

So now we are able to use the “FLOOR” function.

To check what are the other modules present in the math module we can hit “>>> math.” and hold for a sec it will display all the functions associated with it. Or by using “dir()” which is a built-in function which will return the list of the names defined inside a module as like below:

>>> dir(math)
['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']
>>>

So from the above list we can see what are the functions that math module have, now we can use it inside our code whenever it is required.
Now let us see how we can assign it to an variable, so that we don’t have to use ” module.function<<agruments>> ” every time.

Example:

>>> var1 =math.floor
>>> var1(17.5)
17.0
>>>

So now we can use “var1” variable in our code instead of writing “math.floor” every time.

For getting more information on modules in PYTHON we can go to Python official website i.e. “http://docs.python.org/tutorial/modules.html#”.

Advertisements

One thought on “Modules in Python

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s