Mathematical functions in python 3 | math module in python 3

Mathematical functions in python 3 | math module in python 3

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 in Python

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:

Function Syntax Description Example
pow math.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
ceil math.cell(num) This  function returns next integer value. math.ceil(1.3) gives 2.0

math.ceil(-1.3) gives -1.0

floor math.floor(num) This  function returns previous integer value. math.floor(1.3) gives 1.0

math.ceil(-1.3) gives -2

sqrt math.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.


exp math.exp(num) exp() function returns e raised to the power exp. math.exp(2.0) gives the value of e2.
fabs math.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.

log math.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.

log10 math.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.
sin math.sin(arg) sin() function returns the sine of arg. The value of arg must be in radians math.sin(val)

(val is a number)

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

(val is a number)

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

(val is a number)

degrees math.degrees(x) degrees() converts angle x from radians to degrees. math.degrees(3.14) would give 179.91.
radians math.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))
>>> print(math.ceil(3.4))
>>> print(math.floor(3.4))
>>> print(math.sqrt(16))
>>> print(math.exp(2))
>>> print(math.fabs(-2.5))
>>> print(math.log(15))
>>> print(math.log(15,2)) #log base 2
>>> print(math.log10(15)) #log base 10
>>> print(math.sin(30))
>>> print(math.cos(30))
>>> print(math.tan(30))
>>> print(math.degrees(3.14))
>>> print(math.radians(30))
>>> print(math.pi)
>>> print(math.e)

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:"))
area=math.sqrt(s*(s-a)* (s-b)* (s-c))
print("area of triangle is:", area)
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="))
print("root1=", r1)
print("root1=", r2)
Enter value of a=2
Enter value of b=6
Enter value of c=3
root1= -0.6339745962155614
root1= -2.3660254037844384

Spread the love
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
Spread the love