Python 循环语句,,Python提供了f


Python提供了for循环和while循环(在Python中没有do..while循环):

循环类型描述
while 循环在给定的判断条件为 true 时执行循环体,否则退出循环体。
for 循环重复执行语句
嵌套循环你可以在while循环体中嵌套for循环



循环控制语句

循环控制语句可以更改语句执行的顺序。Python支持以下循环控制语句:

控制语句描述
break 语句在语句块执行过程中终止循环,并且跳出整个循环
continue 语句在语句块执行过程中终止当前循环,跳出该次循环,执行下一次循环。
pass 语句pass是空语句,是为了保持程序结构的完整性。


PythonWhile循环语句

Python 编程中 while 语句用于循环执行程序,即在某条件下,循环执行某段程序,以处理需要重复处理的相同任务。其基本形式为:

while判断条件:执行语句……

执行语句可以是单个语句或语句块。判断条件可以是任何表达式,任何非零、或非空(null)的值均为true。


实例:

#!/usr/bin/pythoncount=0while(count<9):print‘Thecountis:‘,countcount=count+1print"Goodbye!"

以上代码执行输出结果:

Thecountis:0Thecountis:1Thecountis:2Thecountis:3Thecountis:4Thecountis:5Thecountis:6Thecountis:7Thecountis:8Goodbye!

while 语句时还有另外两个重要的命令 continue,break 来跳过循环,continue 用于跳过该次循环,break 则是用于退出循环,此外"判断条件"还可以是个常值,表示循环必定成立,具体用法如下:

#continue和break用法i=1whilei<10:i+=1ifi%2>0:#非双数时跳过输出continueprinti#输出双数2、4、6、8、10i=1while1:#循环条件为1必定成立printi#输出1~10i+=1ifi>10:#当i大于10时跳出循环break



无限循环

如果条件判断语句永远为 true,循环将会无限的执行下去,如下实例:

#!/usr/bin/pythonvar=1whilevar==1:#该条件永远为true,循环将无限执行下去num=raw_input("Enteranumber:")print"Youentered:",numprint"Goodbye!"


以上实例输出结果:

Enteranumber:20Youentered:20Enteranumber:29Youentered:29Enteranumber:3Youentered:3Enteranumberbetween:Traceback(mostrecentcalllast):File"test.py",line5,in<module>num=raw_input("Enteranumber:")KeyboardInterrupt


注意:以上的无限循环你可以使用 CTRL+C 来中断循环。



循环使用 else 语句

在 python 中,for … else 表示这样的意思,for 中的语句和普通的没有区别,else 中的语句会在循环正常执行完(即 for 不是通过 break 跳出而中断的)的情况下执行,while … else 也是一样。

#!/usr/bin/pythoncount=0whilecount<5:printcount,"islessthan5"count=count+1else:printcount,"isnotlessthan5"

以上实例输出结果为:

0islessthan51islessthan52islessthan53islessthan54islessthan55isnotlessthan5



简单语句组

类似if语句的语法,如果你的while循环体中只有一条语句,你可以将该语句与while写在同一行中, 如下所示:

#!/usr/bin/pythonflag=1while(flag):print‘Givenflagisreallytrue!‘print"Goodbye!"

注意:以上的无限循环你可以使用 CTRL+C 来中断循环。


Pythonfor 循环语句

Python for循环可以遍历任何序列的项目,如一个列表或者一个字符串。

语法:

for循环的语法格式如下:

foriterating_varinsequence:statements(s)

实例:

#!/usr/bin/pythonforletterin‘Python‘:#FirstExampleprint‘CurrentLetter:‘,letterfruits=[‘banana‘,‘apple‘,‘mango‘]forfruitinfruits:#SecondExampleprint‘Currentfruit:‘,fruitprint"Goodbye!"

以上实例输出结果:

CurrentLetter:PCurrentLetter:yCurrentLetter:tCurrentLetter:hCurrentLetter:oCurrentLetter:nCurrentfruit:bananaCurrentfruit:appleCurrentfruit:mangoGoodbye!



通过序列索引迭代

另外一种执行循环的遍历方式是通过索引,如下实例:

#!/usr/bin/pythonfruits=[‘banana‘,‘apple‘,‘mango‘]forindexinrange(len(fruits)):print‘Currentfruit:‘,fruits[index]print"Goodbye!"

以上实例输出结果:

Currentfruit:bananaCurrentfruit:appleCurrentfruit:mangoGoodbye!

以上实例我们使用了内置函数 len() 和 range(),函数 len() 返回列表的长度,即元素的个数。 range返回一个序列的数。



循环使用 else 语句

在 python 中,for … else 表示这样的意思,for 中的语句和普通的没有区别,else 中的语句会在循环正常执行完(即 for 不是通过 break 跳出而中断的)的情况下执行,while … else 也是一样。

如下实例:

#!/usr/bin/pythonfornuminrange(10,20):#toiteratebetween10to20foriinrange(2,num):#toiterateonthefactorsofthenumberifnum%i==0:#todeterminethefirstfactorj=num/i#tocalculatethesecondfactorprint‘%dequals%d*%d‘%(num,i,j)break#tomovetothenextnumber,the#firstFORelse:#elsepartoftheloopprintnum,‘isaprimenumber‘

以上实例输出结果:

10equals2*511isaprimenumber12equals2*613isaprimenumber14equals2*715equals3*516equals2*817isaprimenumber18equals2*919isaprimenumber

Python循环嵌套

Python 语言允许在一个循环体里面嵌入另一个循环。

Python for 循环嵌套语法:

foriterating_varinsequence:foriterating_varinsequence:statements(s)statements(s)

Python while 循环嵌套语法:

whileexpression:whileexpression:statement(s)statement(s)

你可以在循环体内嵌入其他的循环体,如在while循环中可以嵌入for循环, 反之,你可以在for循环中嵌入while循环。

实例:

以下实例使用了嵌套循环输出2~100之间的素数:

#!/usr/bin/pythoni=2while(i<100):j=2while(j<=(i/j)):ifnot(i%j):breakj=j+1if(j>i/j):printi,"是素数"i=i+1print"Goodbye!"

以上实例输出结果:

2是素数3是素数5是素数7是素数11是素数13是素数17是素数19是素数23是素数29是素数31是素数37是素数41是素数43是素数47是素数53是素数59是素数61是素数67是素数71是素数73是素数79是素数83是素数89是素数97是素数Goodbye!


Python 循环语句,布布扣,bubuko.com

Python 循环语句

评论关闭