Skip to content

Sum a list of numbers in Python

Here is a code snippet that demonstrates how to sum a list of numbers in Python:

Sum a list of numbers in Python using the sum function

python
numbers = [1, 2, 3, 4, 5]
total = sum(numbers)
print(total)

<div class="alert alert-info flex not-prose"> Watch a course Python - The Practical Guide</div>

You can also use a for loop to sum the list of numbers:

Sum a list of numbers in Python using a for loop

python
numbers = [1, 2, 3, 4, 5]
total = 0

for num in numbers:
    total += num

print(total)

Both the above code snippet will give you the sum of the list of numbers.

Do you find this helpful?

Dual-run preview — compare with live Symfony routes.