0% found this document useful (1 vote)
247 views

Worksheet - Random Module

C.s

Uploaded by

Sana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
247 views

Worksheet - Random Module

C.s

Uploaded by

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

WORKSHEET- RANDOM MODULE

1. What will be the output of the following Python code?


random.randrange(0,91,5)
a) 10 b) 18 c) 79 d) 95
2. What are the possible outcomes for the following code?
import random
PICK=random.randint (1,3)
CITY= ["DELHI", "MUMBAI", "CHENNAI", "KOLKATA"]
for I in CITY:
for J in range (0, PICK):
print (I, end = "")
print ()
(i) DELHIDELHI (ii) DELHI
MUMBAIMUMBAI DELHIMUMBAI
CHENNAICHENNAI DELHIMUMBAICHENNAI
KOLKATAKOLKATA
(ii ) DELHI (iv) DELHI
MUMBAI MUMBAIMUMBAI
CHENNAI KOLKATAKOLKATAKOLKATA
KOLKATA
3. What are the possible outcome(s) from the following code? Also, specify the maximum and
minimum values that can be assigned to variable SEL.
import random
SEL=random. randint (1, 3)
ANIMAL = ["DEER", "Monkey", "COW", "Kangaroo"]
for A in ANIMAL:
for AA in range (0, SEL):
print (A, end ="")
print ()
(i) DEERDEER (ii) DEER
MONKEYMONKEY DEERMONKEY
COWCOW DEERMONKEYCOW
KANGAROOKANGAROO
(ii ) DEER (iv) DEER
MONKEY MONKEYMONKEY
COW KANGAROOKANGAROOKANGAROO
KANGAROO
4. What are the possible outcomes executed from the following code?
import random
def main():
p = "MY PROGRAM"
i=0
while p[i] != "R":
l = random.randint(0,3) + 5
print (p[l],end ="_")
i += 1
main()
(i) M_M_Y_P (ii) R_G_A_G (iii) G_G_R_O (iv) O_G_G_A
5. Which is the correct outcome executed from the following code?
import random
n1= random.randrange(1, 10, 2)
n2= random.randrange(1, 10, 3)
print (n1,"and",n2)
(i) 2 and 7 (ii) 3 and 4 (iii) 8 and 10 (iv) 8 and 9
6. What could be the minimum and maximum possible numbers by the following code?
import random
print(random.randint(15,30)-7)
7. Find the suggested output options. Also write the least and highest values generated.
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
8. What possible output to be displayed on the screen at the time of execution of the following
program? Also specify the minimum and 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=’#’)
(i) 10#40#70# (ii) 30#40#50# (iii) 50#60#70# (iv) 40#50#70
9. What possible output to be displayed on the screen at the time of execution of the following
program? Also specify the minimum and maximum values that can be assigned to each of the
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#

10. What possible output to be displayed on the screen at the time of execution of the following
program? Also specify the minimum and maximum values that can be assigned to each of the
variable R when K is assigned the value as 2.
import random
Signal=[‘Stop’,’Wait’,’Go’]
for K in range(2,0,-1):
R=randrange(K)
Print(Signal[R], end=’#’)
(i) Stop#Wait#Go# (ii) wait#Stop# (iii) Go#Wait# (iv) Go#Stop#
11. What possible output to be displayed on the screen at the time of execution of the following
program? Also specify the minimum and maximum values that can be assigned to each of the
variable NUM.
import random
NAV=['LEFT','FRONT','RIGHT','BACK']
NUM=random.randint(1,3)
NAVG=""
for C in range(NUM,1,-1):
NAVG=NAVG+NAV[C]
print(NAVG)
(i) BACKRIGHT (ii) BACKRIGHTFRONT (iii) BACK (iv) LEFTFRONTRIGHT
12. What possible output to be displayed on the screen at the time of execution of the following
program?
import random
M=[5,10,15,20,25,30]
for i in range(1,3):
first=random.randint(2,5)-1
second=random.randint(3,6)-2
third=random.randint(1,4)
print(M[first],M[second],M[third],sep='#')

(a) 10#25#15 (b) 5#25#20 (c) 30#20#20 (d) 10#15#25#


20#25#25 25#20#15 20#25#25 15#20#10#

13. Which of the following output is valid for the below code.
from random import randrange
L = list()
for x in range(5):
L.append(randrange(0, 100, 2)-10)
print(L)
a) [-8, 88, 8, 58, 0] b) [-8, 81, 18, 46, 0] c) [-7, 88, 8, 58, 0] d) [-8, 88, 94, 58, 0]

14. Find the output generated by the below code.


from math import *
a = 2.13
b = 3.7777
c = -3.12
print(int(a), floor(b), ceil(c), fabs(c))

a) 2 3 -4 3 b) 2 3 -3 3.12 c) 2 4 -3 3 d) 2 3 -4 3.12

15. Name the Python Library modules which need to be imported to invoke the following
functions :
(a) load() (b) pow() (c) uniform() (d) fabs()
16. Differentiate between the round() and floor() functions with the help of suitable example.
The function round() is used to convert a fractional number into whole as the nearest next
whereas the function floor() is used convert to the nearest lower whole number, e.g.,
round (5.8) = 6, round (4.1) = 4 and floor (6.9) = 6, floor (5.01) = 5
round(4.50)=4, round(4.56)=5, round(4.5234,2)=4.52, round(4.396,2)=4.4
17. What is the difference between import and from import statement?
import statement import the entire module while from import statement import the selected
objects from a module.
18. What are docstrings? How they are useful?
19. What is the utility of python standard library math and random module?
20. Define module and package.

You might also like