运用循环求和( sum operation in python)

1.for loop

 example 1: sum of 1+2+...+10

**********

>>> sum=0

>>> for x in [1,2,3,4,5,6,7,8,9,10]:

        sum=sum+x

>>> print(sum)

**********

example 2: sum of 1+2+...+100

**********

sum=0

for x in range (101):

   sum=sum+x

print(sum)

********

posted @ 2019-02-17 09:54  kaiming_ai  阅读(1160)  评论(0编辑  收藏  举报