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
[edit | edit source]A range of mathematical functions is available from math module of the standard library:
importmathv1=math.sin(10)# sinev2=math.cos(10)# cosinev3=math.tan(10)# tangent v4=math.asin(10)# arc sinev5=math.acos(10)# arc cosinev6=math.atan(10)# arc tangentv7=math.sinh(10)# hyperbolic sine v8=math.cosh(10)# hyperbolic cosinev9=math.tanh(10)# hyperbolic tangentvA=math.pow(2,4)# 2 raised to 4vB=math.exp(4)# e ^ 4vC=math.sqrt(10)# square rootvD=math.pow(5,1/3.0)# cubic root of 5vE=math.log(3)# ln; natural logarithmvF=math.log(100,10)# base 10vG=math.ceil(2.3)# ceilingvH=math.floor(2.7)# floorvI=math.pivJ=math.e
Example code using in-built operators
[edit | edit source]This code was made to replicate the log function in a calculator
importtimebase_number=input("[A]input base number: ")new_number=0result=input("[end number]input result ")exponent=0whileint(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
[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:
importrandomv1=random.random()# Uniformly distributed random float >= 0.0 and < 1.0.v2=random.random()*10# Uniformly distributed random float >= 0.0 and < 10.0v3=random.randint(0,9)# Uniformly distributed random int >= 0 and <=9li=[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.
importdecimalplainFloat=1/3.0v1=plainFloat# 0.3333333333333333decFloat=decimal.Decimal("0.33333333333333333333333333333333333333")v2=decFloat# Decimal('0.33333333333333333333333333333333333333')decFloat2=decimal.Decimal(plainFloat)v3=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.
fromfractionsimportFractiononeThird=Fraction(1,3)floatOneThird=1/3.0v1=Fraction(0.25)# 1/4v2=Fraction(floatOneThird)# 6004799503160661/18014398509481984v3=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.
importstatisticsasstatsv1=stats.mean([1,2,3,100])# 26.5v2=stats.median([1,2,3,100])# 2.5v3=stats.mode([1,1,2,3])# 1v4=stats.pstdev([1,1,2,3])# 0.82915...; population standard deviationv5=stats.pvariance([1,1,2,3])# 0.6875; population variance
External links
[edit | edit source]- 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