Mathematical Functions in Python

In this tutorial, we will learn all mathematical functions present in Python and how you can use them in the program. In python, a lot of mathematical functions are present and we learn about them one by one. The math module is used to access mathematical functions in Python. All methods of these functions are used for integer or real type objects, not for complex numbers.

Mathematical Functions

First of all, we import the math module to use its built-in function. The math module can be imported simply as:

import math

factorial function

  • factorial(n): This function takes an integer as an argument and returns the factorial of that number. It displays an error if the argument is not an integer. It can be used as:
import math
fact=math.factorial(5)
print(fact)

output:

120

log function in Python

  • log(a,b): This function takes two numbers as an argument and returns the logarithmic value of a with base b if b is not given then the value of base is taken as natural log. it can be used as :
import math
print(math.log(2,3))

output:

0.6309297535714574
  • log2(a): This function takes one number as an argument and returns the logarithmic value of a with base 2 as:
import math
print(math.log2(16))

output:

4

pow function in Python

  • pow(a,b): This function takes two number as an argument as return a raise to the power b as:
import math
print(math.pow(3,2))

output:

9
  • pow(a,b,c): This function takes three number as argument and returns the a raise to the power b modulus c as:
import math
print(math.pow(7,2,5))

output:

4

ceil and floor function in Python

  • ceil(n): This function takes a number as an argument and returns the smallest integer number greater than the number. If the number already as an integer, it return the same number as:
import math
print(math.ceil(7.54))
print(math.ceil(5))

output:

8
5
  • floor(n): This function takes a number as argument and returns the greatest integer value smaller than the number  if the number is already an integer than it returns same as:
import math
print(math.floor(5.34))
print(math.floor(7))

output:

5
7

gcd function in Python

  • gcd(a,b): This function takes two number as argument and returns the greatest common divisor of a and b as:
import math
print(math.gcd(5,15))
output:
5

copysign function

  • copysign(a,b): This function takes two number as argument and returns the number with a magnitude of a and sign of b as:
import math
print(math.copysign(3,-5))

output:

-3

exp function

  • exp(a): This function takes a number as argument and returns the value of e raised to the power a as:
import math
print(math.exp(4))

output:

54.598150033144236

sqrt function

  • sqrt(n): This function takes a number as argument and returns the value of square root of n as:
import math
print(math.sqrt(25))

output:

5

you may also read:
Exploring random Module in Python

Leave a Reply

Your email address will not be published. Required fields are marked *