find the sum of all elements in an array in python

To find the sum of all elements in an array in python, you can use a for loop to iterate over each element in the array and add it to a running sum. Here's an example code snippet:

main.py
arr = [1, 2, 3, 4, 5]
sum = 0

for i in arr:
    sum += i

print("The sum of all elements in the array is:", sum)
114 chars
8 lines

This code initializes an array arr and a variable sum. It then loops over each element in the array, adding it to sum. Finally, it prints the sum to the console. You can replace arr with any array of integers to find the sum of its elements.

related categories

gistlibby LogSnag