Think Python: Chapter 7 Iteration(迭代) 笔记

目录

这是麻省理工大学(MIT)官方编程教程中Python Tutorial的内容,教材为《Think Python: How to Think Like a Computer Scientist》。这是我的学习笔记,因为水品有限,请大家多多包涵。如果有一起学习的同学可以一起交流。如笔记中错误,请一定要告诉我啊,我肯定及时改正。所有笔记的目录详见:MIT:Python Tutorial目录

这是MIT官方编程教程中Python TutorialUsing if, else, and while的内容。本篇博客为《 Think Python: How to Think Like a Computer Scientist》的第7章 Fruitful Iteration的笔记内容。(Think Python:Chapter 7 Iteration(迭代))和本阶段Python Tutorial:Functions and Scope 课后作业题目(QUESTIONS)的部分编程。和本阶段Python Tutorial:Using if, else, and while 课后作业题目(QUESTIONS)的部分编程。

Chapter 7 Iteration(迭代)

7.1 Multiple assignment(多重任务,多次赋值)

multiple assignment: Making more than one assignment to the same variable during the execution of a program.(注意赋值次序)

7.2 Updating variables

update: An assignment where the new value of the variable depends on the old.
initialization(初始化): An assignment that gives an initial value to a variable that will be updated.
increment(递增): An update that increases the value of a variable (often by one).
decrement(递减): An update that decreases the value of a variable.

7.3 The while statement

iteration(迭代): Repeated execution of a set of statements using either a recursive function call or a loop.
Because iteration is so common, Python provides several language features to make it easier. One is the for statement we saw in Section 4.2. We’ll get back to that later.

#5.8 recursion 循环语句-用if
def countdown(n):
    if n<=0:
        print('Blastoff')
    else:
        print(n)
        countdown(n-1)

Another is the while statement. Here is a version of countdown that uses a while statement:

#7.3 iteration 循环语句-用while
def countdown2(n):
    while n>0:
        print n
        n=n-1
    print('Blastoff')

infinite loop(无限循环): A loop in which the terminating condition is never satisfied.

if,while;循环,迭代的差别
if代表的是recursion(循环),是函数里面再嵌入函数;
while代表的是iteration(迭代),是自动迭代

7.4 break

Sometimes you don’t know it’s time to end a loop until you get half way through the body.In that case you can use the break statement to jump out of the loop:

while True:
    line = raw_input('>' )
    if line == 'done' :
       
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值