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.
>>> 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.
>>> 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.
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.
def module1():
print " It really worked "
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.
>>> import mod1
Make sure you do not enter the extension which we have used earlier while saving the module.
>>> mod1.module1()
It really worked
>>>
So now if we can make many functions and use the variables on the one module which is little bit similar to packages in oracle.
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)