- Homepage
- Python
Python sum: How to Calculate Sum of List, Tuple
The sum is the most frequent operation in programming, and Python is no different. Python provides the sum[] function that can help you to calculate the sum of numbers.
Python sum
The sum[] is a built-in Python function that takes an iterable as an argument and adds the elements of an iterable and returns the sum. The iterators can be anything like List, Tuple, Set, or Dictionary. To calculate the sum of a list, tuple, dictionary, or set in Python, use the sum[] method.
Syntax
sum[iterable, start]Arguments
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.
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 of
You need to define the list and pass the list as a parameter to the sum[] function, and in return, you will get the sum of list items.
Lets 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 pytIf 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 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. For example, define a tuple with number values and pass the tuple as a parameter to the sum[] function, and in return, you will get the sum of tuple items.
Lets define a tuple and pass the tuple in the sum[] function, and see the output.
See the following output.
pyt python3 app.py 115 pytHow to find Sum of Set elements in Python
To calculate the sum of set in Python, use the sum[] method. Define a set and pass the set as a parameter to the sum[] function, and 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 pytFind 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 keys of the dictionary.
# app.py dictA = { 11: 'Eleven', 18: 'Dustin', 19: 'Mike', 21: 'Lucas', 46: 'Noah' } print[sum[dictA]]See the output.
pyt python3 app.py 115 pytSee, 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 there are other data types instead of 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 print[sum[dictA]] TypeError: unsupported operand type[s] for +: 'int' and 'str' pytNow, 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 pytThat 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.
If you need to add floating-point numbers with exact precision, then you should use math.fsum[] function instead.
import math listA = [1.1, 1.9, 2.1, 4.6] print[math.fsum[listA]]Output
9.7If you need to concatenate items of the given iterable [items must be strings], you can use the Python join[] method.
Thats it for the sum[] function in Python.
See also
Python mean of List
Python median[]
Python variance[]
Python mode[]
Python stddev[]
Krunal Lathiya is an Information Technology Engineer. By profession, he is a web developer with knowledge of multiple back-end platforms [e.g., PHP, Node.js, Python] and frontend JavaScript frameworks [e.g., Angular, React, and Vue].
- Python os.path.commonprefix[] Function: Complete Guide
- Python os.path.commonpath[]: Complete Guide
- Python os.path.isdir[] Function: The Complete Guide
Related Post
Recent Posts
- Cloud Computing
Difference Between Cloud and VPS Hosting: Guide in 2022
In this post, we will see the Difference Between Cloud and VPS Hosting. As you
- Python
Python Comparison Operators: The Complete Guide
Python comparison operators determine the equality or difference between values. The expression ultimately returns a
- PHP
PHP chunk_split: How to Split String in PHP
PHP chunk_split[] is a string function that splits the string into a series of smaller
- Javascript
JavaScript array some: How to Check Array Element in JavaScript
Javascript arraysome[] function tests whether some item in the array passes the test implemented by
- PHP
PHP str_split: How to Split a String in PHP
The str_split[] function splits the given string into smaller strings of the length specified by
- Python
Python all: How to Use all[] Method in Python
Python all Python all[] is a built-in function that returns True when all items in
Headline
- t
- L