Python sum() Function
The sum() function sums of all the items of an iterable and returns the total.
If you specify an optional parameter start
, it will be added to the final sum.
Syntax
sum(iterable,start)
sum() Parameters
Python sum()
function parameters:
Parameter | Condition | Description |
---|---|---|
iterable | Required | An iterable (such as list, tuple, dictionary etc.) |
start | Optional | A value to be added to the final sum. Default value is 0 . |
sum() Return Value
Python sum()
function returns the sum of start
and items of the given iterable
.
This function is created specifically for numeric values. For other values, it will raise TypeError
.
Examples
Example 1: Summing Numeric Elements of a List
For example, you can sum numbers
from a list:
numbers = [1, 2, 3, 4, 5]
result = sum(numbers)
print(result) # 15
output
15
Example 2: Providing an Initial start
Value
The start
parameter of the sum()
function allows you to specify an initial value for the summation. This can be especially useful when you want to start the sum from a non-zero value.
numbers = [1, 2, 3, 4, 5]
initial_value = 10
result = sum(numbers, initial_value)
print(result) # Output: 25
output
25
Example 3: Summing Strings (concatenating strings)
You can not sum strings: the sum()
function is primarily used for numeric values!
The join()
method is the correct approach for combining strings in a list into a single string.
strings = ['Hello', ' ', 'World', '!']
result = ''.join(strings)
print(result) # Output: Hello World!
output
Hello World!
Example 4: Summing Elements of a Tuple
This example sums the elements of a tuple containing floating-point numbers:
my_tuple = (10.99, 5.99, 8.49)
result = sum(my_tuple)
print(result) # Output: 25.47
output
25.47
Example 5: Summing Elements of a Tuple nested in a List with map() and sum()
my_tuple = [(1, 3), (5, 6, 7), (2, 6)]
res = sum(map(sum, my_tuple))
print(res) # Output: 30
output
30
Example 6: Summing Values of a Dictionary
To sum the values of a dictionary, you can use the sum()
function in combination with the .values()
method of the dictionary, which returns an iterable of the dictionary's values.
prices_dict = {'apple': 1.2, 'banana': 0.5, 'orange': 0.8}
total_price = sum(prices_dict.values())
print(total_price) # Output: 2.5
output
2.5
Example 7: Summing Floating-Point Numbers of a List
The sum()
function can add up the floating-point numbers in the list.
my_list = [10.2, 12.5, 11.8]
result = sum(my_list)
print(result) # Output: 34.5
output
34.5
Example 8: Summing Complex Numbers of a List
The sum()
function is used to add complex numbers from a list, resulting in a complex number.
my_list = [3 + 2j, 5 + 6j]
result = sum(my_list)
print(result) # Output: (8+8j)
output
(8+8j)
Example 9: Summing Decimal Numbers of a List
This example shows summing Decimal numbers from a list using sum()
function:
from decimal import Decimal
my_list = [Decimal("10.2"), Decimal("12.5"), Decimal("11.8")]
result = sum(my_list)
print(result) # Output: Decimal('34.5')
output
34.5
Example 10: Summing Fraction Numbers of a List
The sum()
function adds Fraction numbers from a list, returning a simplified Fraction as the result.
from fractions import Fraction
my_list = [Fraction(51, 5), Fraction(25, 2), Fraction(59, 5)]
result = sum(my_list)
print(result) # Output: 69/2
output
69/2
Example 11: Concatenating Lists using sum() function
This example shows how sum()
function can be used to concatenate lists, effectively flattening a list of lists. The second argument to sum()
is an empty list, which serves as the starting value for the summation.
my_list = [[1, 2, 3], [4, 5, 6]]
result = sum(my_list, [])
print(result) # Output: [1, 2, 3, 4, 5, 6]
output
[1, 2, 3, 4, 5, 6]
Example 12: Concatenating Tuples using sum() function
This example shows how sum()
function can concatenate tuples by providing an empty tuple as the starting value.
The result contains the flattening of the nested tuples given as input.
my_tuple = ((1, 2, 3), (4, 5, 6))
result = sum(my_tuple, ())
print(result) # Output: (1, 2, 3, 4, 5, 6)
output
(1, 2, 3, 4, 5, 6)