Python程序设计-第四章 控制流课件

上传人:我*** 文档编号:144996463 上传时间:2020-09-15 格式:PPT 页数:23 大小:108KB
返回 下载 相关 举报
Python程序设计-第四章 控制流课件_第1页
第1页 / 共23页
Python程序设计-第四章 控制流课件_第2页
第2页 / 共23页
Python程序设计-第四章 控制流课件_第3页
第3页 / 共23页
Python程序设计-第四章 控制流课件_第4页
第4页 / 共23页
Python程序设计-第四章 控制流课件_第5页
第5页 / 共23页
点击查看更多>>
资源描述

《Python程序设计-第四章 控制流课件》由会员分享,可在线阅读,更多相关《Python程序设计-第四章 控制流课件(23页珍藏版)》请在金锄头文库上搜索。

1、Python程序设计Programming in Python,主讲:庞胜利,2,第四章 控制流,if while for break continue,控制流简介,在到目前为止我们所见到的程序中,总是有一系列的语句,Python忠实地按照它们的顺序执行它们。如果想要改变语句流的执行顺序,该怎么办呢? 例如,想要让程序做一些决定,根据不同的情况做不同的事情,如:根据时间打印“早上好”或者“晚上好”。 这是通过控制流语句实现的,在Python中有三种控制流语句if、for和while。,if,if语句是选取要执行的操作,是Python主要的选择工具,代表Python程序所拥有的大多数逻辑。 if

2、语句是复合语句,同其他复合语句一样,if语句可以包含其他语句,if 通用格式,if : elif : else: ,if 的例子,#coding:utf-8 number = 23 guess = int(raw_input(Enter an integer : ) if guess = number: print Congratulations, you guessed it. # New block starts here print (but you do not win any prizes!) # New block ends here elif guess number to re

3、ach here print Done # This last statement is always executed, after the if statement is executed,if,Python中没有switch、case语句 可以用多个if实现,或者对字典进行索引运算或搜索列表,因为字典和列表可在运行时创建,有时会比硬编码的if逻辑更有灵活性。,字典实现switch,choice = ham dic = spam: 1.25, ham: 1.99, eggs: 0.99, bacon: 1.10 print dicchoice,if choice = spam: print

4、 1.25 elif choice = ham: print 1.99 elif choice = eggs: print 0.99 elif choice = bacon: print 1.10 else: print bad choice,字典适用于将值和键相关联,值也可以是函数, 因此可以用于更多灵活的处理。,真值测试,在Python中,与大多数程序设计语言一样,整数0代表假,1代表真。不过,除此之外,Python也把任意的空数据结构视为假。 更一般的,真和假的概念是Python中每个对象的固有属性:每个对象不是真就是假,真值测试,数字如果非零,则为真 对象如果非空,则为真 数字零、空对

5、象以及特殊对象None都被认作是假 比较和相等测试会递归的应用在数据结构中 比较和相等测试会返回True或False 布尔and和or运算符会返回真或假的操作对象,真值测试, 23 True, 2 or 3 2 0 or 2 2 or hello hello or ,Python会由左向右求算操作对象,然后返回第一个为真的操作对象,再者Python会在其找到的第一个真值操作数的地方停止,这通常称为短路运算。, 2 and 3 3 2 and and 3 and ,Python会由左向右求算操作对象,然后返回第一个为假的操作对象,再者Python会在其找到的第一个假值操作数的地方停止,这些最终结

6、果都和C及其他语言相同:如果在if测试时, 会得到逻辑真或假的值。 然而,Python中, 布尔运算返回左边或右边的对象,而不是简单的整数标志位。,三元表达式,A = Y?X:Z,if X: A = Y else: A = Z,A = Y if X else Z, a = t if spam else f a t a = t if else f a f,while、for,while、for用于提供循环的控制功能 while一般格式:,while :#Loop test #Loop body else:#Optional else #Run if didnt exit loop with br

7、eak,a = 0 b = 10 while ab: print a a = a + 1,while例子,number = 23 running = True while running: guess = int(raw_input(Enter an integer : ) if guess = number: print Congratulations, you guessed it. running = False # this causes the while loop to stop elif guess number: print No, it is a little lower t

8、han that else: print No, it is a little higher than that else: print The while loop is over. # Do anything else you want to do here print Done,中断循环,在循环进行中,如果满足一定条件而中断整个循环或本次循环,可以使用break或continue。 break语句是用来 终止 循环语句的,哪怕循环条件没有称为False或序列还没有被完全递归,也停止执行循环语句。 注意的是:如果从for或while循环中终止 ,任何对应的循环else块将不执行。,brea

9、k的例子,while True: s = raw_input(Enter something : ) if s = quit: break print Length of the string is, len(s) print Done,continue,continue语句被用来告诉Python跳过当前循环块中的剩余语句,然后 继续 进行下一轮循环,while True: s = raw_input(Enter something : ) if s = quit: break if len(s) 3: print Input is of sufficient length continue

10、# Do other kinds of processing here.,for,for循环在Python中是一个通用的序列迭代器:可以遍历任何有序的序列对象内的元素。 for语句可用于字符串、列表、元组、其他内置可迭代对象,以及用户通过类创建的新对象。,for一般格式,for循环首行定义一个赋值目标,以及想遍历的对象;首行后面是想重复的语句块 运行for循环时,会逐个将序列对象中的元素赋值给目标,然后为每个元素执行循环主体。循环主体一般使用赋值目标来引用序列中当前元素。,for in :#Assign object items to target #Repeated loop body:us

11、e target else: #If we didnt hit a break,for完整格式,for in :#Assign object items to target #Repeated loop body:use target if : break #Exit loop now, skip else if : continue #Go to top of loop now else: #If we didnt hit a break,for例子, for x in a, b, c: . print x . a b c, sum = 0 for x in 1, 2, 3, 4: . sum = sum + x . sum 10, for i in range(0, 5): . print i . 0 1 2 3 4,range,range用来产生整数列表 range(start, stop, step),本章结束,谢谢!,

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

最新文档


当前位置:首页 > 办公文档 > PPT模板库 > PPT素材/模板

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