﻿ Python: Compute the sum of elements of a given array of integers - w3resource

Python: Compute the sum of elements of a given array of integers

Python map: Exercise-11 with Solution

Write a Python program to compute the sum of elements of a given array of integers, use map() function.

Sample Solution:

Python Code :

``````from array import array
def array_sum(nums_arr):
sum_n = 0
for n in nums_arr:
sum_n += n
return sum_n

nums = array('i', [1, 2, 3, 4, 5, -15])
print("Original array:",nums)
nums_arr = list(map(int, nums))
result = array_sum(nums_arr)
print("Sum of all elements of the said array:")
print(result)
``````

Sample Output:

```Original array: array('i', [1, 2, 3, 4, 5, -15])
Sum of all elements of the said array:
0
```

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz

﻿

Python: Tips of the Day

```print(2_000_000)
```2000000