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