TL ; DR:
min(lst) gives the smallest value of a list.
max(lst) gives the largest value.
sum(lst) adds all the values up.
prices = [4, 9, 2, 7]
print(min(prices)) # 2
print(max(prices)) # 9
print(sum(prices)) # 22
Full lesson:
In Maximum Number In Array, a loop found the largest value. In Sum of Numbers, a loop added numbers up. These jobs come up all the time. So 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.
Each one is a single call that runs the loop for you. This loop and max(nums) give the same answer:
biggest = nums[0]
for val in nums:
biggest = max(biggest, val)
That loop uses max too, with two values. Give max separate values and it picks the largest of them. Give it one list and it looks inside the list.
Watch out for an empty list. sum([]) is 0. But min([]) and max([]) stop with a ValueError. An empty list has no smallest value. Check that the list isn't empty first.
Watch out for names. Sum of Numbers named its total sum. That name hides the built-in, and a later sum(prices) fails. Use another name, like total.
Your task
minutes holds how many minutes you practiced coding on each day of a week.
Store the shortest day in shortest and the longest day in longest. Store the week's total in total.
Use the built-ins from this lesson. The code at the bottom prints all three.
Expected output:
shortest = 15
longest = 60
total = 235Free plan: 30 tutor messages. PRO is unlimited.
