Python Program to find the sum of even and odd elements of an array


In this program, you will learn how to find the sum of even and odd elements of an array in Python.


a = arr.array('i', [2, 3, 4, 5])

Example: How to find the sum of even and odd elements of an array in Python

import array as arr

a = arr.array('i', [])

se = sod = 0

k = int(input("Enter size of array:"))
for i in range(0, k):
    num = int(input("Enter %d array element:" % (i + 1)))
    a.append(num)

for i in a:
    if i % 2 == 0:
        se = se + i
    else:
        sod = sod + i

print("Sum of even elements:", se)
print("Sum of odd elements:", sod)

Output:

Enter size of array:4
Enter 1 array element:2
Enter 2 array element:3
Enter 3 array element:4
Enter 4 array element:5
Sum of even elements: 6
Sum of odd elements: 8