PYTHON PROGRAMMING
Mathematical Functions in
Python
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.
Important mathematical functions in python are:
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.
EXAMPL
E the working of ceil() and
# Python code to demonstrate
floor()
# 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 ([Link](a))
o/p : The ceil of 2.3 is : 3
# returning the floor of 2.3
print ("The floor of 2.3 is : ",
end=“ ") print ([Link](a))
o/p : The floor of 2.3 is : 2
fabs() :- This function returns the absolute value of the number.
factorial() :- This function returns the factorial of the
number. Argument must be a positive integer.
fmod() :- This function returns the remainder of the
specified arguments.
Syntax: [Link]( x, y )
Parameters:
x any valid number (positive or negative).
y any valid number(positive or negative).
Returns: Returns the remainder in x/y as a floating point
number.
EXAMPL
E the working of
# Python code to demonstrate fabs() and
factorial()
# importing "math" for mathematical operations
import math
a = -10.3
b= 5
# returning the absolute value.
print ("The absolute value of -10.3 is : ", end=“ ")
print ([Link](a))
o/p : The absolute value of -10.3 is : 10.3
# returning the factorial of 5
print ("The factorial of 5 is : ",
end=“ ") print ([Link](b))
o/p : The factorial of 5 is : 120
EXAMPLE
# Python program to demonstrate fmod()
function
import math
# modulus of +ve integer number
print([Link](4, 5))
print([Link](43.50, 4.5))
# modulus of -ve integer
number print([Link](-17,
5))
print('%.2f' %[Link](-10,
4.78))
o/p :
4.0
fsum() :- This function find and return an accurate floating
point sum between some range or an iterable ( list ).
trunc() :- This function returns the integer part of the
argument after truncation.
exp(x) :- This function returns e**x.
log(a, b) :- This function returns the logarithmic value of a with base
b. With one argument , function returns the natural logarithm of a to
base e. With two arguments , function returns the logarithm of a to the
given base b.
pow(a, b) :- This function is used to compute value of a raised to the
power b (a**b).
sqrt() :- This function returns the square root of the number.
EXAMPL
E use of [Link]()
# Python code to demonstrate
function
# fsum() is found in math
library import math o/p :
# range(10)
print([Link](range(10)
45.0
)) 11.0
# Integer 7.99
list arr = [1,
4, 6]
print([Link]
um(arr))
# Floating point
list arr = [2.5, 2.4,
3.09]
print([Link](arr
EXAMPL
# trunc() for a positive E
number. import math
print ([Link](3.5))
o/p :
# trunc() for a negative 3
number. import math -4
print [Link](-4.5)
# Python code to demonstrate the working of exp() and
log()
import math
# returning the exp of 4
print ("The e**4 value is : ",
end=“ ") print ([Link](4)) o/p :The e**4 value is : 54.598150033144236
The value of log 2 with base 3 is :
# returning the log of 2,3 0.6309297535714574
print ("The value of log 2 with base 3 is : ",
end=“ ") print ([Link](2,3))
EXAMPL
# Python code to demonstrateEthe working of pow() and
sqrt()
# 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 ([Link](3,2))
# returning the square root of 25
print ("The value of square root of 25 : ", end=“
") print ([Link](25))
o/p : The value of 3 to the power 2
is : 9.0 The value of square root of
pi :- This is an inbuilt constant that outputs the value of
pi(3.141592).
e :- This is an inbuilt constant that outputs the value of
e(2.718281).
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
EXAMPL
# Python code to demonstrateEthe working
of # const. pi and e
# importing "math" for mathematical
operations import math
# returning the value of c onst. pi
print ("The value of const. pi is : ", end=“
") print ([Link])
# returning the value of c onst. e
print ("The value of const. e is : ", end=“
") print (math.e)
o/p : The value of const. pi is :
3.141592653589793 The value of const. e is
: 2.718281828459045
EXAMPL
# Python code to demonstrateEthe working of sin() , cos() and
tan()
import math
a =
[Link]/6
# returning
the value of
sine of pi/6
print ("The value of sine of pi/6 is : ", end=“
") print ([Link](a))
o/p : The value of sine of pi/6 is :
0.49999999999999994
# returning the value of cosine of pi/6
print ("The value of cosine of pi/6 is : ",
EXAMPL
E
# returning the value of tangent of pi/6
print ("The value of tangent of pi/6 is : ", end=“
") print ([Link](a))
o/p : The value of tangent of pi/6 is :
0.5773502691896257
EXAMPL
E the working of degrees() and
# Python code to demonstrate
radians() import math
a =
[Link]/6 b
= 30
# returning
the
converted
value from
radians to
degrees
print ("The converted value from radians to degrees is : ",
end=“ ") print ([Link](a))
# returning the converted value from degrees to radians
asin() :- This function returns the arc sine of value passed as
argument. The value passed in this function should be in radians.
acos() :- This function returns the arc cosine of value passed as
argument. The value passed in this function should be in radians.
atan() :- This function returns the arc tangent of value passed as
argument. The value passed in this function should be in radians.
sinh() :- This function returns the hyperbolic sine of value passed as
argument.
The value passed in this function should be in radians.
cosh() :- This function returns the hyperbolic cosine of value
passed
as argument. The value passed in this function should be in
radians.
EXAMPL
E asin() , acos() and
# Python code to implementthe
atan() import math
a = [Link] / 6
# returning the value of arc sine of pi / 6
print ("The value of arc sine of pi / 6 is : ", end
=“ ") print ([Link](a))
# returning the value of arc cosine of pi / 6
print ("The value of arc cosine of pi / 6 is : ", end
=“ ") print ([Link](a))
# returning the value of arc tangent of pi / 6
print ("The value of arc tangent of pi / 6 is : ", end
=“ ") print ([Link](a))
o/p : The value of arc sine of pi / 6 is :
0.5510695830994463 The value of arc cosine of pi /
6 is : 1.0197267436954502 The value of tangent of
EXAMPLE
# Python code to implementthe sinh() , cosh() and
tanh()
import
math
a = [Link] /
o/p :
6
0.5478534738880397
# Return the hyperbolic sine
1.1402383210764286
value print ([Link](a))
0.4804727781564516
# Return the hyperbolic cosine
value print ([Link](a))
# Return the hyperbolic tangent
value print ([Link](a))