Three built-ins for three common jobs
Finding the largest value and adding numbers up are jobs a loop can do, and you have written those loops. They come up so often that Python has them built in:
min(lst)gives the smallest value of the listlst.max(lst)gives the largest value.sum(lst)adds all the values up.
prices = [4, 9, 2, 7]
print(min(prices))
print(max(prices))
print(sum(prices))
2
9
22
Each one is a single call that runs the loop for you, so no loop is needed.
Two values or a whole list
Both min and max also accept separate values. max(3, 8) is 8, and max([3, 8, 1]) is 8 as well. Give them separate values and they pick the best of those; give them one list and they look inside it. This loop and max(nums) give the same answer, and the loop itself uses max with two values:
biggest = nums[0]
for val in nums:
biggest = max(biggest, val)
The functions work on strings too, comparing them alphabetically: min(["pear", "fig"]) is 'fig'. sum needs numbers, and it also adds decimals: sum([1.5, 2]) is 3.5.
Watch out for an empty list
sum([]) is 0, which makes sense: nothing added up is nothing. But an empty list has no smallest or largest value, so min([]) stops with ValueError: min() iterable argument is empty, and max([]) stops with the same kind of error. A call such as max([], default=0) returns the default you give instead, which is useful when an empty list is a normal case. Check that the list isn't empty before you call them.
When the values are not plain numbers
Both min and max accept a key function, the same idea as in sorting. min(["pear", "fig", "apple"], key=len) gives 'fig', the shortest word. And sum takes an optional second value to start from: sum([1, 2, 3], 10) is 16. You won't need these in the exercise, but they show that the three functions are more flexible than a bare loop.
Watch out for names
If your code uses sum as a variable name for a running total, as a loop often does, that name hides the built-in. A later sum(prices) then fails with TypeError: 'int' object is not callable, because sum now holds a number. Pick another name, like total, and the same goes for min and max.
Loop or built-in?
Write the loop by hand when you need to do something extra on each step, such as skipping values or counting as you go. Use the built-in when the job is exactly "smallest", "largest" or "total": it is shorter, it says what you mean, and there is no starting value to get wrong. A hand-written maximum that starts at 0 fails for a list of negative numbers; max doesn't have that problem.
The exercise, step by step
minutes holds how many minutes you practiced coding on each day of a week. The task is to store three values using the built-ins, and the code at the bottom prints them.
- Store the smallest value in
shortestwithmin. - Store the largest value in
longestwithmax. - Store the sum of all the values in
totalwithsum.
minutes = [25, 40, 15, 60, 30, 45, 20]
shortest = min(minutes)
longest = max(minutes)
total = sum(minutes)
print("shortest =", shortest)
print("longest =", longest)
print("total =", total)
shortest = 15
longest = 60
total = 235
Key takeaways
min,maxandsumeach replace a loop with one call.minandmaxraise aValueErroron an empty list;sum([])is 0.- Don't name a variable
sum,minormax: it hides the built-in.
