Numeric and Mathematical Modules

math — mathematical functions

provides access to the mathematical functions defined by the C standard.

import math
e = math.e
#mathematical constant e = 2.718281…
p = math.pi
#mathematical constant π = 3.141592…
t = math.tau
#mathematical constant τ = 6.283185…
c = math.sqrt(4)
#the square root of x.
cos_x = math.cos(4)
#return the arc cosine of x, in radians. 
sin_e = math.sin(math.e)
#return the sine of x radians.
print(e)
print(p)
print(t)
print(cos_x)
print(sin_e)

#output:
# 2.718281828459045
# 3.141592653589793
#6.283185307179586
# -0.6536436208636119
# 0.41078129050290885
g1 = math.comb(4,2) #output: 6
g2 = math.comb(2,4) #output: 0
#  n! / (k! * (n - k)!) when k <= n 

cmath — mathematical functions for complex numbers

provides access to mathematical functions for complex numbers. The functions in this module accept integers, floating-point numbers or complex numbers as arguments.

p = cmath.pi
#mathematical constant π, as a float.
print(p)
#3.141592653589793
e = cmath.e
#mathematical constant e, as a float.
print(e)
#2.718281828459045
t = cmath.tau
#mathematical constant τ, as a float.
print(t)
#6.283185307179586

decimal — decimal fixed-point and floating-point arithmetic

provides support for fast correctly rounded decimal floating-point arithmetic

from decimal import *

a = 1
b = 7
c = 13
print(a/b,c/b,a/c)
#0.14285714285714285 1.8571428571428572 0.07692307692307693

getcontext().prec = 2 
#prec means the number of significant digits, not decimal digits.
print(Decimal(a)/Decimal(b)) #0.14
print(x.quantize(Decimal("0.0000")))  # 0.1429
print(Decimal(c)/Decimal(b)) #0.54
print(Decimal(a)/Decimal(c)) #0.077

fractions — rational numbers

from fractions import Fraction
print(Fraction(16, -10))
print(Fraction(-8, 6))
print(Fraction(3, 7))
print(Fraction('1.414213 \t\n'))
print(Fraction(Decimal('1.1')))
#The fractions module is for exact fractions, 
#without approximations.

random — generate pseudo-random numbers

import random
from decimal import *

#different tuples every run
print(random.random())
print(random.randrange(1,10))

print(random.getstate())
print(type(random.getstate()))
print(len(random.getstate()))

print(random.randint(10,100))

L1 = [1,7,2,4,9,0,10,11,99,100]
print(random.choice(L1))

L2 = ["One", "Two", "Three", "Other"]
print(random.choices(L2))
print(
    random.choices(L2,
                   [1,2,3,11], #probability weights/
                             #probabiliuty of use 
                            #for each elem of L2
                   k= 6)) # how many elements to draw

getcontext().prec =3
L3 = [round(math.e,3), 
      round(math.pi,3), 
      float((Decimal(1)/Decimal(7))), 
      round(math.tau,3)]
print(L3)
print(random.sample(L3,2))

statistics — mathematical statistics functions

import math
from statistics import *

a = 1
b = 2

T = (1,8,2,3,4,5,6,7,8,9,0,11,67,99,101,112,119, 
     89,3,22,1,9,9,3,2,7,8,9,0)
L = [round(math.e,2), round(math.pi,2), round(math.tau,2), 
     round(1/7), 2.22,6.89,7.21,8.19]
L2 = [1.0,2.0,3.0, round(math.e,2), round(math.pi,2), 
      round(math.tau,2), round(1/7), 100.01 ]


print(len(L))
print(len(L2))

a = int(len(L))
b = int(len(L2))
c = a - b
d = b - a

print(a,b)

if b > a :
    print(f"you need to increase the number of data in 'L' by:"
          f"{d}"),
elif a > b:
    print(f"you need to increase the number of data in 'L2' by:"
          f"{c}")
elif a == 0 or b == 0:
    print("at least one of the lists is empty")
else:
    print("both lists contain the same amount of data")

print(covariance(L,L2))
print(correlation(L,L2))
print(linear_regression())