Saturday 5 December 2015

Python: import module in other module

Modules can import other modules using import statement.

arithmetic.py
def sum(a, b):
  return a+b

def subtract(a, b):
  return a-b
  
def mul(a,b):
  return a*b
  
def div(a, b):
  return a/b


main.py
import arithmetic

print(arithmetic.sum(10, 20))
print(arithmetic.subtract(10, 20))
print(arithmetic.mul(10, 20))
print(arithmetic.div(10, 20))


$ python3 main.py
30
-10
200
0.5

Note
It is not necessary to put all import statements in the beginning of module. You can import whenever required.





Previous                                                 Next                                                 Home

No comments:

Post a Comment