0% found this document useful (0 votes)
9 views10 pages

Python Module Functions and Outputs Guide

The document provides a comprehensive overview of Python modules, including how to import them, use random and statistics modules, and built-in math functions. It includes example code snippets and expected outputs for various scenarios involving random number generation and statistical calculations. Additionally, it outlines the maximum and minimum values for certain variables in the context of the given code examples.
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 (0 votes)
9 views10 pages

Python Module Functions and Outputs Guide

The document provides a comprehensive overview of Python modules, including how to import them, use random and statistics modules, and built-in math functions. It includes example code snippets and expected outputs for various scenarios involving random number generation and statistical calculations. Additionally, it outlines the maximum and minimum values for certain variables in the context of the given code examples.
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

RUBY PARK PUBLIC SCHOOL

CLASS XII
SUBJECT: COMPUTER SCIENCE
REVISION
BT1 RANDOM MODULE AND OTHER MODULE QUESTIONS

A module is a file containing Python definition, functions, variables, classes


and elements. The extension of these files are .py
Importing Python Modules:
Using import: import < module_name>
import random
[Link](3,5)
import <module 1>,<module2>…….<module3>
To access a particular method for any module:
from random import randint
randint(3,7)
Module aliasing:
import <modulename> as <aliasname>
import statistics as s
[Link]([2,3,4,5])
Random modules
random()-generates floating value between 0 and 1 (including 0 but
excluding 1). It does not require any argument. Eg: (0,1]
[Link]()
randrange()- returns an integer between lower and upper limit to this
method (excluding upper limit)
ex: [Link](a,b,stepvalue)
NOTE: donot take any float values
start Optional. An integer specifying at which position to start.
Default 0
difference between range() and randrange() – chooses any possible values
from random
randint(): takes two parameters a,b in which first one is lower and second
is upper limit. This function returns any integer number between the two
numbers, including both limits.
If lower and upper limit same then each time will give same value
[Link](2,2)
2
Statistics module:

mean(): it returns the arithmetic mean of data. (find outs the average)
Syntax: [Link](X): where X is a numeric sequence
import statistics
[Link]([2,3,4,5])
3.5

[Link]((6,7,8,9))
7.5
median(): it returns the median (middle value) of data.
If the number of elements in a sequence is:
ODD: the median will be the middle value
EVEN: The median will be the mean of the two central numbers.
NOTE : the data must first be sorted in ascending order.
mode(): it returns the mode ( the most repeated value)
L=[1,5,2,24,5]
[Link](L)
5
If there are multiple modes with same frequency, return the first one
encountered.
If the sequence is empty, mode() raises StatisticsError
L=[1,3,5,2,24,5,10,3]
[Link](L)
3
If no element is repeated, then mode will be first element
Mode can be applied in string also, as most repeated value it checks
(It works with non-numeric data also)
Math module: It mostly returns float value.
sqrt(x): it returns square root of x for x>=0
x may be a positive integer or a floating point number.
ceil(x): returns smallest number not less than x. x may be a positive integer
or a floating point number.
Ex: [Link](5.2)- above value is 6 , so ans is 6
6
[Link](-5.6): here in negative small value is big and big value is small
-5
[Link](5)
5
floor(x): returns largest number not more than x. x may be a positive
integer or a floating point number.
pow(x,y): x raised to the power of x. x,y may be integer or floating point
numbers.
NOTE: Answer comes in difference
Difference between pow() and [Link]()
Built in function: pow(x,y[,z])- x raised to the power of y, if z is provided,
then : (xy)%z
x,y,z may be integer or floating point number.
fabs(x): returns the absolute value of float x. x may be an integer or a
floating point number.
NOTE: It means a negative number will become positive, if any positive it
will remain positive.
degrees(x): Converts angle x from radians to degrees. x must be a numeric
value.
radians(x): Converts a degree value into radians. x must be a numeric
value.

1. What possible outputs(s) will be obtained when the following code is


executed?

Values : 0,1,2,3

Loop:
mynumber: If 0 – not run as start
value is 1
mynumber: If 1 – not run as start
value is 1 (same)
if i =2 : start 1, stop 2: so runs 1
so o/p: COLOR [1]=WHITE*
If i= 3 : start , stop 3: so I gets 1
and 2- runs two times
So COLOR[1]= WHITE*
COLOR[2]=BLACK*
ANOTHER POSSIBLE: NO OUTPUT
Ans: b. WHITE*
BLACK*

2. What possible output(s) are expected to be displayed on screen at the


time of execution of the following code? i=1, so loop runs once only, so option II and
III cannot be as two values then only came
two outputs.
f= 1,3 = 1,2,3
s=(2,4)= 2,3,4
f=1/2/3 , so it has to start with Pencil /
Options: Eraser / Bag
(I) Pencil:Book Pencil:Eraser
(II) Pencil:Book s=2/3/4 -> so it has to be Eraser / Bag / Book
Eraser:Bag
(III) Pen:Book
Bag:Book
(IV) Bag:Eraser
So, correct is option I and IV

[Link] possible output(s) are expected to be displayed on screen at the


time
of execution of the following program:
i will run : 1,2
first=( 2,3,4,5)-1 -> 1,2,3,4
sec= (3,4,5,6)-1 -> 2,3,4,5
third= (1,2,3,4)
Note : option ii – cannot be
as i=0 will not come so
M[0]=5 cannot be
Now, first and third will go till
4 so M[5] cannot be , i.e. 30
So option (iii) cannot be as
here first value is 30

Ans: option i So, correct coming option ii


and iv
But in option (iv) the # is at
the end which will not come,
as the sep is #, not end.
So correct is option i

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 and minimum value that can be assigned to the variable R when
K is assigned value as 2.

import random
Signal = [ 'Stop', 'Wait', 'Go' ]
for K in range(2, 0, -1):
K=2,1
R=rangrange(K)-> K=2,1 -> If K=2, R=0/1
R = [Link](K) If K=1, R=0

print (Signal[R], end = ' # ') So output can be either Stop# (if R=0)

(a) Stop # Wait # Go # R=0/1 or 1/0 -> Stop # Wait# or

(b) Wait # Stop # Wait # Stop #


(c) Go # Wait # K=0 cannot be so option Signal[0]=Stop , so option
(a) cannot be
(d) Go # Stop #
R cannot be 2, so Signal [2]= Go cannot be , so
option (c) and (d) cannot be as Go is there
So correct is option (b)
When K=2 , Max value of R= 1
Min value of R=0

5. 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.
End= randrange(2)+3->0,1->3,4
import random
Begin= randrange(End)+1-> End can be 3/4 -
Colours = ["VIOLET","INDIGO","BLUE", > If End = 3: Begin = 0/1/2 -> 1,2,3
"GREEN", "YELLOW","ORANGE","RED"] If End = 4: Begin = 0/1/2 /3-> 1,2,3,4
End = [Link](2)+3 So, Begin can be 1/2/3/4 So, Colours[1]/
Colours[2]/ Colours[3]/ Colours[4] –
Begin = [Link](End)+1
"INDIGO","BLUE",
for i in range(Begin,End):
"GREEN",
print(Colours[i],end="&")
"YELLOW"
(i) INDIGO&BLUE&GREEN&
So option (ii) cannot be as VIOLET is starting
(ii) VIOLET&INDIGO&BLUE& value
(iii) BLUE&GREEN&YELLOW& Now End can be 3/4-> will be one less as
(iv) GREEN&YELLOW&ORANGE& stop value -> 2 / 3 -> Colours[2]/ Colours[3]
->

"BLUE","GREEN"
So option (iii) and (iv) cannot be , already
option (ii) removed, so option (i)
Ans: option (i)
Max value of End =4
Min value of End=3

6. What will be the output of the following code?

a. Delhi#Mumbai#Chennai#Kolkata#
b. Mumbai#Chennai#Kolkata#Mumbai#
c. Mumbai# Mumbai #Mumbai # Delhi#
d. Mumbai# Mumbai #Chennai # Mumbai

Ans: option (b)

7. 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.
(i) 10#40#70# (ii) 30#40#50# (iii) 50#60#70# (iv) 40#50#70#

Ans: So correct is option (ii)


Max value of Upper -> 4, Max value of Lower -> 3

8. Observe the following Python code and find out , which out of the given
options i) to iv) are the expected correct output(s).Also assign the maximum
and minimum value that can be assigned to the variable ‘Go’.
Go=0/1/2/3
import random
i values: 0/1/2/3 : end will be one less -> will not
X =[100,75,10,125]
run/ 0 / 1/ 2
Go = [Link](0,3)
Here for i in range(Go): -> so whatever be
for i in range(Go): value of Go, first should be 0 so X[0] =100 ,
should start with 100 so option (i) is correct
print (X[i],"$$")
Min value of Go -> 0, Max value of Go -> 3
i. 100$$75$$10$$ ii. 75$$10$$125$$ iii. 75$$10$$ iv.10$$125$$100
End also $$ will come as “$$” it is end , not sep

Ans (i)
9. Observe the following Python code and find out , which out of the given
options i) to iv) are the expected correct output(s).Also assign the maximum
and minimum value that can be assigned to the variable ‘Num’.
NUM= 1/2/3

for i in range(NUM,1,-1) -> starts with 1/2/3 ->


"FRONT", "RIGHT", "BACK" -> so option (iv) cannot be
NUM=1,1,-1 -> no output, so cannot start with FRONT
NUM=2, 1 time, end =1, output : RIGHT
NUM=3 , end=2 , 2 times output: BACKRIGHT
So option i is correct, Max value Num=3, min=1
i. BACKRIGHT
[Link]
[Link]
iv. LEFTFRONTRIGHT

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
minimum values that can be assigned to each of the variables BEGIN and
LAST.

BEGIN= 1/2/3, LAST=1/2/3/4 OR 2/3/4 OR 3 /4


import random
I -> start value is BEGIN so -> values -> 20/30/40
VALUES=[10,20,30,40,50,60,70,80]
So option {ii} out
BEGIN=[Link](1,3)
End value of I -> LAST+1-> but one less than stop
LAST =[Link](BEGIN,4) 1/2/3/4 OR 2/3/4 OR 3 /4
for I in range(BEGIN,LAST+1): 20/30/40/50 or 30/40/50 or 40/50

print (VALUES[I],"-",) so last value cannot be 10/60/70/80 so option (iii) and


(iv) cannot be. So correct is option (i)

(i) 30 - 40 - 50 - (ii) 10 - 20 - 30 - 40 - BEGIN – Max: 3, Min 1, LAST : Max=4 , Min =1


(iii) 30 - 40 - 50 - 60 - (iv) 30 - 40 - 50 - 60 - 70 –

Ans: So correct is option (i)


BEGIN – Max: 3, Min 1, LAST : Max=4 , Min =1

You might also like