To get the value of pi in Python, the easiest way is use the Python math module constant **pi**. math.pi returns the value 3.141592653589793.

```
import math
print(math.pi)
#Output:
3.141592653589793
```

In Python, we can easily get the value of pi for use in trigonometry

with the Python math module.

For example, let’s say we want to find the sine of a number. The Python sin() function takes a number in radians and then finds the sine of the number.

To find the sine of a number, we can use pi to make our lives easy.

To get the value of pi in your Python code, you just need to import the math module and call it as shown below.

```
import math
print(math.pi)
```

Then, if you want to use it like in our example with finding the sine of a number, you can use the pi constant in the following way.

```
import math
print(math.sin(math.pi/3))
#Output:
0.8660254037844386
```

## Finding pi in Python Without the Math Module

With the Python math module, we can get a pretty good estimate of pi.

However, you may want to calculate pi for yourself without the math module to get more digits and a more accurate representation of pi.

In this case, we can use the Leibniz formula.

Below is a function which will let you calculate pi to more digits than just 15 digits like in the math module. In this example, we use the Python decimal module to calculate pi to 100 digits.

```
import decimal
import math
decimal.getcontext().prec = 100
def leibniz(n):
pi = decimal.Decimal(1)
for i in range (1,n):
pi += ((-1) ** i ) * (decimal.Decimal(1) / (2 * i + 1))
return pi * 4
print(leibniz(1000))
#Output:
Decimal('3.140592653839792925963596502869395970451389330779724489367457783541907931239747608265172332007670214')
```

Hopefully this article has been helpful for you to understand how to find the value of pi from the Python math module.

## Leave a Reply