Python Math Module - Cheat Sheet

Python Math Module | Mathematical functions are a very important part of most algorithms and hence, programs. Be it a simple calculator program or a graph-creation program, the math functions will make their presence felt everywhere. Now, we can tackle simple things like addition and multiplication directly, but what if you want to find the square root of a number, or find the value of sine π/2. This is where the math module comes in. This inbuilt module comes with a big bunch of math functions that will solve most of your mathematical requirements. Let's have a look at few of the functions under math.

Now, the first thing you do is import math. 

cheat sheet for functions in math module in python







Assuming you have done that, let's move on the functions.


Trigonometric Functions:

  • math.degrees(x) : Takes a value of x (in radians) and converts it to degrees
  • math.radians(x) : Takes a value of x (in degrees) and converts it to radians.
  1. math.sin() : The math.sin(x) function takes a value of x (in radians) and returns its sine value.
  2. math.cos(): The math.cos(x) function takes a value of x (in radians) and returns its cosine value.
  3. math.tan(): The math.tan(x) function takes a value of x (in radians) and returns its tan value.
  4. math.asin(): The math.asin(x) function returns the arcsin value of x (in radians)
  5. math.acos(): The math.acos(x) function returns the arccos value of x (in radians)
  6. math.atan(): The math.atan(x) function returns the arctan value of x (in radians)

Logarithmic Functions:

  1. math.log() : The math.log(x,b) function takes two values x and b and returns the value of log x to the base b. The default value of b is e (Euler's number, approximately 2.718)
  2. math.log2() :  The math.log2(x) function takes a value x and returns the value of log x to the base 2.
  3. math.log10() :  The math.log10(x) function takes a value x and returns the value of log x to the base 10.
Other Important Functions:
  1. math.fabs(): The math.fabs(x) function takes a value of x and returns the value without any sign.
  2. math.factorial(): The math.factorial(x) function takes a value of x and returns the value of x factorial (x!)
  3. math.pow(): The math.pow(x,y) function takes values of x and y and returns the value of x to the power y.
  4. math.sqrt(): The math.sqrt(x) function takes a value of x and returns the value of its square root.
Constants:
  • math.pi : Returns the value of 𝛑 (approximately 3.14)
  • math.e :  Returns the value of е (Euler's number, explained above)
Now, these are just a few of the many math functions that the math module offers. The math module has enough functions to make math cool. Do explore the module to get yourself familiarized with the module and its functions. In no time, you will find yourself coding mathematical formulae and algorithms a lot faster than before. Feel free to use the comment section to share your questions and suggestions and I will try to solve them to the best of my extent.

Make sure to follow the blog to be updated as soon as a post is released. Just click the blue 'follow' button in the menu to follow.

If you want to explore other modules, check out the random module: THE RANDOM MODULE : 5 MINI PROJECTS (thepygrammer.blogspot.com) or matplotlib, the graphing module MATPLOTLIB - GRAPHS WITH PYTHON (thepygrammer.blogspot.com)




Comments