The sum of numbers in the list is required everywhere. Python provides an inbuilt function sum() which sums up the numbers in the list.
Python
arr = [1, 5, 2]
print(sum(arr))
Sum() Function in Python Syntax
Syntax : sum(iterable, start)
- iterable : iterable can be anything list , tuples or dictionaries , but most importantly it should be numbers.
- start : this start is added to the sum of numbers in the iterable. If start is not given in the syntax , it is assumed to be 0.
Possible two more syntaxes
sum(a) : a is the list , it adds up all the numbers in the list a and takes start to be 0, so returning only the sum of the numbers in the list.
sum(a, start) : this returns the sum of the list + start The sum
Python Sum() Function Examples
Get the sum of the list in Python .
Python
numbers = [1,2,3,4,5,1,4,5]
Sum = sum(numbers)
print(Sum)
Sum = sum(numbers, 10)
print(Sum)
Here below we cover some examples using the sum function with different datatypes in Python to calculate the sum of the data in the given input
- Sum Function on a Dictionary
- Sum Function on a Set
- Sum Function on a Tuple
- The sum in Python with For Loop
- Error and Exceptions
- Practical Application
Python Sum Function on a Dictionary
In this example, we are creating a tuple of 5 numbers and using sum() on the dictionary in Python.
Python
my_dict = {'a': 10, 'b': 20, 'c': 30}
total = sum(my_dict.values())
print(total)
Python Sum Function on a Set
In this example, we are creating a tuple of 5 numbers and using sum() on the set in Python.
Python
my_set = {1, 2, 3, 4, 5}
total = sum(my_set)
print(total)
Python Sum Function on a Tuple
In this example, we are creating a tuple of 5 numbers and using sum() on the tuple in Python.
Python
my_tuple = (1, 2, 3, 4, 5)
total = sum(my_tuple)
print(total)
The sum in Python with For Loop
In this, the code first defines a list of numbers. It then initializes a variable called total to 0. The code then iterates through the list using a for loop, and for each number in the list, it adds that number to the total variable. Finally, the code prints the total value, which is the sum of the numbers in the list.
Python
# Define a list of numbers
numbers = [10, 20, 30, 40, 50]
# Initialize a variable to store the sum
total = 0
# Iterate through the list and add each number to the total
for num in numbers:
total += num
# Print the sum of the numbers
print("The sum of the numbers is:", total)
OutputThe sum of the numbers is: 150
Error and Exceptions
TypeError : This error is raised when there is anything other than numbers in the list . In the given example we are using a list of strings rather than an integer.
Python
arr = ["a"]
# start parameter is not provided
Sum = sum(arr)
print(Sum)
# start = 10
Sum = sum(arr, 10)
print(Sum)
Output :
Traceback (most recent call last):
File "/home/23f0f6c9e022aa96d6c560a7eb4cf387.py", line 6, in
Sum = sum(arr)
TypeError: unsupported operand type(s) for +: 'int' and 'str'
Practical Application
Problems where we require the sum to be calculated to do further operations such as finding out the average of numbers.
Python
numbers = [1,2,3,4,5,1,4,5]
# start = 10
Sum = sum(numbers)
average= Sum/len(numbers)
print (average)
Similar Reads
memoryview() in Python The memoryview() function in Python is used to create a memory view object that allows us to access and manipulate the internal data of an object without copying it. This is particularly useful for handling large datasets efficiently because it avoids the overhead of copying data. A memory view obje
5 min read
Python min() Function Python min() function returns the smallest value from a set of values or the smallest item in an iterable passed as its parameter. It's useful when you need to quickly determine the minimum value from a group of numbers or objects. For example:Pythona = [23,25,65,21,98] print(min(a)) b = ["banana",
4 min read
Python next() method Python's next() function returns the next item of an iterator. Example Let us see a few examples to see how the next() method in Python works. Python3 l = [1, 2, 3] l_iter = iter(l) print(next(l_iter)) Output1 Note: The .next() method was a method for iterating over a sequence in Python 2.  It has
4 min read
Python oct() Function Python oct() function takes an integer and returns the octal representation in a string format. In this article, we will see how we can convert an integer to an octal in Python. Python oct() Function SyntaxSyntax : oct(x) Parameters: x - Must be an integer number and can be in either binary, decimal
2 min read
ord() function in Python Python ord() function returns the Unicode code of a given single character. It is a modern encoding standard that aims to represent every character in every language.Unicode includes:ASCII characters (first 128 code points)Emojis, currency symbols, accented characters, etc.For example, unicode of 'A
2 min read
pow() Function - Python pow() function in Python is a built-in tool that calculates one number raised to the power of another. It also has an optional third part that gives the remainder when dividing the result. Example:Pythonprint(pow(3,2))Output9 Explanation: pow(3, 2) calculates 32 = 9, where the base is positive and t
2 min read
Python print() function The python print() function as the name suggests is used to print a python object(s) in Python as standard output. Syntax: print(object(s), sep, end, file, flush) Parameters: Object(s): It can be any python object(s) like string, list, tuple, etc. But before printing all objects get converted into s
2 min read
Python range() function The Python range() function returns a sequence of numbers, in a given range. The most common use of it is to iterate sequences on a sequence of numbers using Python loops.ExampleIn the given example, we are printing the number from 0 to 4.Pythonfor i in range(5): print(i, end=" ") print()Output:0 1
7 min read
Python reversed() Method reversed() function in Python lets us go through a sequence like a list, tuple or string in reverse order without making a new copy. Instead of storing the reversed sequence, it gives us an iterator that yields elements one by one, saving memory. Example:Pythona = ["nano", "swift", "bolero", "BMW"]
3 min read
round() function in Python Python round() function is a built-in function available with Python. It will return you a float number that will be rounded to the decimal places which are given as input. If the decimal places to be rounded are not specified, it is considered as 0, and it will round to the nearest integer. In this
6 min read