AppDividend
Latest Code Tutorials

# Python sum: How to Calculate Sum of List and Tuple

The sum is the most frequent operation in programming, and Python is no different. Python provides the sum() function that can help you calculate the sum of numbers.

## Python sum

The sum() is a built-in Python function that takes an iterable as an argument, adds the elements of an iterable, and returns the sum. To calculate a sum of a list, tuple, dictionary, or set in Python, use the sum() method.

The iterators can be a List, Tuple, Set, or Dictionary.

### Syntax

`sum(iterable, start)`

### Arguments

The sum() function adds the start and elements of the given iterators from left to right.

The iterable may be a Python list, tuple, set, or dictionary.

## How to find a sum of the List in Python

To find a sum of the List in Python, use the sum() method. The sum() is a built-in method that is used to get the summation. You need to define the List and pass the List as a parameter to the sum() function, and you will get the sum of list items in return.

Let’s take a list, apply the sum function, and see the result.

```# app.py

listA = [
11,
18,
19,
21,
46
]

print(sum(listA))
```

See the following output.

```➜  pyt python3 app.py
115
➜  pyt```

If you need to add floating-point numbers with exact precision, you should use the math.fsum(iterable) instead, if you want to concatenate the items of a given iterable (items must be a string), then you can use the Python String join() method.

## How to find a sum of a tuple in Python

To find a sum of the tuple in Python, use the sum() method. Define a tuple with number values and pass the tuple as a parameter to the sum() function; in return, you will get the sum of tuple items.

Let’s define a tuple, pass the tuple in the sum() function, and see the output.

```# app.py

tupA = (
11,
18,
19,
21,
46
)

print(sum(tupA))
```

See the following output.

```➜  pyt python3 app.py
115
➜  pyt```

## How to find Sum of Set elements in Python

To calculate the sum of sets in Python, use the sum() method. First, define a set and pass the set as a parameter to the sum() function; in return, you will get the sum of set items.

See the following code.

```# app.py

setA = {
11,
18,
19,
21,
46
}

print(sum(setA))
```

See the output.

```➜  pyt python3 app.py
115
➜  pyt```

## Find the sum of Dictionary keys in Python.

To find a sum of dictionary keys in Python, use the sum() method. In the case of the Python dictionary, the key to the dictionary will get added. Thus, the output will be the sum of all the dictionary keys.

```# app.py

dictA = {
11: 'Eleven',
18: 'Dustin',
19: 'Mike',
21: 'Lucas',
46: 'Noah'
}

print(sum(dictA))
```

See the output.

```➜  pyt python3 app.py
115
➜  pyt```

The dictionary values are String, but it does not matter here because we will add the keys and not the values.

## Python sum(): error and exceptions

The error is raised when other data types are other than numbers in the List.

See the following code.

```# app.py

dictA = {
'11': 'Eleven',
'18': 'Dustin',
'19': 'Mike',
'21': 'Lucas',
'46': 'Noah'
}

print(sum(dictA))
```

I have taken the keys as a string, not an integer, in the above code. So, it will raise the following error.

```➜  pyt python3 app.py
Traceback (most recent call last):
File "app.py", line 9, in <module>
print(sum(dictA))
TypeError: unsupported operand type(s) for +: 'int' and 'str'
➜  pyt```

You can convert the integer from a string to get the correct result.

## Pass the second parameter

The sum() function takes the second parameter, “start,” as an optional.

It returns the following.

sum of all elements + “start”

See the following example.

```# app.py

listA = [
11,
18,
19,
21,
46
]

print(sum(listA, 19))
```

See the output.

```➜  pyt python3 app.py
134
➜  pyt```

That means 115 + 19 = 134.

## Sum of float numbers in Python

To calculate the sum of float numbers in Python, use math.fsum() method. The math.fsum() is a built-in method that calculates the sum of the floating-point numbers.

You should use math if you need to add floating-point numbers with exact precision.fsum() function instead.

```import math

listA = [1.1, 1.9, 2.1, 4.6]
print(math.fsum(listA))
```

#### Output

`9.7`

If you need to concatenate items of the given iterable (items must be strings), you can use the Python join() method.

That’s it for the sum() function in Python.