How can you calculate the sum of a Python array?

In Python, you can use the built-in function sum() to calculate the sum of elements in an array. For example:

arr = [1, 2, 3, 4, 5]
sum_arr = sum(arr)
print(sum_arr)  # 输出:15

The sum() function can also take an initial value as a parameter, for example:

arr = [1, 2, 3, 4, 5]
sum_arr = sum(arr, 10)
print(sum_arr)  # 输出:25

In the above code, the sum() function calculates the sum of elements in the array arr and then adds 10 to the result.

bannerAds