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?
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.
Docstrings are triple quoted comments ; useful for documentation purposes. For documentation, the docstrings should be the first string stored inside a module/class.
“””Returns: x converted to centigrade”””
Q.3. What is a package ? How a package different from module?
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.
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 ?
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?
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?
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.
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.
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.
Q.10. What is the usage of help() and dir() functions.
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:
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?
Name classes occur.
Q.14.What do you understand by standard library of python?
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.
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?
It is so because of the way of writing import statements.
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 :
Answer:error wrong argument passed square(3)
Answer: Function expects 2 arguments none is provided.
Answer: No error
Answer:0 will raise error as its denominator
Answer: No error
Answer: No error
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
(c)compute product of 3,4,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:
i)20 22 24 25
ii)22 23 24 25
iii)23 24 23 24
iv)21 21 21 21
Answer: iii) and iv)
Q.9.Consider the following code:
i)102 105 104 105
ii)110 103 104 105
iii)105 107 105 110
iv)110 105 105 110
Q.10.Consider the following package
from music.formats import wavwrite
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.
PICK = random.randint (0, 3)
CITY = [“DELHI”, “MUMBAI”, “CHENNAI”, “KOLKATA”]
for I in CITY :
for j in range(1, PICK):
print(I, end =” “)
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.,
miletokm() to convert miles to kilometers
kmtomile() to convert kilometers to miles
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.
The directory structure would be same, just add the __init__.py files
Thanks for read article on Using Python Libraries Class 12 Solutions for CBSE Computer Science Students.