文档详情

Python基础2优质课件.ppt

发布:2022-05-14约4.68千字共34页下载文档
文本预览下载声明
循环结构 for for x in sequence: statement-block else: else-block 其中else可有可无 for break语句可以强行退出循环。 a [1, 2, 3, 4] for x in a: ... if x==100: break ... else: ... print 100 not found ... 100 not found while while expr1: block else: else-block else可有可无 while i=0 while i 3: ... print i ; i=i+1 ... 0 1 2 谢 谢 聆 听 * 小草从地下探出头来,那是春天的眉毛吧? 早开的野花一朵两朵,那是春天的眼睛吧? 树木吐出点点嫩芽,那是春天的音符吧? 解冻的小溪叮叮咚咚,那是春天的琴声吧? * 程序流程 编程语言的程序流程有3种,顺序,分支,循环。 分支: if expr1: one_line_statement if expr1: statement_block If expr1: statement_block Else(elseif): statement_block 分支 if x0: ... print x is positive ... elif x==0: ... print x is zero ... elif x 0: ... print x is negative ... else: ... print x is not a number 循环结构 for for x in sequence: statement-block else: else-block 其中else可有可无 for for x in abc : print x ... a b c for x in [1,2,3]: print x ... 1 2 3 D={one:1,two:2,three:3} for x in D: print D[,x,]=,D[x] ... D[ three ]= 3 D[ two ]= 2 D[ one ]= 1 for break语句可以强行退出循环。 a [1, 2, 3, 4] for x in a: ... if x==100: break ... else: ... print 100 not found ... 100 not found for range([start,]stop,[,step]) a [1, 2, 3, 4] for x in range(len(a)): ... a[x]=a[x]+2 ... a [3, 4, 5, 6] while while expr1: block else: else-block else可有可无 while i=0 while i 3: ... print i ; i=i+1 ... 0 1 2 while while i 3: ... print i; i = i +1 ... else: ... print i , is 3 ... 0 1 2 i is 3 while while i 3: ... print i; i = i +1 ... else: ... print i , is 3 ... 0 1 2 i is 3 continue语句 continue语句被用来告诉Python跳过当前循环块中的剩余语句,然后 继续 进行下一轮循环。 while True: s = raw_input(Enter something : ) if s == quit: break if len(s) 3: continue print Input is of sufficient length continue语句 在这个程序中,我们反复地取得用户地输入,然后打印每次输入地长度。我们提供了一个特别的条件来停止程序,即检验用户的输入是否是quit。通过 终止 循环到达程序结尾来停止程序。 函数 函数是重用的程序段。它们允许你给一块语句一个名称,然后你可以在你的程序的任何地方使用这个名称任意多次地运行这个语句块。 def function_name ( pa
显示全部
相似文档