Use sum
>>> sum(range(49999951,50000000)) 2449998775L
This is a built-in function , which means that you do not need to import anything or do something special to use it. You should always consult the documentation or study guides before you start asking here if it already exists. In addition, StackOverflow has a search function that could help you find the answer to your problem.
The sum function in this case takes a list of integers and adds them step by step to eachother, similar to the way below
>>> total = 0 >>> for i in range(49999951,50000000): total += i >>> total 2449998775L
Also - similar to Reduce :
>>> reduce(lambda x,y: x+y, range(49999951,50000000)) 2449998775L
source share