python数据处理的流程控制课件.ppt
《python数据处理的流程控制课件.ppt》由会员分享,可在线阅读,更多相关《python数据处理的流程控制课件.ppt(96页珍藏版)》请在三一办公上搜索。
1、第二次上机总结1,1、以file的形式写代码,交互式的界面只适合于单语句代码的测试2、file文件中以函数为单位写代码,方便调用以实现代码重用3、inport语句放在程序的开头,即程序总注释后面,第一个函数前面,1,第二次上机总结11、以file的形式写代码,交互式的界面只适,第二次上机总结2,提高课堂效率了解每次课的主要内容,2,第二次上机总结2提高课堂效率2,第3章,数据处理的流程控制,第3章数据处理的流程控制,计算机程序根据语句的顺序依次向下执行。,# convert.py# A program to convert Celsius temps to Fahrenheitdef main
2、(): celsius = input(What is the .”) fahrenheit = 9.0 / 5.0 * celsius + 32 print The temperature is, fahren.main(),计算机程序根据语句的顺序依次向下执行。# conve,流程图:用标准化的图形符号来表示程序步骤,流程图:用标准化的图形符号来表示程序步骤,但这种顺序执行显然是不够的,我们会要求某个功能重复执行,也会要求根据不同的前提条件执行不同的功能,所以引入了另两种控制结构分支和循环,但这种顺序执行显然是不够的,,3.2 分支结构3.3 异常处理3.4 循环结构3.5 结构化程序设计
3、,1 单分支结构2 二分支结构3 多分支结构,3.2 分支结构1 单分支结构,(一)温度转换的例子(eg3_1.py),# convert.py# A program to convert Celsius temps to Fahrenheitdef main(): celsius = input(What is the .”) fahrenheit = 9.0 / 5.0 * celsius + 32 print The temperature is, fahren.main(),(一)温度转换的例子(eg3_1.py)# convert.,(一)温度转换的例子,希望他可以根据不同的温度给出
4、不同的提示。If fahrenheit 90 print a hot warningIf fahrenheit 30 print a cold warning,(一)温度转换的例子希望他可以根据不同的温度给出不同的提示。,(一)温度转换的例子:流程图,(一)温度转换的例子:流程图,(一)温度转换的例子改进版,# convert2.pydef main(): celsius = input(What is the .? ) fahrenheit = 9.0 / 5.0 * celsius + 32 print The temperature is, fahrenheit,. if fahrenh
5、eit 90: print Its really hot out there, . if fahrenheit 30: print Brrrrr. Be sure to dress warmlymain(),单分支语句,(一)温度转换的例子改进版# convert2.py单分支语,(二)用于执行决策的if语句,语法:if :(if fahrenheit 90:) next 语义:先判断条件表达式(condition)true, 执行语句体(body),再转向下一个语句false, 忽略语句体(body),直接转向下一个语句语句体(body):一个或多个语句 在:后面缩进显示,body中的各语句
6、缩进对齐,(二)用于执行决策的if语句语法:if condition,(三)条件表达式的格式,简单条件表达式(condition):比较两个表达式 (a+2 =, , !=数值比较:整型数的比较,实型数的比较字符串比较: 按字典序。字母序由编码(ASCII等)决定. 如:大写字母在小写字母前.称为布尔表达式结果为true/false,1/0,Relation operation(关系运算符),(三)条件表达式的格式简单条件表达式(condition):,3.2 分支结构3.3 异常处理3.4 循环结构3.5 结构化程序设计,1 单分支结构2 二分支结构3 多分支结构,3.2 分支结构1 单分支
7、结构,求一元二次方程的例子(ver1.0),# quadratic.py import mathdef main(): print This program finds the real . a, b, c = input(Please enter. (a, b, c): ) discRoot = math.sqrt(b * b - 4 * a * c) root1 = (-b + discRoot) / (2 * a) root2 = (-b - discRoot) / (2 * a) print The solutions are:, root1, root2 main(),求一元二次方程
8、的例子(ver1.0)# quadratic.,This program finds the real solutions to a quadraticPlease enter the coefficients (a, b, c): 1,1,2Traceback (most recent call last): File C:Documents and SettingsTerryMy DocumentsTeachingW04CS 120Textbookcodechapter3quadratic.py, line 21, in -toplevel- main() File C:Documents
9、 and SettingsTerryMy DocumentsTeachingW04CS 120Textbookcodechapter3quadratic.py, line 14, in main discRoot = math.sqrt(b * b - 4 * a * c)ValueError: math domain error,ver1.0的运行,引入决策分支结构,This program finds the real so,# quadratic2.py,当discrim=0时才有解import math def main(): print This program finds the
10、real solutions .n a, b, c = input(Please enter the . (a, b, c): ) discrim = b * b - 4 * a * c if discrim = 0: discRoot = math.sqrt(discrim) root1 = (-b + discRoot) / (2 * a) root2 = (-b - discRoot) / (2 * a) print nThe solutions are:, root1, root2main(),求一元二次方程的例子(ver2.0),# quadratic2.py,当discrim=0时
11、才有,ver2.0的运行结果,This program finds the real solutions to a quadraticPlease enter the coefficients (a, b, c): 1,1,1,discRoot0时,没有任何执行语句就结束了,应该给出提示,ver2.0的运行结果 This program fin,# quadratic2.py的改良import math def main(): print This program finds the real solutions .n a, b, c = input(Please enter the . (a
12、, b, c): ) discrim = b * b - 4 * a * c if discrim = 0: discRoot = math.sqrt(discrim) root1 = (-b + discRoot) / (2 * a) root2 = (-b - discRoot) / (2 * a) print nThe solutions are:, root1, root2 if discRoot 0: print “The equation has no real roots!” main(),求一元二次方程的例子(ver2.0+),该程序两个if语句的流程图?,做了基于同一个值的两
13、次条件表达式的判断,不够高效,# quadratic2.py的改良求一元二次方程的例子(v,二分支结构,二分支结构,二分支结构,语法: if : else: next statements,二分支结构语法:,# quadratic3.py 考虑到负值并会给出提示import math def main(): print This program finds the real solutions .n a, b, c = input(Please enter the . (a, b, c): ) discrim = b * b - 4 * a * c if discrim 0: print nT
14、he equation has no real roots! else: discRoot = math.sqrt(b * b - 4 * a * c) root1 = (-b + discRoot) / (2 * a) root2 = (-b - discRoot) / (2 * a) print nThe solutions are:, root1, root2 main(),求一元二次方程的例子(ver3.0),# quadratic3.py 考虑到负值并会给出提示求一元,This program finds the real solutions to a quadratic Pleas
15、e enter the coefficients (a, b, c): 1,2,1 The solutions are: -1.0 -1.0 该程序还可以改善:同时0三种情况,ver3.0的运行结果,ver3.0的运行结果,3.2 分支结构3.3 异常处理3.4 循环结构3.5 结构化程序设计,1 单分支结构2 二分支结构3 多分支结构,3.2 分支结构1 单分支结构,多分支结构,多分支结构,if discrim 0: print Equation has no real rootselse: #大于等于0的情况 if discrim = 0: root = -b / (2 * a) prin
16、t There is a double root at, root else: #大于0的情况 discRoot = math.sqrt(b * b - 4 * a * c) root1 = (-b + discRoot) / (2 * a) root2 = (-b - discRoot) / (2 * a) print nThe solutions are:, root1, root2,if.else的嵌套: if或else后面的语句块可以是ifelse语句,多分支结构的实现1: if.else的嵌套,if discrim 0:if.else的嵌套:多分,教材p121: 第13题,程序设计:
17、 输入百分制的考试分数,输出相应的等级制名称。设A:90-100, B:80-89, C:70-79, D:60-69, F: 60分以下,多层嵌套会导致缩进很多,教材p121: 第13题程序设计: 输入百分制的考试分数,输,多分支结构的实现2: elif,语法: if : elif : elif : else: ,语义: 找到第一个为真的条件表达式并执行对应语句序列,控制转向下一条语句;若无,则执行else下的语句序列,控制转向下一条语句。,多分支结构的实现2: elif语法:语义:,# quadratic4.py 同时考虑三种情况import math def main(): print
18、This program finds the real solutions .n a, b, c = input(Please enter the . (a, b, c): ) discrim = b * b - 4 * a * c if discrim 0: print nThe equation has no real roots! elif discrim = 0: root = -b / (2 * a) print nThere is a double root at, root else: discRoot = math.sqrt(b * b - 4 * a * c) root1 =
19、 (-b + discRoot) / (2 * a) root2 = (-b - discRoot) / (2 * a) print nThe solutions are:, root1, root2,求一元二次方程的例子(ver4.0),# quadratic4.py 同时考虑三种情况求一元二次方,教材p121: 第13题,程序设计: 输入百分制的考试分数,输出相应的等级制名称。设A:90-100, B:80-89, C:70-79, D:60-69, F: 60分以下,教材p121: 第13题程序设计: 输入百分制的考试分数,输,Boolean操作符,有时候简单的bool表达式还不够用an
20、d、or、not 等去组合多个布尔表达式 and or not ,Boolean操作符有时候简单的bool表达式还不够,布尔操作结果,我们可以用布尔操作符任意地组合各种布尔表达式。最终的结果还是T或F。,布尔操作结果PQP and QP or Qnot PTTTT,Boolean操作符,a or not b and c 是T还是F(a,b,c皆为T)?操作符的优先级: not and or上述表达式等价于(a or (not b) and c)在记不住优先级的时候,加小括号确定优先级,Boolean操作符a or not b and c 是T还,写Bool表达式的例子: 模拟racquetba
21、ll的结束,两个人比赛,得分记为scoreA,scoreB,没分出胜负之前循环比赛,直到有一人胜出。循环的退出表达式怎么写?先得15分者胜(1)有一位得15分的表示 scoreA = 15 or scoreB = 15(2)满足上述条件的时候停止,否则继续比赛 while not(scoreA = 15 or scoreB = 15): # continue playing,只要满足一个条件就要退出取not的时候就是没退出,继续游戏,写Bool表达式的例子: 模拟racquet,例:模拟racquetball的结束,先得15分或7:0者胜while not(a = 15 or b = 15 o
22、r (a = 7 and b = 0) or (b = 7 and a = 0))必须有一方大于15并且至少要多2分才胜while not(a = 15 and a - b = 2) or (b = 15 and b - a = 2),例:模拟racquetball的结束先得15分或7:0者胜,Boolean代数,对布尔表达式的要求: 能写:将实际问题所涉及的条件表达成布尔表达式 能计算:能对布尔表达式进行演算和推导布尔表达式也能进行代数计算(布尔逻辑),Boolean代数对布尔表达式的要求:,Boolean代数,and has properties similar to *or has pr
23、operties similar to +0 and 1 correspond to false and true, respectively.,Boolean代数and has properties si,Boolean代数,任何数 or true,其结果肯定为true:a or true = trueand和or服从分配律:a or (b and c) = (a or b) and (a or c)a and (b or c) = (a and b) or (a and c)双重否定就是肯定:not(not a) = a摩根定律:not(a or b) = (not a) and (not
24、 b)not(a and b) = (not a) or (not b),Boolean代数任何数 or true,其结果肯定为tru,Python对布尔值和布尔运算的处理很灵活可扩展阅读 中文教材2.4.4 Python中真假的表示与计算,Python对布尔值和布尔运算的处理很灵活,3.2 分支结构3.3 异常处理3.4 循环结构3.5 结构化程序设计,3.2 分支结构,quadratic.py程序会产生很多问题quadratic2-4.py利用决策分支避免了其中的开方为负数时的运行错误在其他很多程序里,也经常用决策分支结构保护程序,避免出现虽然几率很小但还是不可避免会发生的错误。,quad
25、ratic.py程序会产生很多问题,# quadratic.py import mathdef main(): print This program finds the real . a, b, c = input(Please enter. (a, b, c): ) if a=0: #非二次方程 handler1 if type(a)!=int or type(b)!=int or type(c)!=int: #输入类型错误 handler2 discRoot = math.sqrt(b * b - 4 * a * c) if discRoot0: handler3 root1 = (-b
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- python 数据处理 流程 控制 课件
链接地址:https://www.31ppt.com/p-1287614.html