Using Python Libraries || Important Questions || Sumita-Arora || Preeti-Arora || Computer Science || Class 12



Q. Python has certain functions that you can readily use without having to write any special code. What types of functions are these?


Q. What is a Python module? What is its significance?



Q. What are docstrings? How are they useful?


Q. What is the utility of Python standard library math module and random module?



Q. What is a module list? Give at least two reasons why we need modules.


Q. Why is the use of import all statement not recommended?


Q. How do you create your own package in Python?


Q. How will you share global variables across modules?


Q. What is a Python module?


Q. What is the significance of assigning namespace to Python modules?


Q. What is namespace in Python?


Q. What is the difference between import statement and from import statement?


Q. What are the rules for local and global variables in Python?


Q. Define 'module' and 'package'.


Q. Why is a banner saying "RESTART" always displayed in Python module/program execution?


Q. What is a module, package and a library?


Q. Write a program to print the number of occurrences of a substring into a line using built-in string function find().


Q. What do you understand by local and global scope of variables? How can you access a global variable inside the function, if function has a variable with the same name?


Q. Write a method in Python to find and display the prime numbers between 2 to N. Pass argument to the method.


Q. Write definition of a method ZeroEnding(SCORES) to add all those values in the list of SCORES, which are ending with zero (0) and display the sum.
For example,
If the SCORES contain [200,456,300,100,234,678]
The sum should be displayed as 600



Q. Write a program with a user-defined function with string as a parameter which replaces all vowels in the string with "*".


Q. What is a Python module? What is its significance?



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

import random
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#



Q. What is the utility of built-in function help()?


Q. Write a module to input total number of days and find the total number of months and remaining days after months, and display it in another program.


Q. What happens when Python encounters an import statement in a program? What would happen, if there is one more import statement for the same module, already imported in the same program?


Q. What is the procedure to create own library/package in Python?


Q. Why is a package attached to site-packages folder of Python installation? Can't we use it without doing so?


Q. The random() function generates a random floating point number in the range 0.0 to 1.0 and randint(a, b) function generates random integer between range a to b. To generate random numbers between range a to b using random(), following formula is used:

math.random()*(b-a) + a
Now if we have following two statements (carefully have a look)

(i) int ( (math.random()* (b-a) + a))
(ii) math.randint (a, b)
Can we say above two statements are now producing random integers from the same range ? Why?



Q. Name the Python Library modules which need to be imported to invoke the following functions:
(i) sin()
(ii) randint ()



Q. Consider a module 'simple' given below:

# module simple.py
"""Greets or scolds on call"""
def greet():
    """Greet anyone you like :-)"""
    print ("Helloz")
def scold():
    """Use me for scolding, but scolding is not good :-("""
    print("Get lost")

count = 10
print("greeting or scolding - is it simple ?")
Another program 'test.py' imports this module. The code inside test.py is:-
#test.py
import simple
print (simple.count)
What would be the output produced, if we run the program test.py? Justify your answer.



Q. What would be the output produced by the following code:

import math
import random
print (math.ceil(random.random()))
Justify your answer.



Q. Consider the following code:

import math
import random
print (str(int (math.pow( random.randint (2, 4), 2))), end = ' ')
print (str(int (math.pow( random.randint (2, 4), 2))), end = ' ')
print (str(int (math.pow( random.randint (2,4), 2))))

What could be the possible outputs out of the given four choices?
(i) 234
(ii) 944
(iii) 16 16 16
(iv) 249
(v) 494
(vi) 444



Q. What possible outputs(s) are expected to be displayed on screen at the time of execution of the program from the following code ? Also specify the maximum values that can be assigned to each of the variables FROM and TO.

import random
AR = [20,30,40,50,60,70];
FROM = random.randint(1,3)
TO = random.randint (2,4)
for K in range (FROM, TO + 1):
    print (AR[K], end = "#")
(i) 10#40#70#
(ii) 30#40#50#
(iii) 50#60#70#
(iv) 40#50#70#



Q. What is the output of the following piece of code?
#mod1
def change(a):
    b = [x*2 for x in a]
    print (b)
#mod2
def change(a):
    b = [x*x for x in a]
    print (b)
from mod1 import change
from mod2 import change
#main
s = [1,2,3]
change(s)



Q. What is the problem in the following piece of code ?
from math import factorial
print (math.factorial (5))



Q. Write a random number generator that generates random numbers between 1 and 6 (simulates a dice).

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post