Sum () function in Python

The built-in function sum () in Python returns the sum of all numbers in iterable.

The built-in function sum () in Python returns the sum of all numbers in iterable.

Sum function sum ()

 sum(iterable, start) 

The sum () function starts adding from left to right

  1. iterable : built-in iterable (like list, string, dict) needs to be summed, usually numbers.
  2. start : value added to the value returned from iterable. The default value is 0.

Value returned from sum ()

The sum () function returns the sum of start and iterable component items.

For example: How sum () works in Python

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

# không truyền tham số start
numbersSum = sum(numbers)
print(numbersSum)

# start = 10
numbersSum = sum(numbers, 10)
print(numbersSum)

Run the program, the result is:

 4.5 
14.5

See also: Built-in Python functions

You've just finished reading the article "Sum () function in Python" edited by the TipsMake team. You can save sum-function-in-python.pdf to your computer here to read later or print it out. We hope this article has provided you with many useful tech tips and tricks. You can search for similar articles on tips and guides. Thank you for reading and for following us regularly.

« PREV Safe and effective use of portable steam irons
NEXT » Print () function in Python