python中 for 循环和 while循环求 1-100的和

 

1、for 循环

>>> sum = 0
>>> for i in range(101):
    sum += i

    
>>> print(sum)
5050

 

2、while循环

>>> sum = 0
>>> i = 0
>>> while i <= 100:
    sum += i
    i += 1

    
>>> print(sum)
5050

 

posted @ 2021-02-15 22:07  小鲨鱼2018  阅读(5122)  评论(0编辑  收藏  举报