Libraries in Python
A Python library contains built in data types, function, exceptions etc. A
library is a built-in Python module; therefore, installation is not required to
use it.
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.
The math library
The math library provides us access to some common mathematical
functions and constants in Python, which can be use throughout the code
for more complex mathematical computations. To access the math library,
it has to be imported first by using the statement –
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.
Pie () – It denotes the ratio of circumference to diameter of a circle and
it has a value of 3.141592653589793. We can access this constant using
pi:
print("Pie = ", math.pi)
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 –
sqrt() :- This function returns the square root of the number.
print("Square root of 144 is ", math.sqrt(144))
Output –
Square root of 144 is = 12.0
pow(x, y) :- This function is used to compute value of x raised to the
power of y (x**y).
print("2 raised to the power of 6 = ",math.pow(2,6))
Output –
2 raised to the power of 6 = 64.0
fabs() :- This function returns the absolute value of the number.
print("Absolute value of -7 = ",math.fabs(-7))
print("Absolute value of -7.89 = ",math.fabs(-7.89))
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.
tan() :- This function returns the tangent of value passed as argument.
The value passed in this function should be in radians.
degrees() :- This function is used to convert argument value from
radians to degrees.
radians() :- This function is used to convert argument value from
degrees to radians.
# Converting degree to radian
angle = math.radians(45)
print("45 degrees = ", angle,"radians")
# Converting radian to degree
print(angle,"radians = ", math.degrees(angle),"degrees")
print("sin of ", angle,"radians = ", math.sin(angle))
print("cos of ", angle,"radians = ", math.cos(angle))
print("tan of ", angle,"radians = ", math.tan(angle))
Output –
45 degrees = 0.7853981633974483 radians
0.7853981633974483 radians = 45.0 degrees
sin of 0.7853981633974483 radians = 0.7071067811865475
cos of 0.7853981633974483 radians = 0.7071067811865476
tan of 0.7853981633974483 radians = 0.9999999999999999
The statistics library
Python has the ability to manipulate some statistical data and calculate
results of various statistical operations using the library file “statistics“,
useful in domain of mathematics.
mean() :- This function returns the mean or average of the data passed
in its arguments.
mode() :- This function returns the number with maximum number of
occurrences.
median() :- This function is used to calculate the median, i.e middle
element of data.
import statistics as stat
# Storing a list of values in an object
data = [11,2,13,12,3,12,5,12,11]
# Mean
print("Mean =",stat.mean(data))
# Mode
print("Number with maximum occurrence =",stat.mode(data))
# Median
print("Middle element =",stat.median(data))
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 –
# finding the area of a circle
r = float(input("Enter the radius of a circle "))
area = math.pi * math.pow(r, 2)
print("Area of the circle is", area)
print("Area of the circle rounded to two digits after decimal is",
round(area,2))
Output –
Enter the radius of a circle 4.57
Area of the circle is 65.61184841095748
Area of the circle rounded of to two digits after decimal is 65.61
Random Numbers in Python
Python defines a set of functions that are used to generate or manipulate
random numbers. This particular type of functions is used in a lot of games,
lotteries or any application requiring random number generation like
generating OTPs, passwords etc.
randrange(beg, end, step) :- This function is used to generate a random
integer number within the specified range. It takes 3 arguments, beginning
number (included in generation), last number (excluded in generation) and
step (to skip numbers in range while selecting). If beginning number is not
specified then by default it takes zero (0) as beginning number.
import random
# Generating a random integer number in the range 1 to 5
print(random.randrange(1,6)) 1
# Generating a random integer number in the range 0 to 5
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
randint(beg, end) :- This inbuilt function of the random module in
Python3 is used to generate a random integer number within the specified
range. It takes 2 arguments, beginning number and last number (both are
included in generation).
import random
print(random.randint(1,6)) 6
random() :- This number is used to generate a float random number
less than 1 and greater or equal to 0.
import random
print(random.random()) 0.5046912900553185
Example – 1
import random Output –
Using randrange
print("Using randrange")
10
for C in for C in range (4) : 11
12
print(random.randrange(C+1)+10)
11
Explanation –
When C = 0 then randrange(0+1) = randrange(1) = 0
randrange(0) + 10 = 10 therefore output will be 10
When C = 1 then randrange(1+1) = randrange(2) = 0 or 1
0 + 10 = 10 or 1 + 10 = 11 therefore output will be 10 or 11
When C=2 then randrange(2+1) = randrange(3) = 0, 1 or 2
0 + 10 = 10 or 1 + 10 = 11 or 2 + 10 = 12
therefore output will be 10 or 11 or 12
When C=3 then randrange(3+1) = randrange(4)=0, 1, 2 or 3
0 + 10 = 10 or 1 + 10 = 11 or 2 + 10 = 12 or 3 + 10 = 13
therefore output will be 10 or 11 or 12 or 13
Example – 2
import random Output –
Using randint
print("Using randint")
11
for C in for C in range (4) : 11
print(random.randint(1,C+1)+10) 12
13
Explanation –
When C = 0 then randint(1, 0+1) = randint(1, 1) = 1
1 + 10 = 11 therefore output will be 11
When C = 1 then randint(1, 1+1) = randint(1, 2) = 1 or 2
1 + 10 = 11 or 2 + 10 = 12 therefore output will be 11 or 12
When C=2 then randint(1, 2+1) = randint(1, 3) = 1, 2 or 3
1 + 10 = 11 or 2 + 10 = 12 or 3 + 10 = 13
therefore output will be 11 or 12 or 13
When C=3 then randint(1, 3+1) = randint(1, 4)=1, 2, 3 or 4
1 + 10 = 11 or 2 + 12 = 12 or 3 + 10 = 13 or 4 + 10 = 14
therefore output will be 11 or 12 or 13 or 14
Practice question – 1
BEGIN=random.randrange(1,3)
LAST=random.randint(2,4)
print(BEGIN,LAST)
Which of the following is not the correct output –
(a) 1 4 (b) 2 2 (c) 3 4 (d) 2 3
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 = “:”)
Choose the correct option(s) –
(a) 1:2: (b) 2:3: (c) 3:4: (d) 1:1:
Practice question – 4
import random as r
y = r.randrange(1,4)
for I in range (y) :
print(y, end = ’-’)
Choose the correct option(s) –
(a) 2-3- (b) 1-1- (c) 3-3- (d) 2-2-
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='&')
Choose the correct option(s) –
(a) W 8&l 9&c 6&m 7& (b) W 6&l 7&c 8&m 6&E 8&
(c) W 6&l 8&c 8&m 9&E 8& (d) W 7&l 8&c 8&m 8&
Answers –
Practice question – 1 (c) Practice question – 2 (a) and (d)
Practice question – 3 (a) and (b) Practice question – 4 (d)
Practice question – 5 (b) and (c)