Ways to sum in python

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

Example

marks = [65, 71, 68, 74, 61]

# find sum of all marks total_marks = sum(marks)

print(total_marks) # Output: 339


sum() Syntax

The syntax of the sum() function is:

sum(iterable, start)

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


sum() Parameters

  • iterable - iterable (list, tuple, dict, etc). The items of the iterable should be numbers.
  • start (optional) - this value is added to the sum of items of the iterable. The default value of start is 0 (if omitted)

sum() Return Value

sum() returns the sum of start and items of the given iterable.


Example: Working of Python sum()

numbers = [2.5, 3, 4, -5]

# start parameter is not provided

numbers_sum = sum(numbers)

print(numbers_sum) # start = 10

numbers_sum = sum(numbers, 10)

print(numbers_sum)

Output

4.5
14.5

If you need to add floating-point numbers with exact precision, then you should use math.fsum(iterable) instead.

If you need to concatenate items of the given iterable (items must be strings), then you can use the join() method.

'string'.join(sequence)

Visit this page to learn about, Python join() Method

In this short tutorial, we look at different methods to find the sum() of numbers, integers, float numbers and complex numbers in Python

Table of Contents - Python Sum

  • sum () function
    • Sum of numbers
    • Sum of numbers and a start value
    • Sum of float values
    • Sum of float values and integers
    • Sum of complex numbers
  • Creating a for Loop
  • Using Recursion function
  • Closing thoughts

sum() function

The sum() function returns the sum of all items in an iterable. It is a Python built-in function.

There are various methods to do this. We can use Python libraries or can use other methods for the same. Let us look at them one by one.

Syntax:

sum(iterable, start)

Parameters:

Iterable - the values to be added. It is a compulsory field. Iterators can be lists, tuples, strings or dictionaries - basically, a group of numbers.

Start - the value to which the iterable is to be added. It is an optional field and takes the value “0” if we don’t enter any.

Sum of numbers

Example

# using syntax sum(iterable)
a = (1, 3, 5, 7, 9)
b = sum(a)
print(b)

Output

25

Sum of numbers and a start value

Example

# using syntax sum(iterable, start)
a = (1, 3, 5, 7, 9)
b = sum(a, 11)
print(b)

Output

36

Sum of float values

Example

a= (10.2, 12.5, 11.8)
total= sum(a)
print(total)

Output

34.5

Sum of float values and integers

Example

a= (1, 3, 5, 7, 9, 10.2, 12.5, 11.8)
total= sum(a)
print(total)

Output

59.5

Sum of complex numbers

The sum () function can add complex numbers just like it is used to do a sum of integers, float numbers or a combination of both.

Example

s = sum([1 + 3j, 5 + 7j])
print(s)

s = sum([1 + 3j, 5 + 7j], 9 + 11j)
print(s)

s = sum([1 + 3j, 5, 7.5 - 9j])
print(s)

Output

(6+10j)
(15+21j)
(13.5-6j)

Creating a for loop

The for loop runs from the first to the last value in the declared iterable and adds the numbers one by one to the total. We create a variable that holds the sum till the end of loop, and returns the sum of all values. Here’s an example of the for loop to obtain sum when using Python.

Example

sum = 0
for x in [1,3,5,7,9]:
      sum = sum + x

print(sum)

Output

25

Using Recursion function

The function calling itself is called Recursion. To sum numbers in python, we can declare a function to add two values, and call the same to add another value to the return value of the function. Below is an example of how we can use the recursion function.

Example

def total(numTotal):
    theSum = 0
    for i in numTotal:
        theSum = theSum + i
    return theSum

print(total([1,3,5,7,9]))

Output

25

Closing thoughts

The sum of numbers can be obtained in python using the in-build function sum (), by using for loop, or using recursive function. The easy way to sum numbers is by using the sum() function. We can also find sum of list using Python

How do you sum without sum function in Python?

“how to sum a list of numbers in python without using sum” Code Answer.
def int_list(grades): #list is passed to the function..
summ = 0..
for n in grades:.
summ += n..
print summ..

How do you sum multiple values in Python?

You can now use Python's built-in function sum() to add multiple numeric values together. This function provides an efficient, readable, and Pythonic way to solve summation problems in your code. If you're dealing with math computations that require summing numeric values, then sum() can be your lifesaver.