-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathnegative.py
More file actions
75 lines (53 loc) · 1.26 KB
/
negative.py
File metadata and controls
75 lines (53 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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 numbers
list_num = [-7, 5, 60, -34, 1]
# creating an object of class
obj = Sumofnumbers()
# calling method for sum
# of all negative numbers
obj.negSum(list_num)
# calling method for
# sum of all positive numbers
obj.posSum(list_num)