Start learning free

LESSON 269 OF 673 · TUPLES, SORTING & BUILT-INS

Min, Max and Sum in Python: Built-in Functions

Lesson Read the full explanation

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 list lst.
  • 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 = 235
STEP 1 OF 3
    Ctrl + Enter to check

    PYTHON · TUPLES, SORTING & BUILT-INS

    Min Max Sum in Python, explained

    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 list lst.
    • 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.

    1. Store the smallest value in shortest with min.
    2. Store the largest value in longest with max.
    3. Store the sum of all the values in total with sum.
    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, max and sum each replace a loop with one call.
    • min and max raise a ValueError on an empty list; sum([]) is 0.
    • Don't name a variable sum, min or max: it hides the built-in.
    Keep going with Python500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free