Mathematical Functions in Python (Numeric
Functions):
In python a number of mathematical operations can be performed with ease by importing a
module named “math” which defines various functions which makes our tasks easier.
1. ceil() :- This function returns the smallest integral value greater than the number. If
number is already integer, same number is returned.
# importing "math" for mathematical operations
import math
a = 2.3
# returning the ceil of 2.3
print ("The ceil of 2.3 is : ", end="")
print (math.ceil(a))
Output:
The ceil of 2.3 is : 3
2. floor() :- This function returns the greatest integral value smaller than the number. If number is
already integer, same number is returned.
# importing "math" for mathematical operations
import math
a = 2.3
# returning the floor of 2.3
print ("The floor of 2.3 is : ", end="")
print (math.floor(a))
Output:
The floor of 2.3 is : 2
3. fabs() :- This function returns the absolute value of the number.
# importing "math" for mathematical operations
import math
a = -10
# returning the absolute value.
print ("The absolute value of -10 is : ", end="")
print (math.fabs(a))
Output:
The absolute value of -10 is : 10.0
4. factorial() :- This function returns the factorial of the number. An
error message is displayed if number is not integral.
# importing "math" for mathematical operations
import math
b= 5
# returning the factorial of 5
print ("The factorial of 5 is : ", end="")
print (math.factorial(b))
Output:
The factorial of 5 is : 120
5. copysign(a, b) :- This function returns the number with the value of
‘a’ but with the sign of ‘b’. The returned value is float type.
# importing "math" for mathematical operations
import math
a = -10
b = 5.5
# returning the copysigned value.
print ("The copysigned value of -10 and 5.5 is : ", end="")
print (math.copysign(5.5, -10))
Output:
The copysigned value of -10 and 5.5 is : -5.5
6. gcd() :- This function is used to compute the greatest common divisor of 2
numbers mentioned in its arguments. This function works in python 3.5 and
above.
# importing "math" for mathematical operations
import math
c = 15
d =5
# returning the gcd of 15 and 5
print ("The gcd of 5 and 15 is : ", end="")
print (math.gcd(5,15))
The gcd of 5 and 15 is : 5
Mathematical Functions in Python (Logarithmic
and Power Functions)
Logarithmic and power functions are discussed in this set.
1. exp(a) :- This function returns the value of e raised to the power a (e**a) .
# importing "math" for mathematical operations
import math
# returning the exp of 4
print ("The e**4 value is : ", end="")
print (math.exp(4))
Output:
The e**4 value is : 54.598150033144236
2. log(a, b) :- This function returns the logarithmic value of a with base
b. If base is not mentioned, the computed value is of natural log.
# importing "math" for mathematical operations
import math
# returning the log of 2,3
print ("The value of log 2 with base 3 is : ", end="")
print (math.log(2,3))
Output:
The value of log 2 with base 3 is : 0.6309297535714574
3. log2(a) :- This function computes value of log a with base 2. This value
is more accurate than the value of the function discussed above.
# importing "math" for mathematical operations
import math
# returning the log2 of 16
print ("The value of log2 of 16 is : ", end="")
print (math.log2(16))
Output:
The value of log2 of 16 is : 4.0
4. log10(a) :- This function computes value of log a with base 10. This
value is more accurate than the value of the function discussed above.
# importing "math" for mathematical operations
import math
# returning the log10 of 10000
print ("The value of log10 of 10000 is : ", end="")
print (math.log10(10000))
Output:
The value of log10 of 10000 is : 4.0
5. pow(a, b) :- This function is used to compute value of a raised to the
power b (a**b).
# importing "math" for mathematical operations
import math
# returning the value of 3**2
print ("The value of 3 to the power 2 is : ", end="")
print (math.pow(3,2))
Output:
The value of 3 to the power 2 is : 9.0
6. sqrt() :- This function returns the square root of the number.
# importing "math" for mathematical operations
import math
# returning the square root of 25
print ("The value of square root of 25 : ", end="")
print (math.sqrt(25))
Output:
The value of square root of 25 : 5.0
Mathematical Functions in Python (Trigonometric and
Angular Functions)
1. sin() :- This function returns the sine of value passed as argument. The value passed
in this function should be in radians.
# importing "math" for mathematical operations
import math
a = math.pi/6
# returning the value of sine of pi/6
print ("The value of sine of pi/6 is : ", end="")
print (math.sin(a))
Output:
The value of sine of pi/6 is : 0.49999999999999994
2. cos() :- This function returns the cosine of value passed as argument. The value passed in
this function should be in radians.
# importing "math" for mathematical operations
import math
a = math.pi/6
# returning the value of cosine of pi/6
print ("The value of cosine of pi/6 is : ", end="")
print (math.cos(a))
Output:
The value of cosine of pi/6 is : 0.8660254037844387
3. tan() :- This function returns the tangent of value passed as argument.
The value passed in this function should be in radians.
import math
a = math.pi/6
# returning the value of tangent of pi/6
print ("The value of tangent of pi/6 is : ", end="")
print (math.tan(a))
Output:
The value of tangent of pi/6 is : 0.5773502691896257
4. hypot(a, b) :- This returns the hypotenuse of the values passed in
arguments. Numerically, it returns the value of sqrt(a*a + b*b).
# importing "math" for mathematical operations
import math
b =3
c =4
# returning the value of hypotenuse of 3 and 4
print ("The value of hypotenuse of 3 and 4 is : ", end="")
print (math.hypot(b,c))
Output:
The value of hypotenuse of 3 and 4 is : 5.0
5. degrees() :- This function is used to convert argument value from radians
to degrees.
# importing "math" for mathematical operations
import math
a = math.pi/6
# returning the converted value from radians to degrees
print ("The converted value from radians to degrees is : ", end="")
print (math.degrees(a))
Output:
The converted value from radians to degrees is : 29.999999999999996
6. radians() :- This function is used to convert argument value from degrees
to radians.
# importing "math" for mathematical operations
import math
b = 30
# returning the converted value from degrees to radians
print ("The converted value from degrees to radians is : ", end="")
print (math.radians(b))
Output:
The converted value from degrees to radians is : 0.5235987755982988
Mathematical Functions in Python (Special
Functions and Constants)
1. gamma() :- This function is used to return the gamma function of the
argument.
# importing "math" for mathematical operations
import math
a =4
# returning the gamma() of 4
print ("The gamma() of 4 is : ", end="")
print (math.gamma(a))
Output:
The gamma() of 4 is : 6.0
2. pi :- This is an inbuilt constant that outputs the value of
pi(3.141592).
# importing "math" for mathematical operations
import math
# returning the value of const. pi
print ("The value of const. pi is : ", end="")
print (math.pi)
Output:
The value of const. pi is : 3.141592653589793
3. e :- This is an inbuilt constant that outputs the value of
e(2.718281)
# returning the value of const. e
print ("The value of const. e is : ", end="")
print (math.e)
Output:
The value of const. e is : 2.718281828459045
Random Numbers in Python
Python defines a set of functions that are used to generate or manipulate random numbers. This
particular type of functions are used in a lot of games, lotteries or any application requiring
random number generation.
1. choice() :- This function is used to generate 1 random number from a container.
# importing "random" for random operations
import random
# using choice() to generate a random number from a
# given list of numbers.
print ("A random number from list is : ",end="")
print (random.choice([1, 4, 8, 10, 3]))
Output:
A random number from list is : 4
2. randrange(beg, end, step) :- This function is also used to generate random number but
within a range specified in its arguments. This function takes 3 arguments, beginning
number (included in generation), last number (excluded in generation) and step ( to skip
numbers in range while selecting).
# importing "random" for random operations
import random
# using randrange() to generate in range from 20
# to 50. The last parameter 3 is step size to skip
# three numbers when selecting.
print ("A random number from range is : ",end="")
print (random.randrange(20, 50, 3))
Output:
A random number from range is : 41
3. random() :- This number is used to generate a float random number less than 1 and greater
or equal to 0.
# importing "random" for random operations
import random
# using random() to generate a random number
# between 0 and 1
print ("A random number between 0 and 1 is : ", end="")
print (random.random())
Output:
A random number between 0 and 1 is : 0.510721762520941
4. seed() :- This function maps a particular random number with the seed
argument mentioned. All random numbers called after the seeded value returns
the mapped number.
import random
# using seed() to seed a random number
random.seed(5)
# printing mapped random number
print ("The mapped random number with 5 is : ", end="")
print (random.random())
# using seed() to seed different random number
random.seed(7)
# printing mapped random number
print ("The mapped random number with 7 is : ", end="")
print (random.random())
# using seed() to seed to 5 again
random.seed(5)
# printing mapped random number
print ("The mapped random number with 5 is : ",end="")
print (random.random())
# using seed() to seed to 7 again
random.seed(7)
# printing mapped random number
print ("The mapped random number with 7 is : ",end="")
print (random.random())
Output:
The mapped random number with 5 is : 0.6229016948897019
The mapped random number with 7 is : 0.32383276483316237
The mapped random number with 5 is : 0.6229016948897019
The mapped random number with 7 is : 0.32383276483316237
5. shuffle() :- This function is used to shuffle the entire list to randomly arrange them.
# importing "random" for random operations
import random
# Initializing list
li = [1, 4, 5, 10, 2]
# Printing list before shuffling
print ("The list before shuffling is : ", end="")
for i in range(0, len(li)):
print (li[i], end=" ")
print("\r")
# using shuffle() to shuffle the list
random.shuffle(li)
# Printing list after shuffling
print ("The list after shuffling is : ", end="")
for i in range(0, len(li)):
print (li[i], end=" ")
print("\r")
Output:
The list before shuffling is : 1 4 5 10 2
The list after shuffling is : 2 1 4 5 10
6. uniform(a, b) :- This function is used to generate a floating point random number between
the numbers mentioned in its arguments. It takes two arguments, lower limit(included in
generation) and upper limit(not included in generation).
# importing "random" for random operations
import random
# using uniform() to generate random floating number in range
# prints number between 5 and 10
print ("The random floating point number between 5 and 10 is : ",end="")
print (random.uniform(5,10))
The random floating point number between 5 and 10 is : 5.183697823553464