The Python Math Library
Introduction
The Python Math Library provides us access to some common math functions and constants in Python, which we can use throughout our code for more complex mathematical computations. The library is a built-in Python module, therefore you don’t have to do any installation to use it. In this article, we will be showing example usage of the Python Math Library’s most commonly used functions and constants.
Special Constants
The Python Math Library contains two important constants.
Pie
The first one is Pie (π), a very popular math constant. It denotes the ratio of circumference to diameter of a circle and it has a value of 3.141592653589793. To access it, we first import the Math Library as follows:
import math
We can then access this constant using pi
:
math.pi
Output
3.141592653589793
You can use this constant to calculate the area or circumference of a circle. The following example demonstrates this:
import math
radius = 2
print('The area of a circle with a radius of 2 is:', math.pi * (radius ** 2))
Output
The area of a circle with a radius of 2 is: 12.566370614359172
We raised the value