How to Add All Numbers in a List in Python

To add all numbers in a list, you can use the sum() function or a for loop in Python.

The following examples show how to add all numbers in a list using different methods.

Using sum() Function

We can use the sum() function to add all numbers in a list.

Suppose we have the following list:

# Declare list
numbers = [1, 2, 3, 4, 5]

# Add numbers
total = sum(numbers)

# Show sum of numbers
print(total)

Output: 👇️

15

In this example, we use the sum() function to calculate the sum of all numbers in the list.

Using For Loop

We can use a for loop to add all numbers in a list.

Suppose we have the same list as above.

We can use the following code to add all numbers in the list:

# Declare list
numbers = [1, 2, 3, 4, 5]

# Declare variable
total = 0

# Use for loop to add numbers
for num in numbers:
    total += num

# Show result    
print("The sum of all numbers in the list is:", total)

Output: 👇️

The sum of all numbers in the list is: 15

In this example, we use a for loop to iterate through the list and add each number to the total variable.