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

 

 

 
Define a complex number x+yj  
Define a complex number complex(x,y)  
real part of a complex number c.real  
imaginary part of a complex no. c.imag  
conjugate of a complex number c.conjugate()  
arg of a complex number cmath.phase(c) "import cmath" should be done to use this
polar form of a complex number cmath.polar(c) "import cmath" should be done to use this
convert polar form to rect form cmath.rect(r,phi) "import cmath" should be done to use this
     
     

 

 

Examples 1 > Basic Operator

    >>> 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

     

    >>> 7 % 3

    1

     

 

 

Examples 2>  math package

    >>> import math

     

    >>> math.exp(1)

    2.718281828459045

     

    >>> math.sqrt(2)

    1.4142135623730951

     

     

Examples 3>  complex number 

     

    >>> a = 1+2j

     

    >>> b = complex(2,3)

     

    >>> a

    (1+2j)

     

    >>> b

    (2+3j)

     

    >>> a+b

    (3+5j)

     

    >>> a*b

    (-4+7j)

     

    >>> a/b

    (0.6153846153846154+0.07692307692307691j)

     

    >>> a.real

    1.0

     

    >>> a.imag

    2.0

     

    >>> a.conjugate()

    (1-2j)

     

    >>> import cmath

     

    >>> cmath.phase(a)

    1.1071487177940904

     

    >>> cmath.polar(a)

    (2.23606797749979, 1.1071487177940904)

     

    >>> cmath.rect(2.23606797749979,1.1071487177940904)

    (1.0000000000000002+2j)

     

 

 

Example 4 >  Complex Number (Same as Example 3, but run in  *.py script)

 

    import cmath

     

    a = 1+2j

    b = complex(2,3)

     

    print('a = ',a)

    print('b = ',b)

    print('a+b = ',a+b)

    print('a*b = ',a*b)

    print('a/b = ',a/b)

    print('a.real = ',a.real)

    print('a.imag = ',a.imag)

    print('a.conjugate() = ',a.conjugate())

    print('cmath.phase(a) = ',cmath.phase(a))

    print('cmath.polar(a) = ',cmath.polar(a))

    print('cmath.rect(2.23606797749979,1.1071487177940904) = ',

                                     cmath.rect(2.23606797749979,1.1071487177940904))

     

     

    Result :---------------------------------

     

    a =  (1+2j)

    b =  (2+3j)

    a+b =  (3+5j)

    a*b =  (-4+7j)

    a/b =  (0.6153846153846154+0.07692307692307691j)

    a.real =  1.0

    a.imag =  2.0

    a.conjugate() =  (1-2j)

    cmath.phase(a) =  1.1071487177940904

    cmath.polar(a) =  (2.23606797749979, 1.1071487177940904)

    cmath.rect(2.23606797749979,1.1071487177940904) =  (1.0000000000000002+2j)