Zed-King Institute

    Python Program to Find Sum, Maximum and Minimum of a List

    mediumListsPython

    Reading a whole line of numbers into a list is a pattern you will use in almost every list program.

    The question

    Read a line of integers separated by spaces into a list. Print the sum, then the maximum, then the minimum — each on its own line.

    The code

    program.py

    nums = list(map(int, input().split()))
    print("Sum =", sum(nums))
    print("Maximum =", max(nums))
    print("Minimum =", min(nums))

    Input

    13 51 16 78 53 53 24

    Output

    Sum = 288
    Maximum = 78
    Minimum = 13

    How it works

    1. 1input().split() breaks "4 7 2" into ["4", "7", "2"].
    2. 2map(int, …) converts each piece to a number; list(…) turns the result into a list.
    3. 3sum(), max() and min() are built-in functions that work on any list of numbers.

    Common mistakes

    • Skipping map(int, …) — max(["9", "10"]) compares text and says 9 is bigger.
    • Naming your list sum or max, which hides the built-in function.

    Now solve a similar question yourself

    A new Python question every time, checked instantly.

    Practice questions

    More in Lists