Python Program to find Sum of Negative, Positive Even and Positive Odd numbers in a List
Given a list. The task is to find the sum of Negative, Positive Even, and Positive Odd numbers present in the List.
Examples:
Input: -7 5 60 -34 1 Output: Sum of negative numbers is -41 Sum of even positive numbers is 60 Sum of odd positive numbers is 6 Input: 1 -1 50 -2 0 -3 Output: Sum of negative numbers is -6 Sum of even positive numbers is 50 Sum of odd positive numbers is 1
Negative numbers are the numbers less than 0 while positive even numbers are numbers greater than 0 and also divisible by 2. 0 is assumed to be a positive even number, in this case.
Approach:
- The first approach input a list of numbers from the user.
- Two loops are run, one for the positive numbers and the other for the negative numbers, computing the numbers’ summation.
- If n is the size of the list of numbers,
- it takes O(2n) time complexity, for iterating over the list of numbers twice.
Python3
class Sumofnumbers: # find sum of negative numbers def negSum(self, list): # counter for sum of # negative numbers neg_sum = 0 for num in list: # converting number # to integer explicitly num = int(num) # if negative number if(num < 0): # simply add to the # negative sum neg_sum + = num print("Sum of negative numbers is ", neg_sum) # find sum of positive numbers # according to categories def posSum(self, list): # counter for sum of # positive even numbers pos_even_sum = 0 # counter for sum of # positive odd numbers pos_odd_sum = 0 for num in list: # converting number # to integer explicitly num = int(num) # if negative number if(num >= 0): # if even positive if(num % 2 == 0): # add to positive # even sum pos_even_sum + = num else: # add to positive odd sum pos_odd_sum + = num print("Sum of even positive numbers is ", pos_even_sum) print("Sum of odd positive numbers is ", pos_odd_sum)# input a list of numberslist_num = [-7, 5, 60, -34, 1]# creating an object of classobj = Sumofnumbers()# calling method for sum# of all negative numbersobj.negSum(list_num)# calling method for# sum of all positive numbersobj.posSum(list_num) |
Output:
Sum of negative numbers is -41 Sum of even positive numbers is 60 Sum of odd positive numbers is 6
The second approach computes the sum of respective numbers in a single loop. It maintains three counters for each of the three conditions, checks the condition and accordingly adds the value of the number in the current sum . If n is the size of the list of numbers, it takes O(n) time complexity, for iterating over the list of numbers once.
Python3
class Sumofnumbers: # find sum of numbers # according to categories def Sum(self, list): # counter for sum # of negative numbers neg_sum = 0 # counter for sum of # positive even numbers pos_even_sum = 0 # counter for sum of # positive odd numbers pos_odd_sum = 0 for num in list: # converting number # to integer explicitly num = int(num) # if negative number if(num < 0): # simply add # to the negative sum neg_sum += num # if positive number else: # if even positive if(num % 2 == 0): # add to positive even sum pos_even_sum + = num else: # add to positive odd sum pos_odd_sum + = num print("Sum of negative numbers is ", neg_sum) print("Sum of even positive numbers is ", pos_even_sum) print("Sum of odd positive numbers is ", pos_odd_sum)# input a list of numberslist_num = [1, -1, 50, -2, 0, -3]# creating an object of classobj = Sumofnumbers()# calling method for# largest sum of all numbersobj.Sum(list_num) |
Output:
Sum of negative numbers is -6 Sum of even positive numbers is 50 Sum of odd positive numbers is 1
Method: Using list comprehension
Python3
lst = [1, -1, 50, -2, 0, -3];i=0x=[i for i in lst if i>0 and i%2==0]y=[i for i in lst if i>0 and i%2!=0]z=[i for i in lst if i<0]print("even positive numbers sum",sum(x))print("odd positive numbers sum",sum(y))print("negative numbers sum",sum(z)) |
even positive numbers sum 50 odd positive numbers sum 1 negative numbers sum -6
Method: Using the lambda function
Python3
lst = [1, -1, 50, -2, 0, -3]x=list(filter(lambda i: (i>0 and i%2==0),lst))y=list(filter(lambda i: (i>0 and i%2!=0),lst))z=list(filter(lambda i: (i<0),lst))print("even positive numbers sum",sum(x))print("odd positive numbers sum",sum(y))print("negative numbers sum",sum(z)) |
even positive numbers sum 50 odd positive numbers sum 1 negative numbers sum -6
Method: Using enumerate function
Python3
lst = ['1', '-1', '50', '-2',' 0', '-3']x=[int(i) for a,i in enumerate(lst) if int(i)%2==0 and int(i)>0]y=[int(i) for a,i in enumerate(lst) if int(i)%2!=0 and int(i)>0]z=[int(i) for a,i in enumerate(lst) if int(i)<0]print("even positive numbers sum",sum(x))print("odd positive numbers sum",sum(y))print("negative numbers sum",sum(z)) |
even positive numbers sum 50 odd positive numbers sum 1 negative numbers sum -6


