Python Libraries Final
Python Libraries Final
in a
Python Program
CLASS-XI
• If you want to use the definitions inside a module, you need to first
import the module in your program.
If you want to import some selected items, not all from a module, then
you can use from <module> import used as:
from <module> import <objectname> [, <objectname> [,. . . . ] ] |*
To import single object:
To import constant pi from math module we can write:
from math import pi
To import multiple object:
To import two functions sqrt() and pow() from math module we can write:
from math import sqrt, pow
To import all objects of a module:
from <module name> import *
Using random module:
● Python has a module named random that provides random number generators.
● To use random number generators in the program, first we need to import the
import random
Random number generator functions:
1. Given the following python code, which is repeated four times. What could be the
possible set of outputs out of given four sets (dddd represent any combination of
digits) ?
>> import random
>> print(15 + random.random() * 5)
Option (ii) & (iv) are the correct possible output because:
(a) random() generates number N between range 0.0 < N < 1.0
(b) when it is multiplied with 5, the range becomes 0.0 to < 5
(c) when 15 is added to it, the range becomes 15 to < 20
only option (ii) & (iv) fulfill the condition of range 15 to < 20
Examples:
2. What could be the minimum and maximum numbers by the following code?
>> import random
>> print(random.randint(3, 10) - 3)
Explanation:
3. In a school fest, three randomly chosen students out of 100 students(having roll
number 1-100) have to represent bouquets to the guests. Help the school
authorities choose three students randomly.
Solution:
import random
s1 = random.randint(1, 100)
s2 = random.randint(1, 100)
s3 = random.randint(1, 100)
print(s1, s2, s3)
for i in range(3):
print(random.randint(1, 100))
1. Which of the following is not a function/method of the random module in Python
2. Identify the correct possible output for the following Python code:
import random
for N in range(2,5,2) :
print(random.randrange(1,N),end="#")
What could be the possible outputs out of the given four choices? Also, write the
minimum & maximum value that can be generated.
(i) 102 105 104 105 (ii) 110 103 104 105
(iii) 105 107 105 110 (iv) 110 105 105 110
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) ) )
(a) What is the minimum and maximum number of times the loop will execute?
(b) Find out, which line of output(s) out of (i) to (iv) will not be expected from the
program?
(a) What is the minimum and maximum value assigned to the variable ToGo?
(b) Find out, which line of output(s) out of (i) to (iv) will not be expected from
the program?
(a) What is the minimum and maximum value assigned to variable FLY?
(b) Find out, which line of output(s) out of (i) to (iv) will not be expected from the
program?
a. Delhi#Mumbai#Chennai#Kolkata#
b. Mumbai#Chennai#Kolkata#Mumbai#
c. Mumbai# Mumbai #Mumbai # Delhi#
d. Mumbai# Mumbai #Chennai # Mumbai
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 Lower and Upper.
import random
AR=[20,30,40,50,60,70]
Lower=random.randint(1,3)
Upper =random.randint(2,4)
for K in range(Lower, Upper +1):
print (AR[K],end=”#“)
(a) What is the minimum and maximum value assigned to the variable Number?
(b) Find out, which line of output(s) out of (i) to (iv) will not be expected from the
program?
#Output Options:
i. 29:26:25 :28: ii. 24: 28:25 :26 : iii.29:26:24 :28: iv. 29:26:25 :26 :
What possible output(s) are expected to be displayed on screen at the time of
execution of the program from the following code? Also specify the minimum
and maximum values that can be assigned to the variable End.
import random
Colours=["VIOLET","INDIGO","BLUE","GREEN","YELLOW","ORANGE","RED"]
End = randrange(2)+3
Begin = randrange(End)+1
for i in range(Begin,End):
print(Colours[i],end="&")
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
What could be the possible outputs out of the given four choices? Also, write the
minimum & maximum value that can be generated.
(i) 20 22 24 25 (ii) 22 23 24 25 (iii) 23 24 23 24 (iv) 21 21 21 21
What are the possible output(s) of the following code? Also specify the maximum and
minimum values that can be assigned to variable PICK.
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()
import random
AR = [ 20,30,40,50,60,70 ]
Lower = random.randint(1,3)
Upper = random.randint(2,4)
for K in range(Lower, Upper +1):
print ( AR[K] , end = ”#“ )