[proxy] web.archive.org← back | site home | direct (HTTPS) ↗ | proxy home | ◑ dark◐ light

Python Programming/Math - Wikibooks, open books for an open world

For basic math including addition, subtraction, multiplication and the like, see Basic Math and Operators chapters. For quick reference, the built-in Python math operators include addition (+), subtraction (-), multiplication (*), division (/), floor division (//), modulo (%), and exponentiation (**). The built-in Python math functions include rounding (round()), absolute value (abs()), minimum (min()), maximum (max()), division with a remainder (divmod()), and exponentiation (pow()). Sign function can be created as "sign = lambda n: 1 if n > 0 else -1 if n < 0 else 0".

Math[edit | edit source]

A range of mathematical functions is available from math module of the standard library:

import math

print math.sin(10)       # sine
print math.cos(10)       # cosine
print math.tan(10)       # tangent 

print math.asin(10)      # arc sine
print math.acos(10)      # arc cosine
print math.atan(10)      # arc tangent

print math.sinh(10)      # hyperbolic sine    
print math.cosh(10)      # hyperbolic cosine
print math.tanh(10)      # hyperbolic tangent

print math.pow(2, 4)     # 2 raised to 4
print math.exp(4)        # e ^ 4
print math.sqrt(10)      # square root
print math.pow(5, 1/3.0) # cubic root of 5
print math.log(3)        # ln; natural logarithm
print math.log(100, 10)  # base 10

print math.ceil(2.3)    # ceiling
print math.floor(2.7)   # floor

print math.pi
print math.e

Cmath[edit | edit source]

The cmath module provides similar functions like the math module but for complex numbers, and then some.

Random[edit | edit source]

Pseudo-random generators are available from the random module:

import random
print random.random()     # Uniformly distributed random float >= 0.0 and < 1.0.
print random.random()*10  # Uniformly distributed random float >= 0.0 and < 10.0
print random.randint(0,9) # Uniformly distributed random int >= 0 and <=9
li=[1, 2, 3]; random.shuffle(li); print li # Randomly shuffled list

Decimal[edit | edit source]

The decimal module enables decimal floating point arithmethic, avoiding certain artifacts of the usual underlying binary representation of floating point numbers that are unintuitive to humans.

import decimal
plainFloat = 1/3.0
print plainFloat # 0.3333333333333333
decFloat = decimal.Decimal("0.33333333333333333333333333333333333333")
print decFloat   # Decimal('0.33333333333333333333333333333333333333')
decFloat2 = decimal.Decimal(plainFloat)
print decFloat2  # Decimal('0.333333333333333314829616256247390992939472198486328125')

Fractions[edit | edit source]

The fractions module provides fraction arithmetic via Fraction class. Compared to floating point numbers representing fractions, Fraction fractions do not lose precision.

from fractions import Fraction
oneThird = Fraction(1, 3)
floatOneThird = 1/3.0
print Fraction(0.25)                  # 1/4
print Fraction(floatOneThird)         # 6004799503160661/18014398509481984
print Fraction(1, 3) * Fraction(2, 5) # 2/15

Statistics[edit | edit source]

The statistics module, available since Python 3.4, provides some basic statistical functions. It only provides basics; it does not replace full-fledged 3rd party libraries such as numpy. For Python 2.7, the statistics module can be installed from pypi.

import statistics as stats
print stats.mean([1, 2, 3, 100]) # 26.5
print stats.median([1, 2, 3, 100]) # 2.5
print stats.mode([1, 1, 2, 3]) # 1
print stats.pstdev([1, 1, 2, 3]) # 0.82915...; population standard deviation
print stats.pvariance([1, 1, 2, 3]) # 0.6875; population variance

External links[edit | edit source]