0% found this document useful (0 votes)
10 views

Worksheet Python Modules

Python

Uploaded by

Pratibha jain
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Worksheet Python Modules

Python

Uploaded by

Pratibha jain
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

INTRODUCTION TO PYTHON MODULES

1. Find the correct output(s) of the following code. Also write the maximum and
minimum values that can be assigned to variable Y.
import random
X=random.random()
Y=random.randint(0,4)
print (int(X),":",Y+int(X))
(A) 0:0
(B) 1:6
(C) 2:4
(D) 0:3
Ans. (A) and(D) are possible outputs. Maximum:0 Minimum:4
2.

Ans. a)W# b) W#s#

3. Identify the correct output(s) of the following code. Also write the minimum
and the maximum possible values of the variable Lot
import random
word='Inspiration'
Lot=2*random.randint(2,4)
for i in range(Lot,len(word),3):
print(word[i],end='$')
i) i$a$i$n$ ii) i$n$ iii) i$t$n$ iv) a$i$n$
Ans. Minimum value possible for Lot: 4
Maximum value possible for Lot: 8
Possible outputs are : iii)
Page 1 of 5
4. 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):
print (AR[K],end=”#“)
(i)10#40#70#
(ii)30#40#50#
(iii)50#60#70#
(iv)40#50#70#

Ans. (ii)30#40#50#
Maximum value assigned to FROM is 3
Maximum value assigned to TO is 4

5. Identify the correct output(s) of the following code. Also write the minimum
and the maximum possible values of the variable b.
import random
text = "Adventure"
b = random.randint(1, 5)
for i in range(0, b):
print(text[i], end='*')

(A) A* (B) A*D*


(C) A*d*v* (D) A*d*v*e*n*t*u*
Ans. Minimum possible value of b: 1
Maximum possible value of b: 5
Possible Outputs : (A) and ( C )

6. What possible outputs are expected to be displayed on the screen at the time
of execution of the program from the following code?
import random
temp=[10,20,30,40,50,60]
c=random.randint(0,4)
for i in range(0, c):
print(temp[i],”#”)
a) 10#20# b) 10#20#30#40#50#
c) 10#20#30# d) 50#60#

Ans. a) 10#20# b) 10#20#30#40#50# c) 10#20#30#

Page 2 of 5
7. Find possible o/p (s) at the time of execution of the program from the
following code? Also specify the maximum values of variables Lower and
Upper.
import random as r
AR=[20, 30, 40, 50, 60, 70];
Lower =r.randint(1,3)
Upper =r.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#

Lower = r.randint(1,3) means Lower will have value 1,2, or 3


Upper =r.randint(2,4) means Upper will have value 2, 3, or 4
So K will be from (1, 2, 3) to (2, 3, 4)
Means if K=1, then upper limit (2,3,4)
If K=2, then upper limit (2,3,4)
If K=3, then upper limit (2,3,4)
So correct answer (ii) 30#40#50#
Maximum values of variables Lower and Upper are 3 and 4.

8. Identify the correct output(s) of the following code and write the minimum
and the maximum possible values of the variable b.
import random
a="ComputerScience"
I=0
while (I<3):
b=random.randint(1,len(a)-1)
print(a[b],end='$')
I+=2
A) C$m$ B) m$p$ C) c$n$ D)c$e$c$
Ans. B) m$p$ C) c$n$
Value of b minimum 1 and maximum 14

Page 3 of 5
9. 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
values that can be assigned to each of the variables BEGIN and LAST.
import random
VALUES = [10, 20, 30, 40, 50, 60, 70, 80]
BEGIN = random.randint (1, 3)
LAST = random.randint(2, 4)
for I in range (BEGIN, LAST+1):
print (VALUES[I], end = "-")
(i) 30-40-50- (ii) 10-20-30-40-
(iii) 30-40-50-60- (iv) 30-40-50-60-70-
Ans. (i)

10. 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 BEG and END.
import random
heights=[10,20,30,40,50]
beg=random.randint(0,2)
end=random.randint(2,4)
for x in range(beg,end):
print(heights[x],end=’@’)
(a) 30 @ (b) 10@20@30@40@50@ (c) 20@30 (d) 40@30@
Ans. (a)

Page 4 of 5
11. 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
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="&")
(i) INDIGO&BLUE&GREEN& (ii) VIOLET&INDIGO&BLUE&
(iii) BLUE&GREEN&YELLOW& (iv) GREEN&YELLOW&ORANGE&
Ans. (i)

12. What possible outputs(s) are expected to be displayed on screen at the time of
execution of the program from the following code. Select which option/s
is/are correct.
import random
print(random.randint(15,25) , end=' ')
print((100) + random.randint(15,25) , end = ' ' )
print((100) -random.randint(15,25) , end = ' ' )
print((100) *random.randint(15,25) )

(i) 15 122 84 2500 (ii) 21 120 76 1500


(iii) 105 107 105 1800 (iv) 110 105 105 1900

Ans. (i) and (ii)

Page 5 of 5

You might also like