0% found this document useful (0 votes)
3 views7 pages

CH-4

The document discusses the usage of Python libraries, specifically focusing on module imports and function calls. It provides examples of how to correctly import and use functions from modules, as well as common errors encountered during function calls. Additionally, it includes exercises for completing code and understanding random number generation in Python.

Uploaded by

indianarmie202
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views7 pages

CH-4

The document discusses the usage of Python libraries, specifically focusing on module imports and function calls. It provides examples of how to correctly import and use functions from modules, as well as common errors encountered during function calls. Additionally, it includes exercises for completing code and understanding random number generation in Python.

Uploaded by

indianarmie202
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

CH-4 USING PYTHON LIBRARIES

Type B: Application Based Questions

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)

2. from tempConversion import * — This imports all objects (functions,


variables, etc.) from the module into the current namespace. This
approach imports all objects directly into the current namespace, so
we can call the module's functions without using the module name.
For example:
from tempConversion import *
to_centigrade(32)
to_fahrenheit(0)

Question 2

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 as checkMain(4, 'Z'). Why are these two
function-call statements different from one another when the function
being invoked is just the same ?
Answer
Allchecks.checkMain(3, 'A')and checkMain(4, 'Z') these two function-
call statements are different from one another when the function
being invoked is same because in Allchecks.checkMain(3,
'A') statement, Allchecks.py module is imported using import
Allchecks import statement in a new namespace created with the
same name as that of module name and hence they are called by
dot notation whereas in checkMain(4, 'Z') statement Allchecks.py
module is imported using from Allchecks import checkMain import
statement in the current namespace and hence its module name is
not specified along with the function name.

Question 3

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.

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

# (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

Question 6

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.
Answer
import random
def diff():
x = random.random() - random.random()
return(x)

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

Consider the following code :


import random
print(int(20 + random.random() * 5), end = ' ')
print(int(20 + random.random() * 5), end = ' ')
print(int(20 + random.random() * 5), end = ' ')
print(int(20 + random.random() * 5))

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.

You might also like