Computer Science
Given below is semi-complete code of a module basic.py :
#
"""..............."""
def square(x):
"""..............."""
return mul(x, x)
...............mul(x, y):
"""..............."""
return x * y
def div(x, y):
"""..............."""
return float(x)/y
...............fdiv(x, y)...............
"""..............."""
...............x//y
def floordiv(x, y)...............
...............fdiv(x, y)
Complete the code. Save it as a module.
Answer
# basic.py
"""This module contains basic mathematical operations."""
def square(x):
"""Returns the square of a number."""
return mul(x, x)
def mul(x, y):
"""Returns the product of two numbers."""
return x * y
def div(x, y):
"""Returns the result of dividing x by y."""
return float(x) / y
def fdiv(x, y):
"""Returns the result of floor division of x by y."""
return x // y
def floordiv(x, y):
return fdiv(x, y)
The code is saved with .py extension to save it as module.
Related Questions
Create module tempConversion.py as given in Fig. 4.2 in the chapter. If you invoke the module with two different types of import statements, how would the function call statement for imported module's functions be affected ?
A function checkMain() defined in module Allchecks.py is being used in two different programs. In program 1 as
Allchecks.checkMain(3, 'A')
and in program 2 ascheckMain(4, 'Z')
. Why are these two function-call statements different from one another when the function being invoked is just the same ?After importing the above module, some of its functions are executed as per following statements. Find errors, if any:
(a) square(print 3)
(b) basic.div()
(c) basic.floordiv(7.0, 7)
(d) div(100, 0)
(e) basic.mul(3, 5)
(f) print(basic.square(3.5))
(g) z = basic.div(13, 3)
Import the above module basic.py and write statements for the following :
(a) Compute square of 19.23.
(b) Compute floor division of 1000.01 with 100.23.
(c) Compute product of 3, 4 and 5. (Hint. use a function multiple times).
(d) What is the difference between basic.div(100, 0) and basic.div(0, 100)?