We are given a list of integers and our task is to calculate the sum of only the positive numbers present in the list. In Python, we can solve this problem by iterating through the list and adding the values that are greater than zero. If the list does not contain any positive numbers, the program should return 0 as the result.
Example
[-3, 5, 0, 7, -1, 2]14 (because 5 + 7 + 2 = 14)We only need to iterate through the list and add values that are greater than 0. Python gives multiple concise options — from an explicit loop (clear for beginners) to compact expressions using sum() with a generator or filter().
This method iterates through the list and adds positive numbers one by one to a running total. It is easy to understand and best suited for beginners because the logic is written step by step without using advanced Python features.
# Simple and explicit: iterate and accumulate
nums = [-3, 5, 0, 7, -1, 2]
sum_pos = 0
for x in nums:
if x > 0:
sum_pos += x
print(sum_pos) # Output: 14
Code language: PHP (php)
Output :
14
Explanation :
This method uses a straightforward for-loop and a running total variable sum_pos. For each number, it checks whether the number is positive (> 0) and adds it to the running total when it is. It is easy to read and very suitable for beginners or when you want to show the logic step by step.
sum() with a generator expressionThis method generates only positive numbers using a generator expression and directly sums them using the built-in sum() function. It is efficient and clean because it avoids creating an extra list and processes elements in a single pass.
nums = [-3, 5, 0, 7, -1, 2]
sum_pos = sum(x for x in nums if x > 0)
print(sum_pos) # Output: 14
Code language: PHP (php)
Explanation :
Here sum() is combined with a generator expression x for x in nums if x > 0. The generator produces only positive numbers which sum() then adds. This is concise, efficient (no extra list created) and idiomatic Python for this task.
filter() and sum() This method filters out only the positive numbers using filter() and then calculates their total using the sum() function. It follows a functional programming style and keeps the code compact while still being readable.
nums = [-3, 5, 0, 7, -1, 2]
# filter keeps values where the lambda returns True
sum_pos = sum(filter(lambda x: x > 0, nums))
print(sum_pos) # Output: 14
Code language: PHP (php)
Explanation :filter() selects values that match the condition x > 0. sum() then adds the filtered values. This method reads in a functional style and is clear once you are familiar with filter() and lambda.
This method uses NumPy array operations to quickly select and sum positive values. It is very efficient for large datasets because NumPy performs computations in optimized vectorized form.
import numpy as np
arr = np.array([-3, 5, 0, 7, -1, 2])
sum_pos = arr[arr > 0].sum()
print(int(sum_pos)) # Output: 14
Code language: PHP (php)
Explanation : NumPy is efficient for large numeric arrays. arr > 0 creates a boolean mask; arr[arr > 0] returns only positive values, and .sum() computes their total. Use this method when working with large datasets or when NumPy is already in use.
sum([x for x in nums if x > 0]) would use O(k) extra space where k is the number of positive elements.0.0.int is unbounded, so this is rarely an issue).