Several methods for python to output the sum of 1 to 100

Use the built-in function range

 print sum(range(1,101))

Use the function reduce

 print reduce(lambda a,b:a+b,range(1,101))

use loop

n = 0
for x in range(101):
   n = x + n

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324883133&siteId=291194637