Worksheet - Random Module
Worksheet - Random Module
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='#')
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]
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.