explain 10 math function used in python with example
Answers
Answer:
The math module is used to access mathematical functions in the Python. All methods of this functions are used for integer or real type objects, not for complex numbers.
To use this module, we should import that module into our code.
import math
Some Constants
These constants are used to put them into our calculations.
Sr.No. Constants & Description
1
pi
Return the value of pi: 3.141592
2
E
Return the value of natural base e. e is 0.718282
3
tau
Returns the value of tau. tau = 6.283185
4
inf
Returns the infinite
5
nan
Not a number type.
Numbers and Numeric Representation
These functions are used to represent numbers in different forms. The methods are like below −
Sr.No. Function & Description
1
ceil(x)
Return the Ceiling value. It is the smallest integer, greater or equal to the number x.
2
copysign(x, y)
It returns the number x and copy the sign of y to x.
3
fabs(x)
Returns the absolute value of x.
4
factorial(x)
Returns factorial of x. where x ≥ 0
5
floor(x)
Return the Floor value. It is the largest integer, less or equal to the number x.
6
fsum(iterable)
Find sum of the elements in an iterable object
7
gcd(x, y)
Returns the Greatest Common Divisor of x and y
8
isfinite(x)
Checks whether x is neither an infinity nor nan.
9
isinf(x)
Checks whether x is infinity
10
isnan(x)
Checks whether x is not a number.
11
remainder(x, y)
Find remainder after dividing x by y.
Example Code
import math
print('The Floor and Ceiling value of 23.56 are: ' + str(math.ceil(23.56)) + ', ' + str(math.floor(23.56)))
x = 10
y = -15
print('The value of x after copying the sign from y is: ' + str(math.copysign(x, y)))
print('Absolute value of -96 and 56 are: ' + str(math.fabs(-96)) + ', ' + str(math.fabs(56)))
my_list = [12, 4.25, 89, 3.02, -65.23, -7.2, 6.3]
print('Sum of the elements of the list: ' + str(math.fsum(my_list)))
print('The GCD of 24 and 56 : ' + str(math.gcd(24, 56)))
x = float('nan')
if math.isnan(x):
print('It is not a number')
x = float('inf')
y = 45
if math.isinf(x):
print('It is Infinity')
print(math.isfinite(x)) #x is not a finite number
print(math.isfinite(y)) #y is a finite number
Output
The Floor and Ceiling value of 23.56 are: 24, 23
The value of x after copying the sign from y is: -10.0
Absolute value of -96 and 56 are: 96.0, 56.0
Sum of the elements of the list: 42.13999999999999
The GCD of 24 and 56 : 8
It is not a number
It is Infinity
False
True
Power and Logarithmic Functions
These functions are used to calculate different power related and logarithmic related tasks.
Sr.No. Function & Description
1
pow(x, y)
Return the x to the power y value.
2
sqrt(x)
Finds the square root of x
3
exp(x)
Finds xe, where e = 2.718281
4
log(x[, base])
Returns the Log of x, where base is given. The default base is e
5
log2(x)
Returns the Log of x, where base is 2
6
log10(x)
Returns the Log of x, where base is 10
Example Code
Live Demo
import math
print('The value of 5^8: ' + str(math.pow(5, 8)))
print('Square root of 400: ' + str(math.sqrt(400)))
print('The value of 5^e: ' + str(math.exp(5)))
print('The value of Log(625), base 5: ' + str(math.log(625, 5)))
print('The value of Log(1024), base 2: ' + str(math.log2(1024)))
print('The value of Log(1024), base 10: ' + str(math.log10(1024)))
Output
The value of 5^8: 390625.0
Square root of 400: 20.0
The value of 5^e: 148.4131591025766
The value of Log(625), base 5: 4.0
The value of Log(1024), base 2: 10.0
The value of Log(1024), base 10: 3.010299956639812
Trigonometric & Angular Conversion Functions
These functions are used to calculate different trigonometric operations.
Sr.No. Function & Description
1
sin(x)
Return the sine of x in radians
2
cos(x)
Return the cosine of x in radians
3
tan(x)
Return the tangent of x in radians
4
asin(x)
This is the inverse operation of the sine, there are acos, atan also.
5
degrees(x)
Convert angle x from radian to degrees
6
radians(x)
Convert angle x from degrees to radian
Answer:
The math module is a standard module in Python and is always available. To use mathematical functions under this module, you have to import the module using import math.
It gives access to the underlying C library functions. For example,
# Square root calculation
import math
math.sqrt(4)
This module does not support complex datatypes. The cmath module is the complex counterpart.
Functions in Python Math Module
Here is the list of all the functions and attributes defined in math module with a brief explanation of what they do.
List of Functions in Python Math Module
Function Description
ceil(x) Returns the smallest integer greater than or equal to x.
copysign(x, y) Returns x with the sign of y
fabs(x) Returns the absolute value of x
factorial(x) Returns the factorial of x
floor(x) Returns the largest integer less than or equal to x
fmod(x, y) Returns the remainder when x is divided by y
frexp(x) Returns the mantissa and exponent of x as the pair (m, e)
fsum(iterable) Returns an accurate floating point sum of values in the iterable
isfinite(x) Returns True if x is neither an infinity nor a NaN (Not a Number)
isinf(x) Returns True if x is a positive or negative infinity
isnan(x) Returns True if x is a NaN
ldexp(x, i) Returns x * (2**i)
modf(x) Returns the fractional and integer parts of x
trunc(x) Returns the truncated integer value of x
exp(x) Returns e**x
expm1(x) Returns e**x - 1
log(x[, base]) Returns the logarithm of x to the base (defaults to e)
log1p(x) Returns the natural logarithm of 1+x
log2(x) Returns the base-2 logarithm of x
log10(x) Returns the base-10 logarithm of x
pow(x, y) Returns x raised to the power y
sqrt(x) Returns the square root of x
acos(x) Returns the arc cosine of x
asin(x) Returns the arc sine of x
atan(x) Returns the arc tangent of x
atan2(y, x) Returns atan(y / x)
cos(x) Returns the cosine of x
hypot(x, y) Returns the Euclidean norm, sqrt(x*x + y*y)
sin(x) Returns the sine of x
tan(x) Returns the tangent of x
degrees(x) Converts angle x from radians to degrees
radians(x) Converts angle x from degrees to radians
acosh(x) Returns the inverse hyperbolic cosine of x
asinh(x) Returns the inverse hyperbolic sine of x
atanh(x) Returns the inverse hyperbolic tangent of x
cosh(x) Returns the hyperbolic cosine of x
sinh(x) Returns the hyperbolic cosine of x
tanh(x) Returns the hyperbolic tangent of x
erf(x) Returns the error function at x
erfc(x) Returns the complementary error function at x
gamma(x) Returns the Gamma function at x
lgamma(x) Returns the natural logarithm of the absolute value of the Gamma function at x
pi Mathematical constant, the ratio of circumference of a circle to it's diameter (3.14159...)
e mathematical constant e (2.71828