Create a user defined module with simple functions for: addition, subtraction, multiplication, division, modulo, square, factorial. Write a program to import the module and access functions defined in the module in python
Code
create mymath.py and app.py
mymath.py
# create add, sub, mul, div, mod, square, factorialdef add(a, b):return a + bdef sub(a, b):return a - bdef mul(a, b):return a * bdef div(a, b):return a / bdef mod(a, b):return a % bdef square(a):return a * adef factorial(a):if a == 0:return 1else:return a * factorial(a - 1)
app.py
import mymathprint(mymath.add(1, 2))print(mymath.sub(1, 2))print(mymath.mul(1, 2))print(mymath.div(1, 2))print(mymath.mod(1, 2))print(mymath.square(2))print(mymath.factorial(5))
0 Comments
Post a Comment