1. 使用内置函数 `abs()`:
```python
x = -5
result = abs(x)
print(result) 输出:5
2. 使用 `math` 模块中的 `fabs()` 函数:
```python
import math
x = -5.0
result = math.fabs(x)
print(result) 输出:5.0
3. 使用 `numpy` 库中的 `np.abs()` 函数(适用于数组和标量):
```python
import numpy as np
x = np.array([-5, 0, 5])
result = np.abs(x)
print(result) 输出:[5. 0. 5.]
4. 使用条件语句 `if-else`:
```python
x = -10
if x >= 0:
abs_x = x
else:
abs_x = -x
print(abs_x) 输出:10
以上方法都可以用来计算一个数值的绝对值。选择哪种方法取决于你的具体需求,比如是否需要处理数组、是否已经导入了 `numpy` 库等