AppDividend
Latest Code Tutorials

Python Sum Example | sum() Function In Python Tutorial

0

Python Sum Example | sum() Function In Python Tutorial is today’s topic. The sum() function takes an iterable and returns the sum of items in it. The sum() function adds the elements of an iterable and returns the sum. Sum of numbers in the list is required everywhere. Python provides an inbuilt function sum() which sums up the numbers in the list.

Python sum()

The sum() function adds the elements of an iterable and returns the sum. 

The syntax of sum() is following.

sum(iterable, start)

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

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

See the following examples.

#Python List

Okay, let’s take a list and apply the sum function on it 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 then, 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 Python String join() method.

#Python Tuple

Let’s take a tuple.

# app.py

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

print(sum(tupA))

See the following output.

➜  pyt python3 app.py
115
➜  pyt

#Python Set

Let’s take a set.

# app.py

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

print(sum(setA))

See the output.

➜  pyt python3 app.py
115
➜  pyt

#Python Dictionary

In the case of Python dictionary, the key of the dicitonary will get added. The output will be the sum of all the keys of dicitonary.

# 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

See the values of the dictionary is String, but it does not matter here because we will add the keys and not the values.

#Error and Exceptions

The error is raised in the case when there is anything 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))

In the above code, I have taken the keys as a string and not an integer. 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

Now, you can convert the integer from a string in order 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.

Finally, Python Sum Example | sum() Function In Python Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.