codescracker
python

Python Program to Find Sum of Odd and Even Numbers in List



« Previous Program Next Program »

This article deals with some programs in Python that find and prints sum of even and odd numbers in a list given by user at run-time. Here are the list of programs covered in this article:

Find Sum of Even Numbers in a List

The question is, write a Python program that find the sum of even numbers in a list. The elements of list must be received by user at run-time. The answer to this question is the program given below:

nums = []
print("Enter 5 elements for the list: ")
for i in range(5):
    val = int(input())
    nums.append(val)

sum = 0

for i in range(5):
    if nums[i]%2 == 0:
        sum = sum + nums[i]

print("\nSum of Even Numbers is", sum)

Here is the initial output produced by this Python program:

Python find sum of even numbers in list

Now supply the input, that is any 5 elements or numbers for the list, say 1, 2, 3, 4, 5 and press ENTER key to find and print the sum of all even numbers from given five numbers like shown in the snapshot given below:

python sum of even numbers in list

Since the number 2 and 4 from 1, 2, 3, 4, 5 are even, therefore 2+4 or 6 is the output produced by the above program.

Print Sum of Even Numbers in List of n Elements

Basically this is the modified version of previous program. Since this program leaves the size of list to be defined by the user. That is, user is allowed to provide the size, before providing the elements to the list.

nums = []
print("Enter the size of list: ", end="")
tot = int(input())
print("Enter", tot, "Elements for the list: ", end="")
for i in range(tot):
    nums.append(int(input()))

sum = 0
count = 0
for i in range(tot):
    if nums[i]%2 == 0:
        sum = sum + nums[i]
        count = count+1

if count==0:
    print("\nEven number is not found in this list!")
else:
    print("\nSum of Even Numbers =", sum)

Here is its sample run with user input, 6 as size and 1, 2, 3, 4, 5, 6 as six elements:

sum of even numbers in list Python

And here is another sample run with user input, 4 as size, and 1, 3, 5, 7 as four elements. This time, there is no any even numbers in the list:

print sum of even numbers in list python

Modified Version of Previous Program

This program is created to provide the manual message while user enters an invalid inputs. We've done the job using try-except block.

nums = []
print("Enter the size of list: ", end="")
try:
    tot = int(input())
    print("Enter", tot, "Elements for the list: ", end="")
    for i in range(tot):
        try:
            nums.append(int(input()))
        except ValueError:
            print("\nInvalid Element Input!")
            exit()
    sum = 0
    count = 0
    for i in range(tot):
        if nums[i]%2 == 0:
            sum = sum + nums[i]
            count = count+1
    if count==0:
        print("\nEven number is not found in this list!")
    else:
        print("\nSum of Even Numbers =", sum)
except ValueError:
    print("\nInvalid Size Input!")

Short Version of Previous Program

This is the short version of previous program. To be a better program, always try to shorten the code. That is, always create the program as short as possible. As it looks cool and appealing.

nums = []
sum = 0
print("Enter the size of list: ", end="")
tot = int(input())
print("Enter", tot, "Elements for the list: ", end="")
for i in range(tot):
    nums.append(int(input()))
    if nums[i]%2 == 0:
        sum = sum + nums[i]
print("\nSum of Even Numbers =", sum)

Find Sum of Odd Numbers in List

This program is almost same to the program that find sum of even numbers. The only thing you've to do, is to change the if's condition. That is, replace == with !=. Rest of the things are almost same.

nums = []
print("Enter the size of list: ", end="")
tot = int(input())
print("Enter", tot, "Elements for the list: ", end="")
for i in range(tot):
    nums.append(int(input()))

sum = 0
count = 0
for i in range(tot):
    if nums[i]%2 != 0:
        sum = sum + nums[i]
        count = count+1

if count==0:
    print("\nOdd number is not found in this list!")
else:
    print("\nSum of Odd Numbers =", sum)

Here is its sample run with user input 5 as size and 6, 7, 8, 9, 10 as six elements:

sum of odd numbers in list python

Find Sum of Even and Odd Numbers in a List

This program is the combined version of both the program that find and prints even and odd numbers in a list of n numbers.

nums = []
sumeven = 0
counteven = 0
sumodd = 0
countodd = 0

print("Enter the size of list: ", end="")
tot = int(input())
print("Enter", tot, "Elements for the list: ", end="")
for i in range(tot):
    nums.append(int(input()))
    if nums[i]%2 == 0:
        sumeven = sumeven + nums[i]
        counteven = counteven + 1
    else:
        sumodd = sumodd + nums[i]
        countodd = countodd + 1

if counteven==0:
    print("\nEven number is not found in this list!")
else:
    print("\nSum of Even Numbers =", sumeven)
if countodd==0:
    print("Odd number is not found in this list!")
else:
    print("Sum of Odd Numbers =", sumodd)

Here is its sample run with user input, 6 as size, and 10, 11, 12, 13, 14, 15 as six elements:

sum of even and odd numbers in list python

Python Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker