python modules-1
python modules-1
TO PYTHON MODULE
Syllabus:
Introduction to Python modules: Importing math module (pi, e, sqrt, ceil, floor,
pow, fabs, sin, cos, tan); random module (random, randint, randrange), statistics
module (mean, median, mode).
Random module:
1. random():this function is used to generate a random number between 0 and 1.
Ex. random.random()
2. randint(a,b):this function takes the minimum value as a and maxm. Value as b.
so picks any number between a and b( a<=N<=b)
Ex: a=random.randint(3,5)
min value of a=3 and maxm value of a=5
it will generate the random no. between 3
and 5.
3. randrange(a,b):this function will take minimum value as a and maxm. Value as
b-1.so picks any number between a and b-1(a<=N<=b-1)
Ex: a=random.randrange(3,10)
min value of a=3 and maxm value of a=9
it will generate the random no. between 3
and 9.
.
Q. 1
In the following program, if the value of N given by the user is 20, what
maximum and minimum values the program could possibly display?
import random
N=int (input(“Enter any number”))
Guessnum=random.randint(1,N-10)+10
print(Guessnum)
Explanation:
As per question N=20
Guessnum=random.randint(1,N-10)+10
randint() can pick any number between 1 and 10 after that 10 is
added to the values
So min value picked by randint() is 1 and max value picked is 10
i.e. 1+10 or 10+10
print(Guessnum)
It can print
Min Value = 11 Max Value = 20
Q.2
Go through the Python code shown below, and find out the
possible output or outputs from the suggested Output Options (i)
to (iv).
import random
Guess=65
for I in range (1,5):
New =Guess +random.randrange(0,I)
print(chr(New),end=’ ‘)
#Output Options
i. A B B C ii. A C B A
iii. B C D A iv.C A B D
Explanation :
Explanation:
Myscore = Score[2 + random.randint(0,2)]
In the above statement randint() can take 0 or 1 which will
be added to 2, i.e. score[2] or score[3] can be printed. It
means 34 or 56 can be printed.
# Correct Output Option
(ii) 34
Q.5
Go through the Python code shown below, and find out the
possible output or outputs from the suggested Output Options (i)
to (iv). Justify your answer.
import random
Max=3
Div=1+random.randrange(0,Max)
for N in range (1,5):
print (100%Div,end= “#”)
print ()
#Output Options
i. 0#0#0#0# ii. 1#1#1#1#
iii. 2#2#2#2# iv. 3#3#3#3#
Explanation:
Div=1+random.randrange(0,3) As per this Div can take 1,2 or 3
for N in range (1,5): Loop will execute 4 times
Div=1 than print (100%Div,end= “#”) will result in 0#0#0#0#
Div=2 than print (100%Div,end= “#”) will result in 0#0#0#0#
Div=3 than print (100%Div,end= “#”) will result in 1#1#1#1#
#Correct Output Options
i. 0#0#0#0# ii. 1#1#1#1#
Q.6
Go through the Python code shown below, and find out the
possible output or outputs from the suggested Output Options (i)
to (iv). Justify your answer.
import random
city= [ “DEL” , “ CHN” , “KOL” , “BOM” ,“BNG”]
for I in range (1,4):
Fly = random.randrange(0,2)+1
print (city[Fly],end= “:”)
print ()
#Output Options:
i. DEL:CHN:KOL: ii. CHN:KOL:CHN:
iii. KOL:BOM:BNG: iv. KOL:CHN:KOL:
Explanation:
Fly = random.randrange(0,2)+1 According to this fly can take
value 1 or 2
for I in range (1,4): Loop will print 3 times
city= [ “DEL” , “ CHN” , “KOL” , “BOM” ,“BNG”]
As per value of Fly used as Indexing in list city combination of CHN
or KOL will be printed
Q.7
Go through the Python code shown below, and find out the
possible output or outputs from the suggested Output Options (i)
to (iv). Justify your answer.
import random
High =4
Guess=random.randrange(0,High)+50
for C in range (Guess,56):
print(C,end= “#”)
#Output Options:
i. 50#51#52#53#54#55# ii. 52#53#54#55#
iii.53#54#55# iv. 51#52#53#54#55
Explanation :
High=4, Guess=random,randrange(0,High)+50: As per the
statements, Guess can take values 50,51,52 or 53
for C in range (Guess,56): so loop can start from 50,51,52 or 53
and will move till 56 to print values.
# Correct Output Options:
Explanation
NUM = random.randint(1,3)
As per the statements, NUM can take values 1, 2 or 3
for C in range (NUM,1,-1 ) : So loop can start from 3, 2 or 1 and will
move till 1 to print values
NUM=3 BACKRIGHT
NUM=2 RIGHT
NUM=1 Loop will not execute
The statistics module provides functions to mathematical statistics of numeric data. The
following popular statistical functions are defined in this module.
Mean
The mean() method calculates the arithmetic mean of the numbers in a list.
Example:
Median
The median() method returns the middle value of numeric data in a list.
Example:
>>> import statistics
>>> statistics.median([1,2,3,8,9])
3
>>> statistics.median([1,2,3,7,8,9])
5.0
Mode
The mode() method returns the most common data point in the list.
Example:
>>> import statistics
>>> statistics.mode([2,5,3,2,8,3,9,4,2,5,6])
2
Functions in Python Math Module
Here is the list of all the functions and attributes defined in math module with a brief explanation
of what they do.
List of Functions in Python Math Module
Function Description
Some of the most popular mathematical functions are defined in the math module. These
include trigonometric functions, representation functions, logarithmic functions, angle
conversion functions, etc. In addition, two mathematical constants are also defined in this
module.
Pie (π) is a well-known mathematical constant, which is defined as the ratio of the
circumference to the diameter of a circle and its value is 3.141592653589793.
Example: e Value
The following statements show sin, cos and tan ratios for the angle of 30 degrees
(0.5235987755982988 radians):
You may recall that sin(30)=0.5, cos(30)=32 (which is 0.8660254037844387) and tan(30)=
13 (which is 0.5773502691896257).
math.exp()
The math.exp() method returns a float number after raising e to the power of the given
number. In other words, exp(x) gives e**x.
Example: Exponent
>>> import math
>>>math.exp(10)
1.0
math.pow()
The math.pow() method receives two float arguments, raises the first to the second and
returns the result. In other words, pow(4,4) is equivalent to 4**4.
Example: Power
>>> import math
>>> math.pow(2,4)
16.0
>>> 2**4
16
math.sqrt()
The following two functions are called representation functions. The ceil() function
approximates the given number to the smallest integer, greater than or equal to the given
floating point number. The floor() function returns the largest integer less than or equal to
the given number.