Back to Chapters
Chapter Progress: 0 %0 / 7 Lessons Read
‹›

4. Lists

4.7 List problems: sum, average and missing number

Highlights & notes

Select lesson text to highlight it, or tap Add note.

    Bookmarks

    Bookmark a lesson to see it here.

      Python has built-in helpers for lists of numbers: sum(nums) adds every item, len(nums) counts the items, and max(nums) and min(nums) find the largest and smallest.

      Average = total ÷ count. Use round(value, 2) to keep two decimal places.

      Problem 1: Sum and average

      Find the sum and the average of nums. Print both, with the average rounded to 2 decimal places.

      Expected output
      Sum: 119
      Average: 8.5

      Reference solution
      nums = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
      total = sum(nums)
      average = total / len(nums)
      print("Sum:", total)
      print("Average:", round(average, 2))

      Problem 2: Find the missing number

      nums should hold every number from 1 to 10, but one is missing. Find it without a loop.

      Hint: the numbers 1 to n add up to n * (n + 1) // 2. Here n is one more than the length of the list.

      Expected output
      Missing number: 5

      Reference solution
      nums = [1, 2, 3, 4, 6, 7, 8, 9, 10]
      n = len(nums) + 1
      full_total = n * (n + 1) // 2
      missing = full_total - sum(nums)
      print("Missing number:", missing)

      Step through the code

      Python

      scores = [70, 85, 90]total = sum(scores)average = total / len(scores)print(total, round(average, 2))

      Output

      245 81.67

      Key takeaways

      sum() and len() give you the total and the count; the average is total ÷ count.

      Lesson 20 of 67

      Save your progress

      Start free and your progress, code and notes stay saved. Free for 1 month, no card needed.

      Start free Log in
      PreviousList problems: sum, average and missing number
      Save your progress free for 1 month. Start free · Log in