Python sum() function


(Sponsors) Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!


The sum() function takes an iterable and returns the sum of items in it.

Syntax:

ParameterDescription
iterable(required) Iterable item like string, list, dictionary etc.
start(optional) An optional numeric value added to the final result. It defaults to 0.

The sum()  function only works with numerical values, trying to use it with non-numeric type will result in an error.

Here is an example:

In the last command, the sum() adds the keys in the dictionary, ignoring its values.

Here is another example, which specifies the start value to be added to the final result.


Other Tutorials (Sponsors)

This site generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!


Leave a Reply

Your email address will not be published. Required fields are marked *