Math Statistic Random
Math Statistic Random
Python code in one module gains access to the code in another module by
the process of importing it. The import statement is the most common
method of loading a library in the memory.
import math
or
import math as m
Here m is an alias given to the math library. It is generally used when the
library name is big.
The math library in Python contains two special constants – pie ()
and Euler’s number.
Output –
Pie = 3.141592653589793
Euler's number (e) – The Euler’s number which is denoted by e is the
base of natural logarithm
# Euler's number
print("Euler\'s number is ", math.e)
Output –
Euler’s number is 2.718281828459045
Mathematical functions –
Output –
Square root of 144 is = 12.0
Output –
2 raised to the power of 6 = 64.0
Output –
Absolute value of -7 = 7
Absolute value of -7 = 7.89
ceil() :- This function returns the smallest integral value greater than the
number. If number is already integer, same number is returned.
floor() :- This function returns the greatest integral value smaller than
the number. If number is already integer, same number is returned.
a = -11.18
b = 25.15
c = 25.83
print(math.ceil(a)) -11
print(math.ceil(b)) 26
print(math.ceil(c)) 26
print(math.floor(a)) -12
print(math.floor(b)) 25
print(math.floor(c)) 25
Trigonometric functions –
sin() :- This function returns the sine of value passed as argument. The
value passed in this function should be in radians.
cos() :- This function returns the cosine of value passed as argument. The
value passed in this function should be in radians.
Output –
mean() :- This function returns the mean or average of the data passed
in its arguments.
Output –
Mean = 9
Number with maximum occurrence = 12
Middle element = 11
Python provides an inbuilt function round() which rounds off to the given
number of digits and returns the floating point number, if no number of
digits is provided for round off , it rounds off the number to the nearest
integer. It can be used with importing any library in Python.
a = -11.18
b = 25.16
c = 25.83
print(round(a)) -11
print(round(b)) 25
print(round(c)) 26
print(round(a,1)) -11.2
print(round(b,1)) 25.2
print(round(c,1)) 25.8
Using functions in a program –
Output –
import random
print(random.randrange(1,6)) 1
print(random.randrange(6)) 4
’’’ Generating a random integer number in the range 1 to 5. Skipping by 2
while selecting a number. Number generated can be 1,3 or 5’’’
print(random.randrange(1,6,2)) 5
import random
print(random.randint(1,6)) 6
import random
print(random.random()) 0.5046912900553185
Example – 1
Explanation –
When C = 0 then randrange(0+1) = randrange(1) = 0
randrange(0) + 10 = 10 therefore output will be 10
Explanation –
When C = 0 then randint(1, 0+1) = randint(1, 1) = 1
1 + 10 = 11 therefore output will be 11
Practice question – 1
BEGIN=random.randrange(1,3)
LAST=random.randint(2,4)
print(BEGIN,LAST)
Practice question – 2
import random as r val = 35
val, P, Num = 35, 7, 0 P=7
for i in range(1, 5): Num = 0
Num = val + r.randint(0, P - 1)
print(Num, " $ ", end = "")
P=P-1
Choose the correct option(s) –
(a) 41 $ 38 $ 38 $ 37 $ (b) 38 $ 40 $ 37 $ 34 $
(c) 36 $ 35 $ 42 $ 37 $ (d) 40 $ 37 $ 39 $ 35 $
Practice question – 3
import random as r
x= r.random()
y= r.randint(1, 3)
for z in range(1, y) :
print(z + round(x), end = “:”)
Practice question – 4
import random as r
y = r.randrange(1,4)
for I in range (y) :
print(y, end = ’-’)
Practice question – 5
import random
string="WelcomE"
for ch in string :
if ch not in ('a','e','i','o','u') :
print(ch,random.randrange(5,10),end='&')