math
math module provides access to the mathematical functions defined by the C standard.
Number-theoretic and representation functions
import math
math.ceil(5.8) # 6
ceil(x)
returns the ceiling of x, the smallest integer greater than or equal to x.
math.floor(5.8) # 5
floor(x)
returns he floor of x, the largest integer less than or equal to x.
math.comb(6, 2) # 15
comb is combinations.
comb(n, k)
returns the number of ways to choose k items from n items without repetition and without order.
Evaluates to n! / (k! * (n - k)!)
when k <= n
and evaluates to zero when k > n
.
math.fabs(-5) # 5.0
fabs(x)
returns the absolute value of x.
math.factorial(5) # 120
factorial(n)
returns n factorial as an integer.
math.gcd(12, 60, 30) # 6
gcd(*integers)
returns the greatest common divisor of the specified integer arguments.
math.isclose(5.6, 5.601) # False
math.isclose(5.6, 5.6000000001) # True
isclose(a, b, ...)
returns True
if the values a and b are close to each other and False
otherwise.
The default tolerance is 1e-09
, which assures that the two values are the same within about 9 decimal digits.
math.lcm(6, 5, 3) # 30
lcm(*integers)
returns the least common multiple of the specified integer arguments.
math.perm(6, 2) # 30
perm(n, k)
returns the number of ways to choose k items from n items without repetition and with order.
Evaluates to n! / (n - k)!
when k <= n
and evaluates to zero when k > n
.
Power and logarithmic functions
# Return e raised to the power x, where e = 2.718281… is the base of natural logarithms.
math.exp(5) # 148.4131591025766
# Return 2 raised to the power x.
math.exp2(5) # 32.0
# With one argument, return the natural logarithm of x (to base e).
math.log(math.e ** 2) # 2.0
# With two arguments, return the logarithm of x to the given base,
# calculated as log(x)/log(base).
math.log(25, 5) # 2.0
# Return the base-2 logarithm of x.
math.log2(8) # 3.0
# Return the base-10 logarithm of x.
math.log10(1000) # 3.0
# Return x raised to the power y.
math.pow(2, 10) # 1024.0
# Return the square root of x.
math.sqrt(100) # 10.0
Trigonometric functions
# Return the cosine of x radians.
math.cos(math.pi) # -1.0
# Return the Euclidean distance between two points p and q.
math.dist((0, 0), (2, 2)) # 2.8284271247461903
# Roughly equivalent to:
sqrt(sum((px - qx) ** 2.0 for px, qx in zip(p, q)))
# Return the sine of x radians.
math.sin(math.pi/2) # 1.0
# Return the tangent of x radians.
math.tan(math.pi/4) # 0.9999999999999999, basically 1.0
Angular conversion
# Convert angle x from radians to degress
math.degrees(math.pi) # 180.0
# Convert angle x from degrees to radians.
math.radians(180) # 3.141592653589793
Constants
# The mathematical constant π
math.pi # 3.141592653589793
# The mathematical constant e
math.e # 2.718281828459045
# The mathematical constant τ, which is 2π
math.tau # 6.283185307179586
# A floating-point positive infinity
math.inf
# A floating-point “not a number” (NaN) value
math.nan
# math.nan are not considered to equal to any other numeric value,
# including themselves.
math.nan == math.nan # False
Code Challenge
Try to modify the code in the editor to make it output the answer.