YouTip LogoYouTip

Python3 Sum Array

# Python3.x Python Calculate Sum of Array Elements [![Image 3: Document Object Reference](#) 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 function, arr is the array, n is the array length, can be used as an optional parameter, not used here def _sum(arr,n): # Use the built-in sum function 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('Sum of array elements is',ans) The output of the above example is: Sum of array elements is 34 [![Image 4: Document Object Reference](#) Python3 Examples](#)
← Python List InterchangeCpp Nested If β†’