Python: Compute the sum of the three lowest positive numbers from a given list of numbers
Python Basic - 1: Exercise-89 with Solution
Write a Python program to compute the sum of the three lowest positive numbers from a given list of numbers.
Sample Solution:
Python Code:
def sum_three_smallest_nums(lst):
return sum(sorted([x for x in lst if x > 0])[:3])
nums = [10, 20, 30, 40, 50, 60, 7]
print("Original list of numbers: ",nums)
print("Sum of the three lowest positive numbers of the said array: ",sum_three_smallest_nums(nums))
nums = [1, 2, 3, 4, 5]
print("\nOriginal list of numbers: ",nums)
print("Sum of the three lowest positive numbers of the said array: ",sum_three_smallest_nums(nums))
nums = [0, 1, 2, 3, 4, 5]
print("\nOriginal list of numbers: ",nums)
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
Pictorial Presentation:
Flowchart:

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.
Python: Tips of the Day
Iterating over dictionaries using 'for' loops:
I am a bit puzzled by the following code: d = {'x': 1, 'y': 2, 'z': 3} for key in d: print key, 'corresponds to', d[key] What I don't understand is the key portion. How does Python recognize ...
key is just a variable name.
for key in d:
For Python 3.x:
for key, value in d.items():
For Python 2.x:
for key, value in d.iteritems():
To test for yourself, change the word key to poop.
In Python 3.x, iteritems() was replaced with simply items(), which returns a set-like view backed by the dict, like iteritems() but even better. This is also available in 2.7 as viewitems().
The operation items() will work for both 2 and 3, but in 2 it will return a list of the dictionary's (key, value) pairs, which will not reflect changes to the dict that happen after the items() call. If you want the 2.x behavior in 3.x, you can call list(d.items()).
Ref: https://bit.ly/37dm0Qo
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework