Python Math Functionality Usage

Posted by

Python Math, When it comes to the computation python has it’s own importance as it is used almost by large scale of developers in their respective fields.

Making using of a computer for math functionality is the perfect choice so why not learn how we can make use of it in this part of the tutorial.

Python is a powerful programming language that has a wide range of built-in mathematical functions. These functions make it easy for developers to perform complex mathematical operations without having to write their own functions. Here are some of the most commonly used mathematical functions in Python:

  1. abs(x): returns the absolute value of a number x.
  2. pow(x, y): returns x raised to the power of y.
  3. round(x, n): rounds a number x to n decimal places.
  4. min(x1, x2, …, xn): returns the smallest of the input values.
  5. max(x1, x2, …, xn): returns the largest of the input values.
  6. sqrt(x): returns the square root of a number x.
  7. exp(x): returns the exponential of x.
  8. log(x): returns the natural logarithm of x.
  9. log10(x): returns the base-10 logarithm of x.
  10. sin(x): returns the sine of x (in radians).
  11. cos(x): returns the cosine of x (in radians).
  12. tan(x): returns the tangent of x (in radians).
  13. asin(x): returns the inverse sine of x (in radians).
  14. acos(x): returns the inverse cosine of x (in radians).
  15. atan(x): returns the inverse tangent of x (in radians).

Python Math Functionality Video Tutorial :

In this video tutorial let us try to see the detailed implementation of python Math Functionality.

Import Python Math ??

We need to make use of a package math in our python coding to make use of mathematical functionary’s.

Once the module is imported, the following additional mathematical functions are available:

  1. math.pi: returns the value of pi.
  2. math.e: returns the value of e.
  3. math.ceil(x): returns the smallest integer greater than or equal to x.
  4. math.floor(x): returns the largest integer less than or equal to x.
  5. math.trunc(x): returns the integer part of x.
  6. math.degrees(x): converts x from radians to degrees.
  7. math.radians(x): converts x from degrees to radians.
  8. math.sinh(x): returns the hyperbolic sine of x.
  9. math.cosh(x): returns the hyperbolic cosine of x.
  10. math.tanh(x): returns the hyperbolic tangent of x.
  11. math.asinh(x): returns the inverse hyperbolic sine of x.
  12. math.acosh(x): returns the inverse hyperbolic cosine of x.
  13. math.atanh(x): returns the inverse hyperbolic tangent of x.
  14. math.erf(x): returns the error function of x.
  15. math.gamma(x): returns the gamma function of x.
  16. math.lgamma(x): returns the natural logarithm of the absolute value of the gamma function of x.

Different methods are available in this package by using which we can perform calculations.

import math as m

Floor :

Floor will round up the given number to the least by ignoring the decimal values i.e., when you provide 3.4 the out is observed as 3.

x = m.floor( 3.4 )

output :

3.0

Ceil :

Ceil will round up the number to the highest available i.e., when you provide 3.4 it will be rounded up as 4.

y = m.ceil( 3.4 )

output :

4.0

Square Root (sqrt) :

Square root is again the most common used math function it is easy with small numbers but the problem arises when the large numbers are provided.

It’s very much easy to solve with the help of python math functionality.

z = m.sqrt( 16 )

output :

4.0

In the video tutorial provided we have also show how decimal values are calculated too.

Factorial :

Factorial is the product of all the positive integers greater than zero. Not sure what it is ok…

factorial of 4 can be calculated as

4 * 3 * 2 * 1 = 24

similarly for 5

5 * 4 * 3 * 2 * 1 = 120

a = m.factorial( 4 )

output :

24

Power :

When we want to calculate 3 power 2 i.e., 3 * 3 = 9

b = m.pow( 3, 2 )

output :

9.0

Modulo Operation :

We can perform division for the provided values using modulo operator.

c = m.fmod( 12, 4 )

output :

0

i.e., 12 divided by 4 leaves reminder 0.

Sum :

We can add all the list of provided numbers and provided the result as output.

d = m.fsum([ 1, 2, 3, 4 ])

output :

10

LCM :

Calculating LCM might be remembering the child hood maths classes. It’s now very easy with python let’s see

To calculate LCM of given two numbers 90, 30

e = m.lcm( 90, 30 )

output :

90

Python Math

Clear explanation of Python Math provided in video tutorial. If you have any query’s in this tutorial on Python Math do let us know in the comment section below. If you like this tutorial do like and share us for more interesting updates..

Python Math