Categories: python

Math Functions in Python

Python allows us to perform a variety of mathematical operations with ease by importing a module called “math,” which is used to access mathematical functions. These methods are only applicable to integer or real-type objects, not complex numbers.

Types of pre-defined functions:

Python has two kinds of pre-defined functions.

  • Inbuilt functions: These are functions that do not require any additional (external) code files (known as, Modules or Library Files). Because these are part of the Python core and are only built within the Python compiler, there is no need to import these modules/libraries into our code.
  • The second type of function necessitates the use of some external files (modules). The process of incorporating these external files into our code is known as importing. So all we have to do is import the file into our code and use the functions that are already there.

Let us see some of the Math module’s functions:

Function Description
ceil(x)Return the value of the Ceiling. It is the smallest integer greater than or equal to x.
copysign(x, y)It gives back the number x and copies the sign of y to x.
fabs(x)The absolute value of x is returned.
factorial(x)Returns factorial of x. where x ≥ 0
floor(x)Return the Floor value. It is the largest integer, less or equal to the number x.
fsum(iterable)Find the sum of the elements in an iterable object
gcd(x, y)Returns the Greatest Common Divisor of x and y
isfinite(x)Checks whether x is neither infinity nor nan.
isinf(x)Checks whether x is infinity
isnan(x)Checks whether x is not a number.
remainder(x, y)Find the remainder after dividing x by y.
pow(x, y)Return the x to the power y value.
sqrt(x)Finds the square root of x
exp(x)Finds xe, where e = 2.718281
log(x[, base])The Log of x, where base is given, is returned. e is the default base.
log2(x)Returns the Log of x, where the base is 2
log10(x)Returns the Log of x, where the base is 10
Example:

# importing "math" for mathematical operations
import math

a = -10.3
b = 5.5
c = 15
d = 5

# returning the ceil of 2.3
print ("The ceil of 2.3 is : ", end="")
print (math.ceil(a))
 
# returning the floor of 2.3
print ("The floor of 2.3 is : ", end="")
print (math.floor(a))

# returning the absolute value.
print ("The absolute value of -10 is : ", end="")
print (math.fabs(a))
 
# returning the copysigned value.
print ("The copysigned value of -10 and 5.5 is : ", end="")
print (math.copysign(5.5, -10))

# returning the gcd of 15 and 5
print ("The gcd of 5 and 15 is : ", end="")
print (math.gcd(5,15))

# returning the factorial of 5
print ("The factorial of 5 is : ", end="")
print (math.factorial(d))
Output:
The ceil of 2.3 is : -10
The floor of 2.3 is : -11
The absolute value of -10 is : 10.3
The copysigned value of -10 and 5.5 is : -5.5
The gcd of 5 and 15 is : 5
The factorial of 5 is : 120

Note: also read about Python Mathematical Operators

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

2 years ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago