Mathematical functions in python | math module in python

Preview




Python has a variety of predefined functions . We do not need to import any module for them. So we use these functions of Python directly as: <function-name>()

Example: input(), int(), float(), type(), len() etc. are predefined functions.

Consider the following example program that uses some built-in functions of Python.

Working with math Module

Python has a predefined module named math having list of predefined functions to do mathematical calculations

To work with functions of math module, we need to first import it by using statement as:

import math

Following table lists some useful math functions:

FunctionSyntaxDescriptionExample
powmath.pow(base,exp)

This function returns base raised to power exp.

Error occurs if (base=0 and exp<=0) or (base<0 and exp is not integer).

math.pow(2,3) gives 8
ceilmath.cell(num)This  function returns next integer value.

math.ceil(1.3) gives 2.0

math.ceil(-1.3) gives -1.0

floormath.floor(num)This  function returns previous integer value.

math.floor(1.3) gives 1.0

math.ceil(-1.3) gives -2

sqrtmath.sqrt(num)This function returns the square root of  a number. If number is less than 0, error will be shown.

math.sqrt(16.0) gives 4.0.

 

expmath.exp(num)exp() function returns e raised to the power exp.math.exp(2.0) gives the value of e2.
fabsmath.fabs(num)

fabs() function returns the absolute value of num.

Negative value is converted to positive value.

math.fabs(1.0) gives 1.0.

math.fabs(-1.0) gives -1.0.

logmath.log(num,[base])

log() function returns the natural logarithm for num.

Error occurs if num<=0

math.log(1.0) gives the natural logarithm for 1.0.

 

math.log(-1.024, 2) will give logarithm of 1024 to the base 2.

log10math.log10(num)

log10() function returns the base 10 logarithm for num.

Error occurs if num <=0.

math.log10(1.0) give base 10 logarithm for 1.0.
sinmath.sin(arg)sin() function returns the sine of arg. The value of arg must be in radians

math.sin(val)

(val is a number)

cosmath.cos(arg)cos() function returns the cosine of arg. The value of arg must be in radians

math.cos(val)

(val is a number)

tanmath.tan(arg)tan() function returns the tangent of arg. The value of arg must be in radians

math.tan(val)

(val is a number)

degreesmath.degrees(x)degrees() converts angle x from radians to degrees.math.degrees(3.14) would give 179.91.
radiansmath.radians(x)radians() converts angle x from degrees to radians.math.radians(179.91) would give 3.14.

math.pi gives the value of mathematical constant pi=3.141592.

math.e gives the mathematical constant e=2.718281.




Mathematical functions at Python shell
>> import math
>> print(math.pow(2,3))
8.0
>>> print(math.ceil(3.4))
4
>>> print(math.floor(3.4))
3
>>> print(math.sqrt(16))
4.0
>>> print(math.exp(2))
7.38905609893065
>>> print(math.fabs(-2.5))
2.5
>>> print(math.log(15))
2.70805020110221
>>> print(math.log(15,2)) #log base 2
3.9068905956085187
>>> print(math.log10(15)) #log base 10
1.1760912590556813
>>> print(math.sin(30))
-0.9880316240928618
>>> print(math.cos(30))
0.15425144988758405
>>> print(math.tan(30))
-6.405331196646276
>>> print(math.degrees(3.14))
179.9087476710785
>>> print(math.radians(30))
0.5235987755982988>>> print(math.pi)
3.141592653589793
>>> print(math.e)
2.718281828459045

Program to calculate area of a triangle using Heron’s formula
import math
a=float(input(“Enter side1 of triangle:”))
b=float(input(“Enter side2 of triangle:”))
c=float(input(“Enter side3 of triangle:”))
s=(a+b+c)/2
area=math.sqrt(s*(s-a)* (s-b)* (s-c))
print(“area of triangle is:”, area)
Output
Enter side1 of triangle:2
Enter side2 of triangle:3
Enter side3 of triangle:4
area of triangle is: 2.9047375096555625

 

Program to find roots of a quadratic equation
import math
a=float(input(“Enter value of a=”))
b=float(input(“Enter value of b=”))
c=float(input(“Enter value of c=”))
r1=(-b+math.sqrt(b*b-4*a*c))/(2*a)
r2=(-b-math.sqrt(b*b-4*a*c))/(2*a)
print(“root1=”, r1)
print(“root1=”, r2)
Output
Enter value of a=2
Enter value of b=6
Enter value of c=3
root1= -0.6339745962155614
root1= -2.3660254037844384

 




Lesson tags: find area of triangle in python, find roots of quadratic equation in python, math functions in python, math module in python, using mathematical functions in python
Back to: Python Programming Tutorial