Python Programming/Math
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
editA range of mathematical functions is available from math module of the standard library:
import math
v1 = math.sin(10) # sine
v2 = math.cos(10) # cosine
v3 = math.tan(10) # tangent
v4 = math.asin(10) # arc sine
v5 = math.acos(10) # arc cosine
v6 = math.atan(10) # arc tangent
v7 = math.sinh(10) # hyperbolic sine
v8 = math.cosh(10) # hyperbolic cosine
v9 = math.tanh(10) # hyperbolic tangent
vA = math.pow(2, 4) # 2 raised to 4
vB = math.exp(4) # e ^ 4
vC = math.sqrt(10) # square root
vD = math.pow(5, 1/3.0) # cubic root of 5
vE = math.log(3) # ln; natural logarithm
vF = math.log(100, 10) # base 10
vG = math.ceil(2.3) # ceiling
vH = math.floor(2.7) # floor
vI = math.pi
vJ = math.e
Example code using in-built operators
editThis code was made to replicate the log function in a calculator
import time
base_number = input("[A]input base number: ")
new_number = 0
result = input("[end number]input result ")
exponent = 0
while int(new_number) != int(result):
exponent += float("0.0000001")
new_number = int(base_number)**float(exponent)
print(new_number)
else:
print("")
print("The exponent or X is " + str(exponent))
time.sleep(200)
Cmath
editThe cmath module provides similar functions like the math module but for complex numbers, and then some.
Random
editPseudo-random generators are available from the random module:
import random
v1 = random.random() # Uniformly distributed random float >= 0.0 and < 1.0.
v2 = random.random()*10 # Uniformly distributed random float >= 0.0 and < 10.0
v3 = random.randint(0,9) # Uniformly distributed random int >= 0 and <=9
li=[1, 2, 3]; random.shuffle(li); print(li) # Randomly shuffled list
Decimal
editThe 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
v1 = plainFloat # 0.3333333333333333
decFloat = decimal.Decimal("0.33333333333333333333333333333333333333")
v2 = decFloat # Decimal('0.33333333333333333333333333333333333333')
decFloat2 = decimal.Decimal(plainFloat)
v3 = decFloat2 # Decimal('0.333333333333333314829616256247390992939472198486328125')
Fractions
editThe 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
v1 = Fraction(0.25) # 1/4
v2 = Fraction(floatOneThird) # 6004799503160661/18014398509481984
v3 = Fraction(1, 3) * Fraction(2, 5) # 2/15
Statistics
editThe 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
v1 = stats.mean([1, 2, 3, 100]) # 26.5
v2 = stats.median([1, 2, 3, 100]) # 2.5
v3 = stats.mode([1, 1, 2, 3]) # 1
v4 = stats.pstdev([1, 1, 2, 3]) # 0.82915...; population standard deviation
v5 = stats.pvariance([1, 1, 2, 3]) # 0.6875; population variance
External links
edit- 2. Built-in Functions, python.org
- 9. Numeric and Mathematical Modules, python.org
- 9.2. math — Mathematical functions, python.org
- 9.3. cmath — Mathematical functions for complex numbers, python.org
- 9.4. decimal — Decimal fixed point and floating point arithmetic, python.org
- 9.5. fractions — Rational numbers, python.org
- 9.6. random — Generate pseudo-random numbers, python.org
- 9.7. itertools — Functions creating iterators for efficient looping, python.org
- 9.7. statistics — Mathematical statistics functions, python.org