# Python Program to Print Sum of Negative Numbers, Positive Even Numbers and Positive Odd numbers in a List

Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.

List in Python :

The list data type is one of the most often used data types in Python. The square brackets [ ] easily identify a Python List. Lists are used to store data items, with each item separated by a comma (,). A Python List can include data elements of any data type, including integers and Booleans.

One of the primary reasons that lists are so popular is that they are mutable. Any data item in a List can be replaced by any other data item if it is mutable. This distinguishes Lists from Tuples, which are likewise used to store data elements but are immutable.

Given a list the task is to print the sum of all positive even numbers ,odd numbers and negative numbers in the given list in python.

Examples:

Example1:

Input:

given list =[23, 128, -4, -19, 233, 726, 198, 199, 203, -13]

Output:

The sum of all positive even numbers in thee given list  [23, 128, -4, -19, 233, 726, 198, 199, 203, -13] = 1052
The sum of all positive odd numbers in thee given list  [23, 128, -4, -19, 233, 726, 198, 199, 203, -13] = 658
The sum of all positive negative numbers in thee given list  [23, 128, -4, -19, 233, 726, 198, 199, 203, -13] = -36

Example2:

Input:

given list =[-4, 23, 12, -13, 234, 198, 55, -19, 87, 45]

Output:

The sum of all positive even numbers in thee given list [-4, 23, 12, -13, 234, 198, 55, -19, 87, 45]  = 444
The sum of all positive odd numbers in thee given list [-4, 23, 12, -13, 234, 198, 55, -19, 87, 45] =  210
The sum of all positive negative numbers in thee given list [-4, 23, 12, -13, 234, 198, 55, -19, 87, 45] =  -36

## Python Program to Print Sum of Negative Numbers, Positive Even Numbers and Positive Odd numbers in a List

Below are the ways to print the sum of all positive even numbers ,odd numbers and negative numbers in the given list in python.

Drive into Python Programming Examples and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.

### Method #1:Using append() and Conditional Statements (User Input separated by newline)

Approach:

• Take the user’s input on the number of elements to include in the list.
•  Using a for loop, Scan the elements from the user and append them to a list.
• Using a for loop, retrieve the elements from the list one at a time, determine whether they are positive odd, positive even or negative numbers and append them to various lists say posEven, posOdd ,negNum.
• Calculate the sum of posEven, posOdd ,negNum and print them
• Exit of program

Below is the implementation:

# scanning the total number of elements of the given list
totalCount = int(
input("Enter the total number of elements of the given list = "))
# Taking a empty list
given_list = []
# Using for loop to loop totalCount times
for i in range(totalCount):
eleme = int(input("Enter some random element(integer) = "))
given_list.append(eleme)
# Taking three empty lists which stores positive
# ven numbers ,positive odd numbers and negative numbers
posEven = []
posOdd = []
negNum = []
# Traversing the list using for loop
for element in given_list:
# checking if the number is greater than 0
if(element > 0):
# if the element is even then add this element to posEven using append() function
if(element % 2 == 0):
posEven.append(element)
# if the element is even then add this element to posOdd using append() function
else:
posOdd.append(element)
# else if the number is less than 0 then add to negNum list using append() function
else:
negNum.append(element)

# Calculating sum
posEvensum = sum(posEven)
posOddsum = sum(posOdd)
negNumsum = sum(negNum)
# printing the respectve sum's
print("The sum of all positive even numbers in thee given list = ",
given_list, posEvensum)
print("The sum of all positive odd numbers in thee given list = ", given_list, posOddsum)
print("The sum of all positive negative numbers in thee given list = ",
given_list, negNumsum)


Output:

Enter the total number of elements of the given list = 10
Enter some random element(integer) = -4
Enter some random element(integer) = 23
Enter some random element(integer) = 12
Enter some random element(integer) = -13
Enter some random element(integer) = 234
Enter some random element(integer) = 198
Enter some random element(integer) = 55
Enter some random element(integer) = -19
Enter some random element(integer) = 87
Enter some random element(integer) = 45
The sum of all positive even numbers in thee given list [-4, 23, 12, -13, 234, 198, 55, -19, 87, 45]  = 444
The sum of all positive odd numbers in thee given list [-4, 23, 12, -13, 234, 198, 55, -19, 87, 45] =  210
The sum of all positive negative numbers in thee given list [-4, 23, 12, -13, 234, 198, 55, -19, 87, 45] =  -36

### Method #2:Using append() and Conditional Statements (Static Input separated by spaces)

Approach:

• Given the input of the list as static.
• Using a for loop, retrieve the elements from the list one at a time, determine whether they are positive odd, positive even or negative numbers and append them to various lists say posEven, posOdd ,negNum.
• Calculate the sum of posEven, posOdd ,negNum and print them
• Exit of program

Below is the implementation:

# given list
given_list = [23, 128, -4, -19, 233, 726, 198, 199, 203, -13]
# Taking three empty lists which stores positive
# even numbers ,positive odd numbers and negative numbers
posEven = []
posOdd = []
negNum = []
# Traversing the list using for loop
for element in given_list:
# checking if the number is greater than 0
if(element > 0):
# if the element is even then add this element to posEven using append() function
if(element % 2 == 0):
posEven.append(element)
# if the element is even then add this element to posOdd using append() function
else:
posOdd.append(element)
# else if the number is less than 0 then add to negNum list using append() function
else:
negNum.append(element)

# Calculating sum
posEvensum = sum(posEven)
posOddsum = sum(posOdd)
negNumsum = sum(negNum)
# printing the respectve sum's
print("The sum of all positive even numbers in thee given list ",
given_list, "=", posEvensum)
print("The sum of all positive odd numbers in thee given list ",
given_list, "=", posOddsum)
print("The sum of all positive negative numbers in thee given list ",
given_list, "=", negNumsum)


Output:

The sum of all positive even numbers in thee given list  [23, 128, -4, -19, 233, 726, 198, 199, 203, -13] = 1052
The sum of all positive odd numbers in thee given list  [23, 128, -4, -19, 233, 726, 198, 199, 203, -13] = 658
The sum of all positive negative numbers in thee given list  [23, 128, -4, -19, 233, 726, 198, 199, 203, -13] = -36

Related Programs: