Computer Science
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)?
Python Libraries
2 Likes
Answer
The statements are given in the program below:
import basic
# (a) Compute square of 19.23
square_result = basic.square(19.23)
print("Square of 19.23:", square_result)
# (b) Compute floor division of 1000.01 with 100.23
floor_div_result = basic.floordiv(1000.01, 100.23)
print("Floor division of 1000.01 by 100.23:", floor_div_result)
# (c) Compute product of 3, 4 and 5
product_result = basic.mul(basics.mul(3, 4), 5)
print("Product of 3, 4, and 5:", product_result)
# (d)
result2 = basic.div(0, 100)
result1 = basic.div(100, 0)
print("Result of basic.div(100, 0):", result1)
print("Result of basic.div(0, 100):", result2)
Output
Square of 19.23: 369.79290000000003
Floor division of 1000.01 by 100.23: 9.0
Product of 3, 4, and 5: 60
Result of basic.div(0, 100): 0.0
ZeroDivisionError
Explanation
(d) basic.div(100, 0)
results in a ZeroDivisionError because division by zero is not defined whereas basic.div(0, 100)
results in 0.0 as the quotient of '0 / 100'.
Answered By
2 Likes
Related Questions
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.
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)
Suppose that after we import the random module, we define the following function called
diff
in a Python session :def diff(): x = random.random() - random.random() return(x)
What would be the result if you now evaluate
y = diff() print(y)
at the Python prompt ? Give reasons for your answer.
What are the possible outcome(s) executed from the following code? Also specify the maximum and minimum values that can be assigned to variable NUMBER.
STRING = "CBSEONLINE" NUMBER = random.randint(0, 3) N = 9 while STRING[N] != 'L' : print(STRING[N] + STRING[NUMBER] + '#', end = '') NUMBER = NUMBER + 1 N = N - 1
- ES#NE#IO#
- LE#NO#ON#
- NS#IE#LO#
- EC#NB#IS#