Using Python Libraries Class 12 Solutions

Using Python Libraries assignment solutions for Class 12 Computer Science (Python). Solved assignment of Short Answer Questions, Application based questions and programming from Sumita Arora and NCERT book. This post is very useful for the study of CBSE Class XII Computer Science and score maximum marks in Class XII Board Exam.

Short Answer Questions : Using Python Libraries Class 12

Q.1. What is the significance of modules?

Answer:
The act of partitioning a program into individual components is called modularity. A module is a separate unit in itself. The justification for partitioning a program is that
a)it reduces its complexity to some degree
b)it creates a number of well-defined, documented boundaries within the program
c)Module contents can be reused in other programs, without having to rewrite or recreate them.

Q.2.What are docstrings ? What is their significance? Give example to support your answer.

Answer:
Docstrings are triple quoted comments ; useful for documentation purposes. For documentation, the docstrings should be the first string stored inside a module/class.
Example-
def cent(n)
“””Returns: x converted to centigrade”””
return n

Q.3. What is a package ? How a package different from module?

Answer:
 A package is a collection of Python modules: while a module is a single Python file, a package is a directory of Python modules containing an additional __init__.py file, to distinguish a package from a directory that just happens to contain a bunch of Python scripts.

Q.4. What is a library? Write procedure to create own library in Python.

Answer:
A python library is a reusable chunk of code that is used in program / script using import command. A package is a library if it is installable or gets attached to site-packages folder of python installation. The line between a package and a python library is quite blurred and both these terms are often used interchangably . Procedure to create a python library is as follows:
i)Create the directory structure having folders with names of package and subpackages.
ii) Create _init_.py files in package and subpackage folders.
iii) Associate it with python installation

Q.5.what is the use of file _init_.py in a package even when it is empty ?

Answer:
In a directory structure, in order for a folder to be recognized as a package , the file __init__.py must also be stored in the folder, even if the file is empty.

Q.6. What is the importance of site-packages folder of python installation?

Answer:
site-packages is the target directory of manually built python packages. When we build and install python packages from source , we will find the installed modules in site-packages by default. Site-packages is by default part of the python search path , so modules installed there can be imported easily afterwards.

Q.7.How are following import statements different?
(a)import X
module X is imported to the program . the function call is given by X.func().
(b)from X import *
All of the function of module can be directly called as fun().
(c)from X import a,b,c
Only function a,b,c is only imported and called directly as a,b,c. Other function a,b,c is not imported .

Q.8.What is PYTHON PATH variable? what is its significance.

Answer:
Python path variable is where the interpreter will look for modules importing.

Q.9. In which order python looks for the function/module names used by you.

Answer:
Firstly it searches in the code if the definition is in code . If not able to find then it searches in built function . Not in inbuilt function then it goes to imported module. if not found then goes to python path and searchs here. Here it is the package present that have many functions.
import sys
print(sys.path)

Q.10. What is the usage of help() and dir() functions.

Answer:
The help() function in python can be used to see the documentation.
 dir() shows a list of attributes for the object passed in as argument , without an argument it returns the list of names in the current local namespace

Q.11. Name the Python library modules which need to be imported to invoke the following functions:
(i)log() (ii)pow()
Answer:

Math Module

Q.12.What is dot notation of referring to objects inside a module?

Answer: The dot notation is used to refer anything function and classes etc that is inside python module.

Q.13.Why should the from <module>import<object> statement be avoided to import objects?

Answer:
Name classes occur.

Q.14.What do you understand by standard library of python?

Answer:
Is collection of script modules accessible to a python program to simplify the programming process and removing the need to frewrite commonly used commands.

Q.15.Explain the difference between import<module> and from <module>import statement, with examples.

Answer:
By coding import<module> we import all the function , classes etc from the module.
By coding <module>import we import specific elements of the modules all are not imported.

Application Based Questions : Using Python Libraries Class 12 Solutions

Q.1. Create module tempConversion.py. If we invoke the module with two different types of imported statements, how would the function call statement for imported module’s functions be affected?

Answer:
# tempConversion.py
“””Conversion functions between fahrenheit and centrigrade”””
# Functions
def to_centigrade(x):
“””Returns: x converted to centigrade”””
return 5*(x-32)/9.0
def to_fahrenheit(x):
“””Returns: x converted to fahrenheit”””
return 9*x/5.0 + 32
# Constants
FREEZING_C = 0.0 # water freezing temp.(in celsius)
FREEZING_F = 32.0 # water freezing temp.(in fahrenheit)
# programme
import tempConversion
print(tempConversion.to_centigrade(10)) # prefix is used in function call
from tempConversion import to_centigrade
print(to_centigrade(1)) # no prefix needed

Q.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:
It is so because of the way of writing import statements.
import Allchecks
allchecks.checkMain ( 3,’A’)
from Allchecks import checkMain
checkMain(4,’z’) # no prefix needed

Q.3.Given below is semi-complete code of a module basic.py:

Answer:
""" Basic calculations .py"""
def square(x):
    """ square of input"""    #1st fill up
    return mul(x,x)
def mul(x,y)
    """ multiply"""
    return x*y
def div(x,y)
    """Division"""
    return float (x)/y
def fdiv(x,y):
    """floor division"""
    return x//y
def floordiv(x,y):
    return fdiv(x,y)

Q.4.After importing the above module, some of its functions are executed as per following statements. Find errors, if any :
(a)Square(print 3)
Answer:error wrong argument passed square(3)
(b)Basic.div()
Answer: Function expects 2 arguments none is provided.
(c) Basic.floordiv(7.0,7)
Answer: No error
(d)div(100,0)
Answer:0 will raise error as its denominator
(e)basic.mul(3,5)
Answer: No error
(f)print(basic.square(3.5))
Answer: No error
(g)z=basics.div(13,3)
Answer: No error

Q.5.Import the above module basics.py and write statement for the following:
(a)Compute square of 19.23
basics.square( 19.23 )
(b)Compute floor division of 1000.01 with 100.23
basics.fdiv(1000.01,100.23)
(c)compute product of 3,4,5
basics.mul(4, basic.mul(3,5))
(d)What is the difference between basics.div(100,0) and basics.div(0,100)?
Error with basics.div(100,0) as denominator is zero.NO error with basics.div(0,100 ).

Q.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: Non-zero value will be printed . As the random function generate 0 to 1 value randomly in float format . Two random values can come as same but have little probability of this .Output will be in -1 to +1.

Q.7.What are the possible outcomes executed from the following code? Also specify the maximum and minimum values that can be assigned to variable NUMBER. Answer:

Answer:
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
i)ES#NE#IO
ii)LE#NO#ON#
iii)NS#IE#LO#
iv)EC#NB#IS#
Answer:
The first character will be E,fourth character will be N and seventh character will be I.THE 2ND , 5TH , 8 TH Characters would be contagious sequence of string. Hence the possible outcome
 i)ES#NE#IO   iv)EC#NB#IS# 
The while loop will run three times and then string[N] will become 'L'.Hence the maximum value will become 2+2+2=6.

Q.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))

i)20 22 24 25
ii)22 23 24 25
iii)23 24 23 24
iv)21 21 21 21
Answer: iii) and iv)
Highest 24
lowest 20

Q.9.Consider the following code:
import random

print(100+random.randint(5,10)*5),end=’ ‘)
print(100+random.randint(5,10)*5),end=’ ‘)
print(100+random.randint(5,10)*5),end=’ ‘)
print(100+random.randint(5,10)*5),end=’ ‘)

i)102 105 104 105
ii)110 103 104 105
iii)105 107 105 110
iv)110 105 105 110
Answer:
(iii)&(iv)
highest 110
lowest 105

Q.10.Consider the following package

Using Python Libraries Class 12 Solutions

(a)
import music.formats.wavwrite
music.formats.wavwrite.writefile()
(b)
from music.formats import wavwrite
wavwrite.writefile()

Q.11. What are the possible outcome executed from the following code?Also specify the maximum and minimumm values that can be assigned to variable PICKER.
import random
PICK = random.randint (0, 3)
CITY = [“DELHI”, “MUMBAI”, “CHENNAI”, “KOLKATA”]
for I in CITY :
for j in range(1, PICK):
print(I, end =” “)
print()

(i)
DELHIDELHI
MUMBAIMUMBAI
CHENNAICHENNAI
KOLKATAKOLKATA
(ii)
DELHI
DELHIMUMBAI
DELHIMUMBAICHENNAI
(iii)
DELHI
MUMBAI
CHENNAI
KOLKATA
(iv)
DELHI
MUMBAIMUMBAI
KOLKATAKOLKATAKOLKATA

Answer = Option (i) & (iii) will generate only.
Highest value is 3
Least value is 0

Using Python Libraries Class 12 : Solved Assignment for Computer Science Students

Programming Practice/Knowledge based Questions

1.Create a module lengthconversion.py that stores functions for various lengths conversion e.g.,
Answer:

miletokm()     to convert miles to kilometers
kmtomile()     to convert kilometers to miles
feettoinches()
inchestofeet() 
It should also store constant values such as value of (mile in kilometers and vice versa).

Answer: 
i=0
one_mile = 1.6
one_feet = 12

def miletokm(x)  :
    return x * one_mile

def kmtomile(x) :


    return x / one_mile
def feettoinches( x ):


    return x / one_feet

def inchestofeet(x) :


    return x * one_feet
#print(i)

Q2. Create a module MassConversion.py that stores function for mass conversion e.g ,

·      A kgtotonne( ) to convert kg to tonnes
·      A tonnetokg() to convert tonne to kg
·      A kgtopound( ) to convert kg to pound
·      A poundtokg()to convert pound to kg 

 (Also store constants 1 kg – 0.001 tonne, 1 kg = 2.20462 pound)
Help( ) function should give proper information about the module.

Answer:
i=0
tonnes = 0.001
pound = 2.20462


def  kgtotonne( x ):

    
    return x * tonnes



def tonnetokg( x ) :

    
    return x / tonnes

def  kgtopound ( x ):

    
    return x * pound

#print(i)
def  poundtokg( x ) :

    
    return x / pound
Q3. Create a package from above two modules as this:
Conversion
        |______Length______Lengthconversion.py
|______ Mass_______Massconversion.py
Make sure that above package meets the requirements of being a Python package. Also, you should be able to import above package and/or its modules using import command.

Answer
The directory structure would be same, just add the __init__.py files
Conversion
__init__.py
Length
__init__.py
Lengthconversion.py
Mass
__init__.py
Massconversion.py

Thanks for read article on Using Python Libraries Class 12 Solutions for CBSE Computer Science Students.

Leave a Comment

Your email address will not be published.