Py4Inf-05-Iterations-PrintPy4Inf-05-Iterations-Print

上传人:海天 文档编号:23993785 上传时间:2017-10-22 格式:PDF 页数:10 大小:315.65KB
返回 下载 相关 举报
Py4Inf-05-Iterations-PrintPy4Inf-05-Iterations-Print_第1页
第1页 / 共10页
Py4Inf-05-Iterations-PrintPy4Inf-05-Iterations-Print_第2页
第2页 / 共10页
Py4Inf-05-Iterations-PrintPy4Inf-05-Iterations-Print_第3页
第3页 / 共10页
Py4Inf-05-Iterations-PrintPy4Inf-05-Iterations-Print_第4页
第4页 / 共10页
Py4Inf-05-Iterations-PrintPy4Inf-05-Iterations-Print_第5页
第5页 / 共10页
亲,该文档总共10页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《Py4Inf-05-Iterations-PrintPy4Inf-05-Iterations-Print》由会员分享,可在线阅读,更多相关《Py4Inf-05-Iterations-PrintPy4Inf-05-Iterations-Print(10页珍藏版)》请在金锄头文库上搜索。

1、Lops and IterationChapter 5Python for Informatics: Exploring IUnless otherwise noted, the content of this course material is licensed under a Creative Commons Attribution 3.0 License.htp:/creativecommons.org/licenses/by/3.0/.Copyright 2010- Charles SeveranceRepeated StepsOutput:54321Blastoff! 0Progr

2、am:n = 5while n 0 :print nn = n - 1print Blastoff!print nn 0 ?n = n -1Lops (repeated steps) have iteration variables that change each time through a lop. Often these iteration variables go through a sequence of numbers.Noprint BlastoffYesn = 5print nAn Infinite Lopn = 5while n 0 :print Latherprint R

3、inseprint Dry off!n 0 ?Noprint Dry off!Yesn = 5print Latherprint RinseWhat is wrong with this lop?Another Lopn = 0while n 0 :print Latherprint Rinseprint Dry off!n 0 ?Noprint Dry off!Yesn = 0print Latherprint RinseWhat does this lop do?Breaking Out of a LopThe break statement ends the current lop an

4、d jumps to the statement immediately folowing the lopIt is like a lop test that can happen anywhere in the body of the lopwhile True:line = raw_input( )if line = done :breakprint lineprint Done! hello therehello there finishedfinished doneDone!Breaking Out of a LopThe break statement ends the curren

5、t lop and jumps to the statement immediately folowing the lopIt is like a lop test that can happen anywhere in the body of the lopwhile True:line = raw_input( )if line = done :breakprint lineprint Done! hello therehello there finishedfinished doneDone!True ?Noprint DoneYes.breakwhile True:line = raw

6、_input( )if line = done :breakprint lineprint Done!http:/en.wikipedia.org/wiki/Transporter_(Star_Trek)Finishing an Iteration with continueThe continue statement ends the current iteration and jumps to the top of the lop and starts the next iterationwhile True:line = raw_input( )if line0 = # :continu

7、eif line = done :breakprint lineprint Done! hello therehello there # dont print this print this!print this! doneDone!Finishing an Iteration with continueThe continue statement ends the current iteration and jumps to the top of the lop and starts the next iterationwhile True:line = raw_input( )if lin

8、e0 = # :continueif line = done :breakprint lineprint Done! hello therehello there # dont print this print this!print this! doneDone!True ?Noprint DoneYes.while True:line = raw_input( )if line0 = # :continueif line = done :breakprint lineprint Done!continueIndefinite LopsWhile lops are caled indefini

9、te lops because they keep going until a logical condition becomes FalseThe lops we have seen so far are pretty easy to examine to see if they wil terminate or if they wil be infinite lopsSometimes it is a little harder to be sure if a lop wil terminateDefinite LopsQuite often we have a list of items

10、 of the lines in a file - effectively a finite set of thingsWe can write a lop to run the lop once for each of the items in a set using the Python for constructThese lops are caled definite lops because they execute an exact number of timesWe say that definite lops iterate through the members of a s

11、etA Simple Definite Lopfor i in 5, 4, 3, 2, 1 :print iprint Blastoff!54321Blastoff!A Simple Definite Lopfriends = Joseph, Glenn, Salyfor friend in friends :print Happy New Year:, friendprint Done!Happy New Year: JosephHappy New Year: GlennHappy New Year: SalyDone!A Simple Definite Lopfor i in 5, 4,

12、3, 2, 1 :print iprint Blastoff!54321Blastoff!Done?Yesprint Blast off!print iNoMove i aheadDefinite lops (for lops) have explicit iteration variables that change each time through a lop. These iteration variables move through the sequence or set. Loking at In.The iteration variable “iterates” though

13、the sequence (ordered set)The block (body) of code is executed once for each value in the sequenceThe iteration variable moves through al of the values in the sequencefor i in 5, 4, 3, 2, 1 :print iIteration variableFive-element sequenceDone?Yesprint iNoMove i ahead The iteration variable “iterates”

14、 though the sequence (ordered set)The block (body) of code is executed once for each value in the sequenceThe iteration variable moves through al of the values in the sequencefor i in 5, 4, 3, 2, 1 :print iDone?Yesprint iNoMove i aheadprint ii = 5print ii = 4print ii = 3print ii = 2print ii = 1for i

15、 in 5, 4, 3, 2, 1 :print iDefinite LopsQuite often we have a list of items of the lines in a file - effectively a finite set of thingsWe can write a lop to run the lop once for each of the items in a set using the Python for constructThese lops are caled definite lops because they execute an exact number of timesWe say that definite lops iterate through the members of a setLop IdiomsWhat We Do in LopsNote: Even though these examples are simple, the patterns apply to al kinds of lopsMaking “smart” lopsThe trick is “knowing” something about

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 行业资料 > 教育/培训

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号