0
What are modules ? (Please explain)
3 Answers
+ 10
They are independent programs.
Its like having a phone that has no camera so you make one for it. The camera you make would be a independent module which you would plugin to your phone to create a unit.
+ 3
modules are a group of methods|functions already written by another person that share some type of relationship. the purpose is to save the user from having to write the functions every time they are needed. One of the main advantage of using modules comes when you consider speed of computation. instead of. having your memory filled with every possible function one could think of (think of how much that would slow up you machine)it is much more efficient to. "import" or chose the group of. functions most relevant. the truth is if you only need one function from a library it is totally possible to only import this single operation. hope this helps.đ
+ 1
To Play with Modules in Python
1. To import multiple modules we can use
import math, random, sys
2. To import only some useful functions of a module, we can use the code as
from math import sqrt
3. We can also give some alias name to the particular function after import statement
from math import sqrt as square_root
4. We can also give alias names for different functions with in a module using commas.
from math import sqrt as square_root, cos as cosine, tan as tangent
print square_root(49)
>>>7.0
print cosine(0)
>>>1
print tan(45)
>>>1
5. We can print the list of all functions within a module using the line of code
import math
print 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',