# Python Program to find Sum of Negative, +ve Even and +ve Odd numbers in a List

Sometimes the task is to separate the negative and positive numbers or to separate the odd numbers and even numbers from a list of integers. Here, in this Python article, both these tasks are done. First negative numbers are separated into another list. Then the positive odd and positive even numbers are separated into separate lists. To calculate the sum, the numbers from these negative number lists, positive odd number lists, and positive even number lists are taken to calculate sums separately. In the two different examples, the main list of integers is formed. In example 1, the negative and positive numbers are specified in a negative to positive range. In example 2, a random list of 20 numbers is created by picking these 20 numbers randomly, from a given negative to positive range.

## Example 1 - Find Sum of Negative, +ve Even and +ve Odd numbers in a List having all integers within a specified range.

### Algorithm

Step 1 − Specify the smallest and highest number in the range. The smallest number should be negative and the highest number should be positive. Make a list of integers between the given range

Step 2 − First separate all negative numbers in a list. Add these numbers.

Step 3 − Then separate all positive numbers in a list. Now separate these further in odd and even lists.

Step 4 − Add numbers in the odd positive list and numbers in the even positive list separately. Print all the lists and their calculated sums.

Step 5 − Run the program and then check the result.

### The Python File Contains this

lowNum=-10
highNum=10
mainlist=[]
listPositiveOdd=[]
listPositiveEven=[]
listNeg=[]
sumNeg=0
sumPositiveOdd=0
sumPositiveEven=0

#Making the main list with integers starting from lowNum and upto HighNum
for item in range(lowNum, highNum+1):
mainlist.append(item)
print("In the given range from ", lowNum, " to", highNum, " :")
print("\nThe Main List :")
print(mainlist)

#dividing the main list into negatives, positive odds and positive even
# and calculating their sums separately
for item in mainlist:
if (item > 0):
if (item%2 == 0):
listPositiveEven.append(item)
sumPositiveEven += item
else:
listPositiveOdd.append(item)
sumPositiveOdd += item
elif (item < 0):
listNeg.append(item)
sumNeg += item

print("\nThe Negative Elements in the List :")
print(listNeg)
print("\nThe Sum of all Negative Elements in the List :", sumNeg)

print("\nThe Positive Even Elements in the List :")
print(listPositiveEven)
print("\nThe Sum of all Positive Even Elements in the List :", sumPositiveEven)

print("\nThe Positive Odd Elements in the List :")
print(listPositiveOdd)
print("\nThe Sum of all Positive Odd Elements in the List :", sumPositiveOdd)


### Viewing The Result - Example 1

For seeing the result run the Python file in the cmd window.

In the given range from  -10  to 10  :

The Main List :
[-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

The Negative Elements in the List :
[-10, -9, -8, -7, -6, -5, -4, -3, -2, -1]

The Sum of all Negative Elements in the List : -55

The Positive Even Elements in the List :
[2, 4, 6, 8, 10]

The Sum of all Positive Even Elements in the List : 30

The Positive Odd Elements in the List :
[1, 3, 5, 7, 9]

The Sum of all Positive Odd Elements in the List : 25


Fig 1: Showing the result in the command window.

## Example 2: Find Sum of Negative, +ve Even and +ve Odd numbers in a List having random numbers from a given range.

### Algorithm

Step 1 − Specify the smallest and highest number in the range. The smallest number should be negative and highest number should be positive. Make a list of randomly picked 20 integers between the given range

Step 2 − First separate all negative integers from this random list. Add these numbers.

Step 3 − Then separate all positive integers from this random list. Now separate these further in odd and even lists.

Step 4 − Add all integers in the odd positive list and all integers in the even positive list separately. Print all the lists and their calculated sums.

Step 5 − Execute the program to print the required result.

### The Python File Contains this

lowNum=-100
highNum=200
mainlist=[]
listPositiveOdd=[]
listPositiveEven=[]
listNeg=[]
sumNeg=0
sumPositiveOdd=0
sumPositiveEven=0

#Making the 20 random elements list with integers starting from lowNum and upto #HighNum
import random
#Generate 20 random numbers between lowNum and highNum
randomlist = random.sample(range(lowNum, highNum), 20)

print("In the given range from ", lowNum, " to", highNum, " :")
print("\nThe 20 element Random List :")
print(randomlist)

#dividing the main list into negatives, positive odds and positive even
# and calculating their sums separately
for item in randomlist:
if (item > 0):
if (item%2 == 1):
listPositiveOdd.append(item)
sumPositiveOdd += item
else:
listPositiveEven.append(item)
sumPositiveEven += item
elif (item < 0):
listNeg.append(item)
sumNeg += item

print("\nThe Negative Elements in the List :")
print(listNeg)
print("\nThe Sum of all Negative Elements in the List :", sumNeg)

print("\nThe Positive Even Elements in the List :")
print(listPositiveEven)
print("\nThe Sum of all Positive Even Elements in the List :", sumPositiveEven)

print("\nThe Positive Odd Elements in the List :")
print(listPositiveOdd)
print("\nThe Sum of all Positive Odd Elements in the List :", sumPositiveOdd)


### Viewing The Result - Example 2

Open the cmd window and run the python file to see the result

In the given range from  -100  to 200  :

The 20 element Random List :
[117, -56, 28, 198, 36, 151, 155, 197, 56, -84, 133, 131, 97, 99, 4, 43, 80, 39, 47, 69]

The Negative Elements in the List :
[-56, -84]

The Sum of all Negative Elements in the List : -140

The Positive Even Elements in the List :
[28, 198, 36, 56, 4, 80]

The Sum of all Positive Even Elements in the List : 402

The Positive Odd Elements in the List :
[117, 151, 155, 197, 133, 131, 97, 99, 43, 39, 47, 69]

The Sum of all Positive Odd Elements in the List : 1278


Fig 2: Showing the sums and the elements of the lists.

In this Python article, using two different examples, the way to show how to find the sum of all negative numbers and all positive odd numbers, and all positive even numbers within a given list is given. First, in example 1, the main list is created having all integers within a given range and then in example 2, only some randomly picked elements from a given range are taken for calculations.

Updated on: 10-Jul-2023

221 Views