Here are three ways to use Pi:
- Using math.pi
- Using np.pi
- Using scipy.constants.pi
Method 1: Using math.pi
The math module in Python’s standard library includes a constant for π(pi).
Visual Representation
import math
print('The value of pi is: ', math.pi)
Output
The value of pi is: 3.141592653589793
Method 2: Using np.pi
If you are working with numerical computations and have the numpy library installed, you can use π from there as well.
Visual Representation
Example
import numpy as np
print('The value of pi is: ', np.pi)
Output
The value of pi is: 3.141592653589793
Method 3: Using scipy.constants.pi
SciPy is a library used for scientific and technical computing and includes a module for various physical and mathematical constants, including π (pi).
If you don’t have SciPy installed, you can install it using pip. In your command line or terminal, run:
pip install scipy
or
pip3 install scipy
Visual Representation
Example
import scipy.constants as constants
print("Pi (scipy): ", constants.pi)
Output
The value of pi(scipy) is: 3.141592653589793
That’s it.