Ref Math Tan
## Python math.tan() Method
The `math.tan()` method is a built-in function in Python's standard `math` module. It returns the tangent of a given angle expressed in radians.
### Introduction
In trigonometry, the tangent of an angle ($\theta$) is the ratio of the sine of that angle to its cosine:
$$\tan(\theta) = \frac{\sin(\theta)}{\cos(\theta)}$$
The `math.tan(x)` method calculates this ratio for a given angle `x` in **radians**. If you have an angle in degrees, you must convert it to radians first before passing it to this method.
* **Python Version:** Introduced in Python 1.4
---
### Syntax
To use the `math.tan()` method, you must first import the `math` module:
```python
import math
math.tan(x)
```
#### Parameters
| Parameter | Type | Description |
| :--- | :--- | :--- |
| **x** | `float` or `int` | **Required.** A numeric value representing the angle in radians. |
#### Return Value
* **Type:** `float`
* **Description:** Returns a floating-point value representing the tangent of the angle `x`.
#### Exceptions
* Returns a `TypeError` if the argument `x` is not a numeric value (e.g., a string or list).
---
### Code Examples
#### Example 1: Basic Usage (Radians)
The following example demonstrates how to find the tangent value of various positive and negative angles in radians.
```python
import math
# Calculate the tangent of different values in radians
print(math.tan(90)) # Tangent of 90 radians
print(math.tan(-90)) # Tangent of -90 radians
print(math.tan(45)) # Tangent of 45 radians
print(math.tan(60)) # Tangent of 60 radians
```
**Output:**
```text
-1.995200412208242
1.995200412208242
1.6197751905438615
0.3200403893795629
```
---
#### Example 2: Converting Degrees to Radians
Because `math.tan()` expects the input to be in radians, passing degree values directly (like $45^\circ$) will yield incorrect results.
To calculate the tangent of an angle in degrees, use `math.radians()` to convert the angle first:
```python
import math
# Angle in degrees
angle_degrees = 45
# Convert degrees to radians
angle_radians = math.radians(angle_degrees)
# Calculate tangent
tan_value = math.tan(angle_radians)
print(f"Tangent of {angle_degrees}Β° is approximately: {tan_value}")
```
**Output:**
```text
Tangent of 45Β° is approximately: 0.9999999999999999
```
*(Note: Due to floating-point precision limitations, the output is extremely close to `1.0` rather than exactly `1.0`.)*
---
### Considerations
1. **Degrees vs. Radians:** A common mistake is passing degrees directly into `math.tan()`. Always convert degrees to radians using `math.radians(degrees)` or by multiplying by $\frac{\pi}{180}$.
2. **Asymptotes (Undefined Tangents):** Mathematically, $\tan(x)$ is undefined at odd multiples of $\frac{\pi}{2}$ (e.g., $90^\circ$, $270^\circ$) because $\cos(x) = 0$. In computer science, because $\pi$ cannot be represented with infinite precision as a floating-point number, `math.tan(math.pi / 2)` will not throw a division-by-zero error. Instead, it will return an extremely large finite number.
YouTip