Dremendo Tag Line

Mathematical Functions in Python Programming

Mathematical Functions

In this lesson, we will understand what is Mathematical Function, and with the help of examples, we will see how to use mathematical functions in the Python program.

List of Python Mathematical Functions

Python Mathematical functions are predefined functions which accept values and return the result. To use mathematical functions, we have to import the math module in our program.

With the help of mathematical functions we can easily solve a complex equation in our program, for example, if we want to find the square root of a number, then we can use sqrt() mathematical function to find the square root of a given number.

Let's see all the important mathematical functions available in Python in more details with examples.

video-poster

fabs() Function

The fabs() function returns the absolute value of a number. Here Absolute value means number without negative sign. The absolute value of a number is always positive.

Syntax of fabs() function

math.fabs(num)

Example

Python program to input an integer number and print its absolute value.

import math

n=int(input('Enter an integer number '))
x=math.fabs(n)
print('Absolute value of %d is %d' %(n,x))

Output

Enter an integer number -18
Absolute value of -18 is 18

Here you can see that we have input -18 and pass the value to the fabs() function. The fabs() function returned the result in the variable x after removing the negative sign from the number. Thus the final output is 18.

sqrt() Function

The sqrt() function returns the square root of a positive number. Remember that square root of a negative can not be calculated.

Syntax of sqrt() function

math.sqrt(num)

Example

Python program to input an integer and print its square root.

import math

n=int(input('Enter an integer number '))
x=math.sqrt(n)
print('Square root of %d is %d' %(n,x))

Output

Enter an integer number 25
Square root of 25 is 5

Here you can see that we have input 25 and pass the value to the sqrt() function. The sqrt() function returned the result in the variable x after calculating the square root. Thus the final output is 5.

ceil() Function

The ceil() function returns the nearest integer number greater than the number passed as argument.

Syntax of ceil() function

math.ceil(num)

Example

Python program to input a floating point number and print its ceil value.

import math

n=float(input('Enter a floating point (decimal) number '))
x=math.ceil(n)
print('Ceil value of %f is %f' %(n,x))

Output

Enter a floating point (decimal) number 12.4
Ceil value of 12.400000 is 13.000000

Here you can see that we have input 12.4 and pass the value to the ceil() function. The ceil() function returned the result in the variable x after calculating the ceil value of variable n. Thus the final output is 13.000000.

floor() Function

The floor() function returns the nearest integer number less than the number passed as argument.

Syntax of floor() function

math.floor(num)

Example

Python program to input a floating point number and print its floor value.

import math

n=float(input('Enter a floating point (decimal) number '))
x=math.floor(n)
print('Floor value of %f is %f' %(n,x))

Output

Enter a floating point (decimal) number 12.4
Floor value of 12.400000 is 12.000000

Here you can see that we have input 12.4 and pass the value to the floor() function. The floor() function returned the result in the variable x after calculating the floor value of variable n. Thus the final output is 12.000000.

fmod() Function

The fmod() function returns the remainder of the division of two float data type numbers.

Syntax of fmod() function

math.fmod(x, y)

Example

Python program to input two floating point numbers and print the remainder after dividing the numbers.

import math

print('Enter two floating point (decimal) numbers')
x=float(input())
y=float(input())
z=math.fmod(x,y)
print('Remainder = %f' %(z))

Output

Enter two floating point (decimal) numbers
5.26
3.17
Remainder = 2.090000

Here you can see that we have input two floating point numbers 5.26 and 3.17 and pass the values to the fmod() function. The fmod() function returned the result in the variable z after calculating the remainder of the division. Thus the final output is 2.090000.

pow() Function

The pow() function is used to computes the power of a number. In mathematics the power is written as xy.

Syntax of pow() function

math.pow(x, y)

Example

Python program to input two integer numbers and print the power.

import math

print('Enter two integer numbers')
a=int(input())
b=int(input())
c=math.pow(a,b)
print('Power = %d' %(c))

Output

Enter two integer numbers
5
2
Power = 25

Here you can see that we have input two integer numbers 5 and 2 and pass the values to the pow() function. The pow() function returned the result in the variable c after calculating the power. Thus the final output is 25.

All the above functions are most commonly used mathematical functions. Below is the list of other mathematical functions that you can use in your program as per requirement.

List of other useful mathematical functions

Function Syntax Description
cos math.cos(x) The cos() function returns the cosine of x, where x is expressed in radians. The return value of cos() is in the range [-1, 1].
acos math.acos(x) The acos() function returns the arc cosine of x, which will be in the range [0, pi]. x should be between -1 and 1. Value of pi=3.14159265.
cosh math.cosh(x) The function cosh() returns the hyperbolic cosine of x.
sin math.sin(x) The function sin() returns the sine of x, where x is given in radians. The return value of sin() will be in the range [-1, 1].
asin math.asin(x) The asin() function returns the arc sine of x, which will be in the range [-pi/2, +pi/2]. x should be between -1 and 1. Value of pi=3.14159265.
sinh math.sinh(x) The function sinh() returns the hyperbolic sine of x.
asinh math.asinh(x) The function asinh() returns the inverse hyperbolic sine of x.
tan math.tan(x) The tan() function returns the tangent of x, where x is given in radians.
atan math.atan(x) The function atan() returns the arc tangent of x, which will be in the range [-pi/2, +pi/2]. Value of pi=3.14159265.
atan2 math.atan2(y, x) The atan2() function computes the arc tangent of y/x, using the signs of the arguments to compute the quadrant of the return value.
tanh math.tanh(x) The function tanh() returns the hyperbolic tangent of x.
atanh math.atanh(x) The function atanh() returns the inverse hyperbolic tangent of x.
exp math.exp(x) The exp() function returns e (2.7182818) raised to the xth power.
log math.log(x) The function log() returns the natural (base e) logarithm of x.
log10 math.log10(x) The log10() function returns the base 10 (or common) logarithm for x.

Visit this page to learn about all the mathematical functions defined in Python 3.

Test Your Knowledge

Attempt the practical questions to check if the lesson is adequately clear to you.

Test Your Knowledge