Python Tutorial
$ python3 Python 3.5.3 (default, May 11 2017, 09:10:41) [GCC 6.3.1 20161221 (Red Hat 6.3.1-1)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import arithmetic >>> arithmetic.__name__ 'arithmetic' >>>here arithmetic is the python module name.
def sum(a, b): return a+b; def sub(a, b): return a-b; def mul(a, b): return a*b; def div(a, b): return a/b;
$ python Python 2.7.13 (default, May 10 2017, 20:04:36) [GCC 6.3.1 20161221 (Red Hat 6.3.1-1)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import arithmetic >>> arithmetic.sum(6, 4) 10 >>> arithmetic.sub(6, 4) 2 >>> arithmetic.mul(6, 4) 24 >>> arithmetic.div(6, 4) 1 >>> exit()
import importlib; importlib.reload(modulename)Need to restart the interpreter if you change the modules.
import module1[, module2, module3..]
import arithmetic; print "sum of 6 and 5: ", arithmetic.sum(6,4); print "sub of 6 and 5: ", arithmetic.sub(6,4); print "mul of 6 and 5: ", arithmetic.mul(6,4); print "div of 6 and 5: ", arithmetic.div(6,4);Output:
$ python test_module.py sum of 6 and 5: 10 sub of 6 and 5: 2 mul of 6 and 5: 24 div of 6 and 5: 1
def add(a, b): return a+b; def sub(a, b): return a-b; def mul(a, b): return a*b; def div(a, b): return a/b;//test_module.py
from arithmetic import add, sub; print "sum of 6 and 5: ", add(6,4); print "sub of 6 and 5: ", sub(6,4);Output:
$ python test_module.py sum of 6 and 5: 10 sub of 6 and 5: 2if you try to invoke arithmetic.mul(6, 4), raises below error
Traceback (most recent call last): File "test_module.py", line 5, inprint "mul of 6 and 5: ", mul(6,4); NameError: name 'mul' is not defined
from arithmetic import *; print "sum of 6 and 5: ", add(6,4); print "sub of 6 and 5: ", sub(6,4); print "mul of 6 and 5: ", mul(6,4); print "div of 6 and 5: ", div(6,4);Output:
$ python test_module.py sum of 6 and 5: 10 sub of 6 and 5: 2 mul of 6 and 5: 24 div of 6 and 5: 1
if __name__ == "__main__": import sys; print "sum: ", add(int(sys.argv[1]), int(sys.argv[2]));// arithmetic.py
def add(a, b): return a+b; def sub(a, b): return a-b; def mul(a, b): return a*b; def div(a, b): return a/b; if __name__ == "__main__": import sys; print "sum: ", add(int(sys.argv[1]), int(sys.argv[2]));Output:
$ python arithmetic.py 6 4 sum: 10
$ python Python 2.7.13 (default, May 10 2017, 20:04:36) [GCC 6.3.1 20161221 (Red Hat 6.3.1-1)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import arithmetic, sys >>> dir(arithmetic) ['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'add', 'div', 'mul', 'sub'] >>> dir(sys) ['__displayhook__', '__doc__', '__excepthook__', '__name__', '__package__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_debugmallocstats', '_getframe', '_mercurial', 'api_version', 'argv', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dont_write_bytecode', 'exc_clear', 'exc_info', 'exc_type', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'getcheckinterval', 'getdefaultencoding', 'getdlopenflags', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'gettrace', 'hexversion', 'long_info', 'maxint', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'ps1', 'ps2', 'py3kwarning', 'pydebug', 'setcheckinterval', 'setdlopenflags', 'setprofile', 'setrecursionlimit', 'settrace', 'stderr', 'stdin', 'stdout', 'subversion', 'version', 'version_info', 'warnoptions'] >>> dir() ['__builtins__', '__doc__', '__name__', '__package__', 'arithmetic', 'sys']dir() method returns currently defined names list, not lists built-in functions and variables.
A.B # sub module B in a package A.B is the sub module in package A.
import math.arithmetic.addimports all the sub modules of math.arithmetic module.
from math.arithmetic import *relative reference import, imports add sub module from current module.
from . import add from .. import add
Python Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page