资源预览内容
第1页 / 共10页
第2页 / 共10页
第3页 / 共10页
第4页 / 共10页
第5页 / 共10页
亲,该文档总共10页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
Lops and IterationChapter 5Python for Informatics: Exploring Informationww.pythonlearn.comUnless 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! 0Program: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 Rinseprint 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 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!Breaking Out of a LopThe break statement ends the current 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_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 = # :continueif 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 line0 = # :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 indefinite 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 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 setA 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, 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 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” 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 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
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号