math
is a built-in module in the Python 3 standard library that provides standard mathematical constants and functions. You can use the math
module to perform various mathematical calculations, such as numeric, trigonometric, logarithmic, and exponential calculations.
This tutorial will explore the common constants and functions implemented in the math
module — and how to use them.
The math
Module Constants
There are several built-in constants in the math
module. We'll cover some of the most important constants in this section.
math.pi
The number 𝜋 is a mathematical constant, approximately equal to 3.14159. After importing the math
module, you just need to write math.pi
to access the 𝜋 number:
import mathprint(math.pi)
3.141592653589793
Let’s use the 𝜋 number to calculate the area of a circle. The formula for calculating a circle area is as follows:
$Area = \pi r^2$
import mathdef circle_area(r): return math.pi*r**2radius = 3print("Area =", circle_area(radius))
Area = 28.274333882308138
math.tau
The τ constant returns an almost precise value of $2\pi$. Let's print its value:
print(math.tau)
6.283185307179586
math.e
We can access the number e (or the Euler’s number) simply by using the math.e
constant:
print(math.e)
2.718281828459045
math.nan
The math.nan
constant stands for Not a Number, and it can initialize those variables that aren't numbers. Technically, the data type of the math.nan
constant is float; however, it’s not considered a valid number.
print(math.nan)print(type(math.nan))
nan<class 'float'>
math.inf
The math.inf
constant represents a floating-point positive infinity. It can represent both positive infinity and negative infinity constants, as follows:
print(math.inf)print(-math.inf)
inf-inf
The math
Module Functions
The math
module provides a wide range of mathematical functions for many different scientific and engineering applications, including the following:
- Number functions
- Power and logarithmic functions
- Trigonometric functions
- Angular conversion functions
- Hyperbolic functions
- and some special functions
However, we will only discuss the most important ones in this section. Let's explore them.
Number Functions
math.ceil()
The math.ceil()
method maps a floating-point number to the smallest succeeding integer:
p = 10.1print(math.ceil(p))
11
math.floor()
The math.floor()
method maps a floating-point number to the greatest preceeding integer:
q = 9.99print(math.floor(q))
9
math.factorial()
The math.factorial(n)
method returns the product of all natural numbers less than or equal to n
, if n
a positive integer. However, if n = 0
, it returns 1
. The code below uses the math.factorial()
to calculate $5!$:
n = 5print("{}! = {}".format(n, math.factorial(n)))
5! = 120
math.gcd()
The math.gcd()
method returns the greatest common denominator for two numbers; we can use it to reduce fractions.
For example, the GCD for 25 and 120 is 5, so we can divide the numerator and denominator by 5 to get a reduced fraction (e.g., $\frac{25}{120} = \frac{5}{24}$). Let's see how it works:
gcd = math.gcd(25, 120)print(gcd)
5
math.fabs()
The math.fabs()
method removes the negative sign of a given number, if any, and returns its absolute value as a float:
print(math.fabs(-25))
25.0
Power and Logarithmic Functions
math.pow()
The math.pow()
method returns a floating-point value representing the value of x to the power of y $(x^y)$. Let's try the math.pow()
method by forecasting an investment. To do that, we need to know the initial deposit, the annual interest rate, and the number of years that you invest your money in an investment account. Finally, by using the following formula, we can calculate the final amount of the investment:
${amount} = deposit(1+interest)^{years}$
For example, consider the following values:
- Initial deposit: $10,000
- Annual interest rate: 4%
- Number of years: 5
The code calculates the final amount deposited in the investment account after five years:
deposit = 10000interest_rate = 0.04number_years = 5final_amount = deposit * math.pow(1 + interest_rate, number_years)print("The final amount after five years is", final_amount)
The final amount after five years is 12166.529024000001
The math.pow(x, y)
method always returns a floating-point value, Let's check the data type of the function's return value:
print(type(math.pow(2, 4)))
<class 'float'>
math.exp()
The math.exp(x)
method is equal to $e^x$, where $e$ is the Euler's number. We can say the math.exp(x)
method is equivalent to the statement below:
math.pow(math.e, x)
print(math.exp(3))print(math.pow(math.e, 3))
20.08553692318766820.085536923187664
math.sqrt()
The math.sqrt()
method returns the square root of a number. Let's try it:
print(math.sqrt(16))
4.0
math.log()
The math.log()
method accepts two arguments, x
and base
, where the default value of base is $e$. So the method returns the natural logarithm of x $(\log_e x)$ if we only pass one argument. On the other hand, if we provide two arguments, it calculates the logarithm of x to the given base ($\log_b x$). Let's calculate different logarithms:
print(math.log(10)) print(math.log(10, 3))
2.3025850929940462.095903274289385
The first line returns the natural logarithm of 10, and the second line returns the logarithm of 10 to the base 3.
Although we're able to calculate the logarithm of any number to the base 10 using math.log(x, 10)
, the math
module provides a more accurate method to perform the same calculation. Let's check it out:
print(math.log10(1000))
3.0
Trigonometric Functions
The math
module also provides some useful methods for doing trigonometry. In this section, we'll learn how to calculate the sine, cosine, and tangent of a given value using the following methods provided in the math
module.
math.sin()
The math.sin()
method returns the sine of a given value, where the value must be in radians. The returned value is a floating-point number between -1 and 1:
print(math.sin(math.pi/4))
0.7071067811865476
math.cos()
The math.cos()
method returns the cosine of a given value, and like the math.sin()
method, the value must be in radians. The returned value is a floating-point number between -1 and 1:
print(math.cos(math.pi/2))
6.123233995736766e-17
math.tan()
The math.tan()
method returns a floating-point value representing the tangent of a given value. The value must be in radians. Let's find the tangent of different angles:
print(math.tan(math.pi/4))print(math.tan(math.pi/6))print(math.tan(0))
0.99999999999999990.57735026918962570.0
NOTE
The math
module provides two useful methods for angular conversion. To convert a given angle from radians to degrees, use the math.degrees()
, and to convert a given angle from degrees to radians, use math.radians(x)
.
Hyperbolic Functions
The hyperbolic functions are quite similar to the trigonometric functions; however, there are differences. The math
module provides all the hyperbolic functions that appear in scientific and engineering applications, as follows:
Function | Description |
---|---|
math.cosh(x) | computes the hyperbolic cosine of x. |
math.sinh(x) | computes the hyperbolic sine of x. |
math.tanh(x) | computes the hyperbolic tangent of x. |
math.acosh(x) | computes the inverse hyperbolic cosine of x. |
math.asinh(x) | computes the inverse hyperbolic sine of x. |
math.atanh(x) | computes the inverse hyperbolic tangent of x. |
Let's do some calculations using the hyperbolic functions:
x = 0.5print(math.cosh(x))print(math.sinh(x))print(math.tanh(x))
1.12762596520638070.52109530549374740.46211715726000974
Conclusion
The math
built-in module includes a number of constants and methods that support mathematical operations from basic to advanced. We explored some of the most important and widely used constants and methods, including the number, power and logarithmic, trigonometric functions, and more.
Tutorials