Scrollable Nav Bar

Python program that given a list of integers finds the sum of all positive numbers

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

  • Input: [-3, 5, 0, 7, -1, 2]
  • Output: 14 (because 5 + 7 + 2 = 14)

Approach

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().


Method 1 — Simple loop

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.


Method 2 — Using sum() with a generator expression

This 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.


Method 3 — Using 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.


Method 4 — Using NumPy

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.


Complexity analysis

  • Time complexity: O(n) for all methods (each element is checked once).
  • Space complexity:
    • Method 1, Method 2 (generator), and Method 3 (filter) use O(1) extra space (generators/iterators do not build a full new list).
    • A list-comprehension version like sum([x for x in nums if x > 0]) would use O(k) extra space where k is the number of positive elements.
    • NumPy uses additional memory for arrays but offers performance for large-scale numeric work.


Edge cases to consider

  • An empty list should return 0.
  • A list with only non-positive numbers (zeros and negatives) should return 0.
  • Mixed positive and negative large values: ensure the variable used to hold the sum can handle the magnitude (Python int is unbounded, so this is rarely an issue).