1. 使用内置函数 `abs()`:
```python
x = -5
result = abs(x)
print(result) 输出:5
2. 使用条件语句 `if-else`:
```python
x = -102
if x >= 0:
abs_x = x
else:
abs_x = -x
print(abs_x) 输出:102
3. 使用 `math` 库中的 `fabs()` 函数:
```python
import math
x = -5.0
result = math.fabs(x)
print(result) 输出:5.0
4. 使用 `numpy` 库中的 `abs()` 方法(适用于数组):
```python
import numpy as np
x = np.array([-5, 0, 5])
result = np.abs(x)
print(result) 输出:[5. 0. 5.]