Ref Math Pi
## Python math.pi Constant
The **`math.pi`** constant is a pre-defined mathematical constant in Python's built-in `math` module. It represents the mathematical constant $\pi$ (pi), which is the ratio of the circumference of a circle to its diameter.
In Python, `math.pi` is represented as a float value: **`3.141592653589793`**.
---
### Syntax
To use the `math.pi` constant, you must first import the `math` module:
```python
import math
math.pi
```
### Return Value
Returns a float value of `3.141592653589793`, representing the ratio of a circle's circumference to its diameter ($\pi$).
---
### Basic Example
The following example demonstrates how to import the `math` module and print the value of `math.pi`:
```python
# Import the math module
import math
# Print the value of PI
print(math.pi)
```
**Output:**
```text
3.141592653589793
```
---
### Practical Applications
The `math.pi` constant is widely used in geometry, trigonometry, and physics calculations. Below are some common use cases.
#### 1. Calculating the Area and Circumference of a Circle
The formula for the area of a circle is $A = \pi r^2$, and the circumference is $C = 2 \pi r$, where $r$ is the radius.
```python
import math
radius = 5
# Calculate area: pi * r^2
area = math.pi * (radius ** 2)
# Calculate circumference: 2 * pi * r
circumference = 2 * math.pi * radius
print(f"Radius: {radius}")
print(f"Area of the circle: {area:.4f}")
print(f"Circumference of the circle: {circumference:.4f}")
```
**Output:**
```text
Radius: 5
Area of the circle: 78.5398
Circumference of the circle: 31.4159
```
#### 2. Converting Degrees to Radians
Trigonometric functions in Python's `math` module (like `math.sin()`, `math.cos()`) accept angles in radians. You can use `math.pi` to convert degrees to radians manually using the formula: $\text{radians} = \text{degrees} \times \frac{\pi}{180}$.
*(Note: Python also provides a built-in function `math.radians()` for this conversion).*
```python
import math
degrees = 180
# Manual conversion using math.pi
radians = degrees * (math.pi / 180)
print(f"{degrees} degrees is equal to {radians} radians.")
```
**Output:**
```text
180 degrees is equal to 3.141592653589793 radians.
```
---
### Considerations
* **Precision:** `math.pi` provides 15 decimal places of precision, which is the standard precision for a 64-bit floating-point number (double precision) in Python. This is highly accurate and sufficient for almost all scientific and engineering computations.
* **Read-Only:** Although Python does not strictly enforce read-only variables, constants in the `math` module should be treated as immutable. Overwriting `math.pi` (e.g., `math.pi = 3`) is highly discouraged as it will break other mathematical calculations in your runtime environment.
YouTip