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

python modules-1

The document provides an introduction to Python modules, focusing on the math, random, and statistics modules. It includes explanations of various functions, examples of code usage, and possible outputs from specific code snippets. Additionally, it covers mathematical constants and functions, such as pi and e, along with statistical calculations like mean, median, and mode.

Uploaded by

guru.s.shandilya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

python modules-1

The document provides an introduction to Python modules, focusing on the math, random, and statistics modules. It includes explanations of various functions, examples of code usage, and possible outputs from specific code snippets. Additionally, it covers mathematical constants and functions, such as pi and e, along with statistical calculations like mean, median, and mode.

Uploaded by

guru.s.shandilya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

INTRO.

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 :

 Guess=65 and for loop will execute 4 times


 1st time i= 1, New can get value as 65 as randrange( ) will pick 0 as it can
pick value in range of 0 & 1 chr(New), will convert it in its
equivalent character i.e. A. S0 (iii) & (iv) can’t possible.
 2nd time i= 2, New can get value as 65 or 66 as randrange( ) can pick 0 or
1 as it can pick value in range of 0 & 2 chr(New), will convert it in its
equivalent character i.e. A or B and so (ii) is also not possible.

# Correct Output Options


i. A B B C
Q.3
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
RN=random.randrange(0,4)+5
for I in range (1,RN):
print(I,end= ‘ ‘)
#Output Options
i. 0 1 2 iii. 12345678
ii. 4 5 6 7 8 9 iv. 5 6 7 8 9 10 11 12
Explanation:
RN=random.randrange(0,4)+5
 As per above statement RN can get 5, 6, 7 or 8
for I in range (1,RN)
 This loop is start--ing from 1 and moving till RN
Possible outputs can be 1 2 3 4, 1 2 3 4 5 , 1 2 3 4 5 6 or 1 2 3 4 5 6
7
#Correct Output Option
No option is correct
In this case, must explain the working
Q.4
In the following Python program what is the expected value of
Myscore from Options (i) to (iv) given below. Justify your answer.
import random
Score= [25,20,34,56, 72, 63]
Myscore = Score[2 + random.randint(0,2)]
print(Myscore)
#Output Options
(i) 25 (ii) 34
(iii) 20 (iv) None of the above

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

#Correct Output Options:


ii. CHN:KOL:CHN: iv. KOL:CHN:KOL:

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:

i. 50#51#52#53#54#55# iii. 53#54#55#


Q.8
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
NAV = ["LEFT","FRONT","RIGHT","BACK"]
NUM = random.randint(1,3)
NAVG = " "
for C in range (NUM,1,-1):
NAVG = NAVG+NAV[C]
print (NAVG )
#Output Options:
(i) BACKRIGHT (ii) BACKRIGHTFRONT
(iii) BACK (iv) LEFTFRONTRIGHT

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

#Correct Output Option:


(i) BACKRIGHT
Python - Statistics Module

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:

>>> import statistics


>>> statistics.mean([2,5,6,9])
5.5

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

ceil(x) Returns the smallest integer greater than or equal to x.

fabs(x) Returns the absolute value of x


floor(x) Returns the largest integer less than or equal to x

exp(x) Returns e**x

pow(x,y) Returns x raised to the power y

sqrt(x) Returns the square root of x

cos(x) Returns the cosine of x

sin(x) Returns the sine of x

tan(x) Returns the tangent of x

Mathematical constant, the ratio of circumference of a circle to it's diameter


pi
(3.14159...)

e mathematical constant e (2.71828...)


Python - Math Module

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: Pie Value

>>> import math


>>>math.pi
3.141592653589793

Another well-known mathematical constant defined in the math module is e. It is


called Euler's number and it is a base of the natural logarithm. Its value is
2.718281828459045.

Example: e Value

>>> import math


>>> math.e
2.718281828459045

The following statements show sin, cos and tan ratios for the angle of 30 degrees
(0.5235987755982988 radians):

Example: sin, cos, tan Calculation


>>> import math
>>> math.sin(0.5235987755982988)
0.49999999999999994
>>> math.cos(0.5235987755982988)
0.8660254037844387
>>> math.tan(0.5235987755982988)
0.5773502691896257

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

This can be verified by the exponent operator.

Example: Exponent Operator **

>>> import math


>>>math.e**10
22026.465794806703

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 math.sqrt() method returns the square root of a given number.

Example: Square Root

>>> import math


>>> math.sqrt(100)
10.0
>>> math.sqrt(3)
1.7320508075688772

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.

Example: Ceil and Floor


Copy
>>> import math
>>> math.ceil(4.5867)
5
>>> math.floor(4.5687)
4
Example:
import math
a=25
b=2
print(math.pow(a,b))
#625.0
print(math.sqrt(a))
#5.0
print(math.pi)
#3.141592653589793
print(math.e)
#2.718281828459045
print(math.fabs(-2.5))
#2.5
print(math.ceil(2.5678))
#3
print(math.ceil(2.4678))
#3
print(math.floor(2.4678))
#2
print(math.sin(244.567))
#-0.4593177485798059
print(math.cos(244.567))
#0.8882720336921445
print(math.tan(244.567))
#-0.5170913089210184

You might also like