1. 定义一个函数:
```python
def add(a, b):
return a + b
2. 使用内置的 `sum` 函数:
```python
numbers = [1, 2, 3, 4, 5]
total = sum(numbers)
print(total) 输出:15
3. 使用列表推导式:
```python
numbers = [1, 2, 3, 4, 5]
doubled_sum = sum([x * 2 for x in numbers])
print(doubled_sum) 输出:30
4. 使用 `reduce` 函数:
```python
from functools import reduce
numbers = [1, 2, 3, 4, 5]
total = reduce(lambda x, y: x + y, numbers)
print(total) 输出:15
5. 使用 `numpy` 库:
```python
import numpy as np
numbers = np.array([1, 2, 3, 4, 5])
total = np.sum(numbers)
print(total) 输出:15
6. 字符串连接:
```python
str1 = "Hello"
str2 = " World"
result = str1 + str2
print(result) 输出:Hello World
以上是Python中实现加法的一些常见方法。你可以根据具体的需求选择合适的方法