Sum of all elements in 1D and 2D array in Python
This program adds the elements from beginning to end. It can be done by built-in functions or using loop. Let us discuss many methods as follows… 1D array elements: The methods are · Using built-in method sum () · Using loops · Using numpy Using built-in method sum (): #create an array of elements n_array = [100, 200, 300, 400, 500] #find the total from the sum() a_total = sum(n_array) #print the sum print("Sum of the elements in the array:", a_total) Output: Sum of the elements in the array: 1500 Using loops: #Array creation n_array = [11, 22, 33, 44, 55] a_total = 0 #using a for loop to find the sum for no in n_array: a_total += no #Print the sum print("Sum of the elements are:", a_total) Output: Sum of the elements are: 165 Using Numpy: ...