TOPIC : MATH MODULE
(MATHEMATICAL FUNCTIONS)
math module
(Mathematical Functions)
import math # To use any mathematical function import math module
ceil(n) Rounds a number up to the nearest integer.
import math import math
p = [Link](4.2) p = [Link](4.87)
print(p) print(p)
Output : 5 Output : 5
floor(n) Rounds a number down to the nearest integer.
import math import math
p = [Link](4.2) p = [Link](4.87)
print(p) print(p)
Output : 4 Output : 4
fabs(n) Return the absolute(positive) value of the specified number.
import math import math
p = [Link](-10) p = [Link](-4.5)
print(p) print(p)
Output : 10 Output : 4.5
math module
(Mathematical Functions)
pow(x,y) Return the value of xy.
import math import math
p = [Link](2,3) p = [Link](2,-2)
print(p) print(p)
Output : 8 Output : 0.25
sqrt(n) Return the square root of n.
import math import math
p = [Link](4) p = [Link](15)
print(p) print(p)
Output : 2 Output : 3.8729
[Link] It return the value of pi(∏)
import math
print([Link])
Output : 3.141592
math module
(Mathematical Functions)
sin(x) Return the sine of x in radians.
Note : pi/2 radians is converted to 90 degree and pi/3 is converted to 60 degree.
import math import math
p = [Link]([Link]/2) p = [Link]([Link]/3)
print(p) print(p)
Output : 1.0 Output : 0.866025
cos(x) Return the cosine of x in radians.
import math import math
p = [Link]([Link]/2) p = [Link]([Link]/3)
print(p) print(p)
tan(x) Return the tangent of x in radians.
import math import math
p = [Link]([Link]/2) p = [Link]([Link]/3)
print(p) print(p)
statistics module in python
Functions defined in statistics module
This module provides functions for calculating mathematical statistics of numeric (Real
valued) data. There are three functions in this module:
mean() , median() and mode()
1. mean() :- The arithmetic mean is the sum of the data divided by the number of values (i.e.
Average of the given set of values).
import statistics import statistics as st
a = [Link]([3,2,5]) a = [Link]([5,8,4,3])
print(a) print(a)
Output :- 3.3333333333 Output :- 5.0
Functions defined in statistics module
2. median() :- Return the median (middle value) of sorted numeric data if the number of
values are odd and if number of values are even than “mean of middle two values” is
evaluated. If data is empty, Statistics Error is raised.
Data must be in sorted order.
import statistics import statistics
a = [Link]([2,3,4,5,8]) a = [Link]([2,5,8,14,30,70])
print(a) print(a)
Output :- 4 Output :- 11
Functions defined in statistics module
3. mode() :- Return the most common(having more frequency) data point from discrete or
nominal data. If there is not exactly one most common value, then there are three
possible answers:
(i) Statistics Error is raised.
(ii) Return smallest number having same frequency
(iii) Return the number that comes first.
import statistics import statistics
a = [Link]([7,4,9,4,2,4,9]) a = [Link]([5,8,5,2,4,2])
print(a) print(a)
Output :- 4 Output :- Statistics Error or 2 or 5
What is module
and
How to import module in program
and
random module
Module
• A module is a file containing functions and variables defined in
separate files. i.e. it contains collection of related functions.
• With the help of modules it is easier to reuse the same code in
more than one program.
• If a set of functions is required in different programs than we can
place the definition of those functions in a module and than we
can import the module in each program that needs to call any one
of the function defined in the specified module.
• Once we import the module in any program than we can use any
one of the function defined in that module.
Importing Module in a program
• import statement is used to include the specified module in a
program.
Syntax : import original_module as module_name
• To use/access any of the function present in the imported
module, we have to follow the name of the module followed by
the function name separated by dot(.).
Syntax : module_name. Function(arguments)
Functions defined in random module
random module :- random(), randrange(), randint(), uniform(), choice()
All the functions defined in random modules are used to generate a number randomly.
Need of random number :- While programming some situations come when we have to
adopt non-deterministic approach. In these type of situations
random numbers are extensively used.
Examples:- Computer games like throwing dice etc
Recaptcha – login form
1. random() :- This function generates a random floating point number between 0 and 1
(including 0, but excluding 1)
x=random() then 0<=x<1 i.e. x=0.0 (min value) and x = 0.9999(max value)
import random import random as rd
a = [Link]() a = [Link]()
print(a) print(a)
Output :- 0.1245854 Output :- 0.8654254
Functions defined in random module
2. randrange() :- This function generates an integer between its lower and upper arguments.
By default lower argument is 0. (including lower value, but excluding upper)
Syntax:- p = [Link](x,y) then x<=p<y
Examples:-
import random import random
a = [Link](5,20) # 5<=a<20 a = [Link](10) # 0<=a<10
print(a) print(a)
Output :- 7 Output :- 8
3. randint() :- This function generates an integer from its lower to upper arguments
Syntax:- p = [Link](x , y) then x<=p<=y
Example:-
import random
a = [Link](11,20) # 11<=a<=20 i.e. a = 11,12,13,....20
print(a)
Output :- 13
Functions defined in random module
4. uniform() :- This function generates a floating point random number between its lower and
upper arguments. (including lower value, but excluding upper)
Syntax:- p = [Link](x, y) then x<=p<y Here p will be float
Examples:- import random
a = [Link](5,20) # 5<=a<20 i.e a = 5.123,5.234,6.23455, ........etc
print(a)
Output :- 8.1232445434
5. choice() :- This function is used for making a random selection from the specified sequence
like list, tuple or string.
Syntax:- p = [Link](list/tuple/string)
Example:- import random import random
L=[2,5,3,7] P=(9,3,5,8,2,1)
a = [Link](L) a = [Link](P)
print(a) print(a)
Output :- 5 Output :- 8
import random import random
P = “1212” P = “ABCD”
a = [Link](P) a = [Link](P)
print(a) print(a)
Output :- 1 Output :- A
Find the Output
Q.1 : Read the following code and find the possible output(s) from the given options.
import random as rd
Area = ["North","South","East", "West"]
for i in range(0,3):
t = [Link](0,2)
print(Area[t], end = “:")
(i) South:South:South
(ii) North:South:South
(iii)North:South:East
(iv) North:North:South
Find the Output
Q.1 : Read the following code and find the possible output(s) from the given options.
import random as rd
Area = ["North","South","East", "West"]
for i in range(0,3): #0,1,2
t = [Link](0,2) #0,1
print(Area[t], end = “:")
Answers: (i), (ii), (iv)
(i) South:South:South
(ii) North:South:South
(iii)North:South:East
(iv) North:North:South
FINDING OUTPUT OF GIVEN PROGRAM
(use of random(), randrange() and randint() functions)
Find the Output
Q.1 : Read the following code and find the possible output(s) from the given options.
import random as rd
Area = ["North","South","East", "West"]
for i in range(0,3):
t = [Link](0,2)
print(Area[t], end = “:")
(i) South:South:South:
(ii) North:South:South:
(iii)North:South:East:
(iv) North:North:South:
Find the Output
Q.1 : Read the following code and find the possible output(s) from the given options.
import random as rd
Area = ["North","South","East", "West"]
for i in range(0,3): #0,1,2
t = [Link](0,2) #0,1
print(Area[t], end = “:")
Answers: (i), (ii), (iv)
(i) South:South:South:
(ii) North:South:South:
(iii)North:South:East:
(iv) North:North:South:
Find the Output
Q.2 : Find the possible output(s) and write the min and max value of stop.
import random
begin = 3
for i in range(1,4):
stop = [Link](0,3)+6
print(begin,stop,“#”,end = "")
begin = begin+1
(i) 36#48#67#
(ii) 36#48#57#
(iii)37#49#57#
(iv) 38#48#56#
Find the Output
Q.2 : Find the possible output(s) and write the min amd max value of stop.
import random
begin = 3
for i in range(1,4): #1,2,3
stop = [Link](0,3)+6 #6,7,8
print(begin,stop,“#”,end = "")
begin = begin+1
Answers:(ii), (iv)
(i) 36#48#67#
(ii) 36#48#57#
(iii) 37#49#57#
(iv) 38#46#56#
Min value of stop = 0+6 = 6
Max value of stop = 2+6 = 8
Find the Output
Q.3 : Find the possible output(s) and also find the min and max value generated.
import random
print(int(20+[Link]()*5),end=“ ”)
print(int(20+[Link]()*5),end=“ ”)
print(int(20+[Link]()*5),end=“ ”)
print(int(20+[Link]()*5)
(i) 20 22 24 25
(ii) 22 22 22 24
(iii)24 23 22 20
(iv) 22 23 24 25
Find the Output
Q.3 : Find the possible output(s) and also find the min and max value generated.
import random
print(int(20+[Link]()*5),end=" ")
print(int(20+[Link]()*5),end=" ")
print(int(20+[Link]()*5),end=" ")
print(int(20+[Link]()*5))
Answers:(ii), (iii)
(i) 20 22 24 25
(ii) 22 22 22 24
(iii)24 23 22 20
(iv) 22 23 24 25
Note:- Here random() function can generate value 0.0 – 0.9
Min value = int(20+(0.0*5) = int(20+0.0)
= 20
Max value = int(20+(0.9*5) = int(20+4.5)
= int(24.5)
= 24
Find the Output
Q. 4 : Find the possible output(s) from the given options and also find the min and max
value of Pick.
import random
Pick = [Link](0,3)
City = [“DELHI",“MUMBAI",“CHENNAI", “KOLKATA"]
for i in City:
for j in range(1,Pick):
print(i, end = “ ”)
print()
(i) DELHI DELHI (ii) DELHI
MUMBAI MUMBAI MUMBAI
CHENNAI CHENNAI CHENNAI
KOLKATA KOLKATA KOLKATA
(ii) DELHI (iv)DELHI DELHI
MUMBAI DELHI MUMBAI
CHENNAI CHENNAI DELHI MUMBAI CHENNAI
KOLKATA KOLKATA
Find the Output
Q. 4 : Find the possible output(s) from the given options and also find the min and max
value of Pick.
import random
Pick = [Link](0,3) #0,1,2,3
City = [“DELHI",“MUMBAI",“CHENNAI", “KOLKATA"]
for i in City: If Pick = 0, j X
for j in range(1,Pick): Pick = 1 j X
print(i, end = “ ”)
print() Pick = 2 j = 1
Answers:(i), (ii) Pick = 3 j = 1,2
Min value of Pick = 0
Max value of Pick = 3
(i) DELHI DELHI (ii) DELHI
MUMBAI MUMBAI MUMBAI
CHENNAI CHENNAI CHENNAI
KOLKATA KOLKATA KOLKATA
(ii) DELHI (iv)DELHI DELHI
MUMBAI DELHI MUMBAI
CHENNAI CHENNAI DELHI MUMBAI CHENNAI
KOLKATA KOLKATA