Sum a list
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version.
This one builds on a lesson you haven't finished yet. Do that lesson first:
Python Basics · Functions →
Sum a list
Add up every number in a list and return the total. Start a running total at 0, then loop over the list and add each value to it.
This pattern — a running total that grows inside a loop — comes up everywhere in programming.
Difficulty 1/5 · 100 coins + a small bonus on first completion today
Write sum_list(xs) that returns the total of all numbers in the list xs. An empty list sums to 0.
Click Run to see the output here.