What is sum () sum () in python?

❮ Built-in Functions

Example

Add all items in a tuple, and return the result:

a = [1, 2, 3, 4, 5]
x = sum[a]

Try it Yourself »

Definition and Usage

The sum[] function returns a number, the sum of all items in an iterable.

Syntax

Parameter Values

ParameterDescription
iterable Required. The sequence to sum
start Optional. A value that is added to the return value

More Examples

Example

Start with the number 7, and add all the items in a tuple to this number:

a = [1, 2, 3, 4, 5]
x = sum[a, 7]

Try it Yourself »

❮ Built-in Functions


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

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Sum of numbers in the list is required everywhere. Python provides an inbuilt function sum[] which sums up the numbers in the list. 

    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 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 

    Below is the Python implementation of the sum[] 

    Python3

    numbers = [1,2,3,4,5,1,4,5]

    Sum = sum[numbers]

    print[Sum]

    Sum = sum[numbers, 10]

    print[Sum]

    Output:

    25
    35

    Error and Exceptions

    TypeError : This error is raised in the case when there is anything other than numbers in the list. 

    Python3

    arr = ["a"]

    Sum = sum[arr]

    print[Sum]

    Sum = sum[arr, 10]

    print[Sum]

    Runtime Error :

    Traceback [most recent call last]:
      File "/home/23f0f6c9e022aa96d6c560a7eb4cf387.py", line 6, in 
        Sum = sum[arr]
    TypeError: unsupported operand type[s] for +: 'int' and 'str'

    So the list should contain numbers Practical Application: Problems where we require sum to be calculated to do further operations such as finding out the average of numbers. 

    Python3

    numbers = [1,2,3,4,5,1,4,5]

    Sum = sum[numbers]

    average= Sum/len[numbers]

    print [average]

    Output:

    3

    What is sum [] in Python?

    Python sum[] Function The sum[] function returns a number, the sum of all items in an iterable.

    How do you write a sum function in Python?

    The sum[] function is used to get the sum of all items in an iterable..
    Version: ... .
    Syntax: sum[iterable[, start]].
    Parameter: ... .
    Return value: ... .
    Example: Python sum[] num = [3.5, 5, 2, -5] # start parameter is not provided numSum = sum[num] print[numSum] # start = 15 numSum = sum[num, 15] print[numSum] ... .
    Pictorial Presentation:.

    Does sum return an int Python?

    Returns an integer indicating the total of all values of the iterable.

    What is sum of series in Python?

    n=int[input["Enter the number of terms:"]] x=int[input["Enter the value of x:"]] sum1=1 for i in range[2,n+1]: sum1=sum1+[[x**i]/i] print["The sum of series is",round[sum1,2]]

    Chủ Đề