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.
Python has two kinds of pre-defined functions.
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 |
# 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))
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
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
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…