# How to Find the Sum of a List in Python

Contents

In this article, you will learn how to find the sum of a list in Python.

## Find the Sum of a List

The sum of a list in Python can be found using the built-in sum() function. The sum() function is a quick and convenient way to calculate the sum of a list of numbers. It takes an iterable (e.g., list, tuple, etc.) as an argument and returns the sum of all elements in the iterable.

Here’s an example of how to use it:

```
numbers = [1, 2, 3, 4, 5]
result = sum(numbers)
print(result)
```

This code creates a list numbers and then calculates the sum of its elements using the sum function. The sum is then stored in the variable result and printed. In this case, the output will be 15.

The function can also take an optional second argument, which specifies the starting value for the sum. For example:

```
numbers = [1, 2, 3, 4, 5]
result = sum(numbers, 10)
print(result)
```

In this case, the output will be 25, because the sum of the elements in the numbers list is 15, and the starting value is 10.

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

```
numbers = [1, 2, 3, 4, 5]
result = 0
for number in numbers:
result += number
print(result)
```

This code creates a variable result and initializes it to 0. Then it iterates over the elements of the numbers list and adds each element to result. Finally, it prints the value of result. In this case, the output will also be 15.

And, the for loop approach allows you to perform other operations on each element before adding it to the sum. For example, you could calculate the square of each element before adding it to the sum:

```
numbers = [1, 2, 3, 4, 5]
result = 0
for number in numbers:
result += number**2
print(result)
```

In this case, the output will be 55, because the sum of the squares of the elements in the numbers list is 55.

So, in conclusion, the choice between using the sum function and using a for loop to find the sum of a list depends on the specific requirements of your problem and what you want to do with each element before summing them.