数学家高斯的故事。当高斯还是一个小孩时,老师想给全班同学布置很多计算作业。老师让他们从 0 加到 100。高斯想到了
一个聪明办法,在几秒钟内算出了答案,但你可以用 for 循环写一个 Python 程序,替你完成计算。
计算n=1∑100n = 1+2+3+4+5+6+⋯+100的和
i=1∑100i=2n(n+1)
While
while语句的代码:
1 2 3 4 5 6
| n = 1 total = 0 while n <= 100: total = total + n n += 1 print(total)
|
1 2 3 4 5 6 7
| let n = 1 let total = 0 while (n <= 100) { total = total + n; n++; } console.log(total)
|
1 2 3 4 5 6 7 8
| let n = 1 let total = 0 do { total = total + n; n++; } while (n <= 100); console.log(total)
|
break关键词用于让执行提前跳出 while循环。如果执行遇到 break语句,就会马上退出 while 循环。
1 2 3 4 5 6 7 8
| n = 1 total = 0 while True: total = total + n n += 1 if n > 100: break print(total)
|
continue关键词用于让循环继续执行。如果程序执行遇到 continue语句,就会马上跳回到循环开始处,重新开始循环。
1 2 3 4 5 6 7 8 9 10
| n = 1 total = 0 while True: total = total + n n += 1 if n <= 100: continue else: break print(total)
|
换一个顺序,break与continue同理,不作赘述。
1 2 3 4 5 6
| n = 1 total = 1 while n < 100: n += 1 total = total + n print(sum)
|
For
for 循环可以固定循环次数, 所以在知道循环执行的次数的情况下,会很方便。
1 2 3 4
| total = 0 for n in range(101): total = total + n print(total)
|
1 2 3 4
| total = 0 for n in range(100, 0, -1): total = total + n print(total)
|
1 2 3 4 5
| let total = 0 for (let n=1; n <= 100; n++) { total = total + n } console.log(total)
|
然后的话,我们还可以用下面的代码来实现1~100之间的偶数求和。
1 2 3 4
| total = 0 for n in range(2, 101, 2): total += n print(total)
|
也可以通过在循环中使用if分支结构的方式来实现相同的功能:
1 2 3 4 5
| total = 0 for n in range(1, 101): if n % 2 == 0: total += n print(total)
|
也可以使用高阶函数reduce,
1 2 3 4
| from functools import reduce l = range(1,101) print(reduce(lambda x,y:x+y, l))
|