CH-4
CH-4
Question 1
Answer
If we invoke the tempConversion module with two different types of
import statements (import tempConversion and from tempConversion import
*), the way we call the module's functions will be affected as follows:
1. import tempConversion — This imports the entire module in a new
namespace setup with the same name as that of the module
tempConversion. To call the module's functions, we need to use the
dot notation tempConversion.<function_name>.
For example:
import tempConversion
tempConversion.to_centigrade(32)
tempConversion.to_fahrenheit(0)
Question 2
Question 3
def square(x):
"""Returns the square of a number."""
return mul(x, x)
Question 4
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)
Answer
(a) square(print 3) — print function should not be there as parameter
in the function call. So the correct function call statement is square(3).
Also, to call square function without prefixing the module name, it
must be imported as from basic import square.
(b) basic.div() — The div() function requires two arguments but none
are provided. So the correct statement is basic.div(x, y).
(c) basic.floordiv(7.0, 7) — There is no error.
(d) div(100, 0) — This will result in a runtime error of
ZeroDivisionError as we are trying to divide a number by zero. The
second argument of the function call should be any number other
than 0. For example, div(100, 2). Also, to call div function without
prefixing the module name, it must be imported as from basic import
div.
(e) basic.mul(3, 5) — There is no error.
(f) print(basic.square(3.5)) — There is no error.
(g) z = basic.div(13, 3) — There is no error.
Question 5
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)?
Answer
The statements are given in the program below:
import basic
# (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
Question 6
y = diff()
print(y)
Output
0.006054151450219258
-0.2927493777465524
Question 7
Answer
The outcomes are as follows:
ES#NE#IO#
EC#NB#IS#
NUMBER is assigned a random integer between 0 and 3 using
random.randint(0, 3). Therefore, the minimum value for NUMBER is
0 and the maximum value is 3.
Question 8
Find the suggested output options 1 to 4. Also, write the least value
and highest value that can be generated.
1. 20 22 24 25
2. 22 23 24 25
3. 23 24 23 24
4. 21 21 21 21
Answer
23 24 23 24
21 21 21 21
The lowest value that can be generated is 20 and the highest value
that can be generated is 24.
Question 9
Answer
105 107 105 110
110 105 105 110
The least value that can be generated is 105 and the highest value
that can be generated is 110.
Question 10
Answer
(a) If the module wavwrite is imported using the command import
music.formats.wavwrite, we can invoke its writefile() function by using
the module name followed by the function name:
import music.formats.wavwrite
music.formats.wavwrite.writefile()
(b) If the module wavwrite is imported using the command from
music.formats import wavwrite, we can directly invoke its writefile()
function without prefixing the module name:
from music.formats import wavwrite
writefile()
Question 11
Answer
DELHI
MUMBAI
CHENNAI
KOLKATA
DELHIDELHI
MUMBAIMUMBAI
CHENNAICHENNAI
KOLKATAKOLKATA
The minimum value for PICK is 0 and the maximum value for PICK is
3.