Easy Tutorial
❮ Python Func Enumerate Ref Random Randint ❯

Python Calculate Sum of Array Elements

Python3 Examples

Define an integer array and calculate the sum of its elements.

Implementation Requirements:

Input: arr[] = {1, 2, 3}

Output: 6

Calculation: 1 + 2 + 3 = 6

Example

# Define a function, arr is the array, n is the length of the array, which is an optional parameter and not used here
def _sum(arr, n): 

    # Use the built-in sum function to calculate
    return(sum(arr)) 

# Call the function
arr = [] 
# Array elements
arr = [12, 3, 4, 15] 

# Calculate the length of the array
n = len(arr) 

ans = _sum(arr, n) 

# Output the result
print('The sum of array elements is', ans)

The output of the above example is:

The sum of array elements is 34

Python3 Examples

❮ Python Func Enumerate Ref Random Randint ❯