10 Python Math Functions confirm that You’re Not a Rookie…
Math is a built-in module that can be used for mathematical tasks like calculations, such as numeric, trigonometric, logarithmic, exponential calculations, etc.,
We can summarize those functions in these categories,
a. The Constants.
b. Number functions.
c. Power and logarithmic functions.
d. Trigonometric functions.
e. Angular conversion functions.
f. Hyperbolic functions and some special functions.
We are going to discuss a few most valuable functions in this article.
1. CONSTANTS:
A constant is a variable that holds values that cannot be changed. These constants are put into our calculations. There are several constants in the built-in modules we will cover some of the most important ones.
As I mentioned, we can use these values in our calculations/formulae, like,
Or we can access them, for example, Euler’s number, by using the “math. e” constant.
math.e = 2.718281…
math. nan means not a number type; we can use it to initialize those variables that do not include numbers. Its data type is float.
NUMBERS AND NUMERIC REPRESENTATION:
2. ceil and floor:
The ceil (x) returns an integer greater than or equal to the float value. Likewise, the floor (x) returns an integer value lesser or equal to the given float value.
3. factorial:
This method returns the product of all natural numbers for the given value. The factorial of a positive integer n is denoted by n!, which gives all the positive integers less than or equal to n. If n = 0, then it returns 1.
4. gcd:
The gcd (x,y) returns the greatest common denominator of the two given integer values.
5. fabs:
The fabs(x) removes the negative sign of a given number and returns the absolute value of x.
POWER AND LOGARITHMIC FUNCTIONS:
6. sqrt:
The sqrt(x) returns the square root of a given number.
7. pow:
The pow(x,y) method returns the value of x raised to the power of y. It always returns a floating-point value.
8. exp:
Returns e raised to the power of x, where e is the Euler’s number (2.718281…).
9. log:
Returns the log of x, where the base is given. This method accepts two arguments log(x,y). If only one argument is passed in this function, it will return a natural logarithm (Euler’s number (2.718281…) will be considered the base).
10. sin, cos, tan:
The sin, cos, and tan will take the degree and return the value in radians. The return value will be in floating-point
Conclusion:
When I started learning Python, math was the first library I got introduced to. Initially, I faced a few hurdles while learning it, but once I learned the most used functions in the math library, it made the learning process easier. Likewise, I hope this article will help you in a way.
I have written this article from my perspective; other functions in the math library may be more helpful.
Yet to explore more functions and libraries, the sea is the end when it comes to learning. Let’s sail on it and explore.
I will come up with another article until then adios.