w3resource

Python: Compute the sum of the three lowest positive numbers from a given list of numbers


Sum of Three Lowest Positives

Write a Python program to compute the sum of the three lowest positive numbers from a given list of numbers.

Sample Solution:

Python Code:

# Define a function named sum_three_smallest_nums that takes a list of numbers (lst) as an argument.
def sum_three_smallest_nums(lst):
    # Sum the three smallest positive numbers in the sorted list of positive numbers from the input list.
    return sum(sorted([x for x in lst if x > 0])[:3])

# Create a list of numbers for testing.
nums = [10, 20, 30, 40, 50, 60, 7]
# Print the original list of numbers.
print("Original list of numbers: ", nums)
# Print the sum of the three lowest positive numbers in the list.
print("Sum of the three lowest positive numbers of the said array: ", sum_three_smallest_nums(nums))

# Test with a different list of numbers.
nums = [1, 2, 3, 4, 5]
# Print the original list of numbers.
print("\nOriginal list of numbers: ", nums)
# Print the sum of the three lowest positive numbers in the list.
print("Sum of the three lowest positive numbers of the said array: ", sum_three_smallest_nums(nums))

# Test with another list of numbers that includes zero.
nums = [0, 1, 2, 3, 4, 5]
# Print the original list of numbers.
print("\nOriginal list of numbers: ", nums)
# Print the sum of the three lowest positive numbers in the list.
print("Sum of the three lowest positive numbers of the said array: ", sum_three_smallest_nums(nums))

Sample Output:

Original list of numbers:  [10, 20, 30, 40, 50, 60, 7]
Sum of the three lowest positive numbers of the said array:  37

Original list of numbers:  [1, 2, 3, 4, 5]
Sum of the three lowest positive numbers of the said array:  6

Original list of numbers:  [0, 1, 2, 3, 4, 5]
Sum of the three lowest positive numbers of the said array:  6

Explanation:

Here is a breakdown of the above Python code:

  • Function Definition:
    • The code defines a function named "sum_three_smallest_nums()" that takes a list of numbers (lst) as an argument.
  • List Comprehension:
    • The function uses list comprehension to filter positive numbers from the input list.
  • Sorting and Summing:
    • The positive numbers are sorted, and the sum of the three smallest positive numbers is calculated using slicing ([:3])

Visual Presentation:

Python: Compute the sum of the three lowest positive numbers from a given list of numbers.
Python: Compute the sum of the three lowest positive numbers from a given list of numbers.

Flowchart:

Flowchart: Python - Compute the sum of the three lowest positive numbers from a given list of numbers.

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program that accept two strings and test if the letters in the second string are present in the first string.
Next: Write a Python program to replace all but the last five characters of a given string with "*" and returns the new string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.