Python                                                          Home : www.sharetechnote.com

 

 

 

 

Python - Mathematical Operators/Functions

 

These are the math functions that are commonly used (you may use this as a quick cheatsheet). There are more math functions. Some of the functions from math package does almost same thing as native operator/functions but with more accuracy. You may refer to official Python document : 9.2. math Mathematical functions for further details.

 

Operation

Python

Comments

Addition

+

 

Substraction

-

 

Multiplication

*

 

Division

/

 

Power

**

 
mod

%

 
Power (= x ** y) pow(x,y)  
Absolute Value abs(x)  

exponential

math.exp(x)

"import math" should be done to use this

Square Root

math.sqrt(x)

"import math" should be done to use this

pi

math.pi

"import math" should be done to use this

e

math.e

"import math" should be done to use this

Infinite math.inf

"import math" should be done to use this

NaN math.nan

"import math" should be done to use this

Log

math.log(x)

"import math" should be done to use this

Natural Log

math.log(x,math.e)

"import math" should be done to use this

Log with base

math.log(x,base)

"import math" should be done to use this

Absolute Value

math.fabs(x)

"import math" should be done to use this

factorial

math.factorial(x)

"import math" should be done to use this

mod

math.fmod(x, y)

"import math" should be done to use this

sin

math.sin(x)

"import math" should be done to use this

cos

math.cos(x)

"import math" should be done to use this

tan

math.tan(x)

"import math" should be done to use this

asin

math.asin(x)

"import math" should be done to use this

acos

math.acos(x)

"import math" should be done to use this

atan

math.atan(x)

"import math" should be done to use this

sinh

math.sinh(x)

"import math" should be done to use this

cosh

math.cosh(x)

"import math" should be done to use this

tanh

math.tanh(x)

"import math" should be done to use this

asinh

math.asinh(x)

"import math" should be done to use this

acosh

math.acosh(x)

"import math" should be done to use this

atanh

math.atanh(x)

"import math" should be done to use this

radian to degree

math.degrees(x)

"import math" should be done to use this

degree to radian

math.radians(x)

"import math" should be done to use this

ceil

math.ceil(x)

"import math" should be done to use this

floor

math.floor(x)

"import math" should be done to use this

error function

math.erf(x)

"import math" should be done to use this

complimentary effor function

math.erfc(x)

"import math" should be done to use this

gamma function

math.gamma(x)

"import math" should be done to use this

natural logarithm of the absolute value of the Gamma function

math.lgamma(x)

"import math" should be done to use this

 

 

 

 

 

Examples >

    >>> 2 + 5

    7

     

    >>> 3 - 5

    -2

     

    >>> 2 * 3

    6

     

    >>> 2 / 3

    0.6666666666666666

     

    >>> pow(2,3)

    8

     

    >>> pow(2.0,3.0)

    8.0

     

    >>> 2 ** 3

    8

     

    >>> import math

     

    >>> math.exp(1)

    2.718281828459045

     

    >>> math.sqrt(2)

    1.4142135623730951