Py4Inf-03-ConditionalPy4Inf-03-Conditional
-
资源ID:23993782
资源大小:757.60KB
全文页数:32页
- 资源格式: PDF
下载积分:5金贝
快捷下载
账号登录下载
微信登录下载
微信扫一扫登录
1、金锄头文库是“C2C”交易模式,即卖家上传的文档直接由买家下载,本站只是中间服务平台,本站所有文档下载所得的收益全部归上传人(卖家)所有,作为网络服务商,若您的权利被侵害请及时联系右侧客服;
2、如你看到网页展示的文档有jinchutou.com水印,是因预览和防盗链等技术需要对部份页面进行转换压缩成图而已,我们并不对上传的文档进行任何编辑或修改,文档下载后都不会有jinchutou.com水印标识,下载后原文更清晰;
3、所有的PPT和DOC文档都被视为“模板”,允许上传人保留章节、目录结构的情况下删减部份的内容;下载前须认真查看,确认无误后再购买;
4、文档大部份都是可以预览的,金锄头文库作为内容存储提供商,无法对各卖家所售文档的真实性、完整性、准确性以及专业性等问题提供审核和保证,请慎重购买;
5、文档的总页数、文档格式和文档大小以系统显示为准(内容中显示的页数不一定正确),网站客服只以系统显示的页数、文件格式、文档大小作为仲裁依据;
6、如果您还有什么不清楚的或需要我们协助,可以点击右侧栏的客服。
|
下载须知 | 常见问题汇总
|
Py4Inf-03-ConditionalPy4Inf-03-Conditional
Conditional ExecutionChapter 3Python for Informatics: Exploring Informationwww.py4inf.comUnless otherwise noted, the content of this course material is licensed under a Creative Commons Attribution 3.0 License.http:/creativecommons.org/licenses/by/3.0/.Copyright 2010- Charles R. SeveranceConditional StepsOutput:SmallerFinisProgram:x = 5if x 20: print 'Bigger'print 'Finis'x = 5X 20 ?print 'Bigger'print 'Finis'YesYesComparison OperatorsBoolean expressions ask a question and produce a Yes or No result which we use to control program flowBoolean expressions using comparison operators evaluate to - True / False - Yes / NoComparison operators look at variables but do not change the variableshttp:/en.wikipedia.org/wiki/George_BooleRemember: “=” is used for assignment.Python Meaning= Greater than or Equal> Greater than!= Not equalComparison Operatorsx = 5if x = 5 : print 'Equals 5'if x > 4 :print 'Greater than 4'if x >= 5 :print 'Greater than or Equal 5'if x Preferences -> Language Menu/Tab SettingsTextWrangler: TextWrangler -> Preferences -> Editor DefaultsPython cares a *lot* about how far line is indented. If you mix tabs and spaces, you may get “indentation errors” even if everything looks finePlease do this now while you are thinking about it so we can all stay sane.This will save you much unnecessary pain.x = 5if x > 2 :print 'Bigger than 2'print 'Still bigger'print 'Done with 2'for i in range(5) :print iif i > 2 : print 'Bigger than 2'print 'Done with i', ix = 5if x > 2 :# commentsprint 'Bigger than 2'# dont matterprint 'Still bigger'# but can confuse youprint 'Done with 2'# if you dont line # them upincrease / maintain after if or fordecrease to indicate end of blockblank lines and comment lines ignoredMental begin/end squaresx = 5if x > 2 :print 'Bigger than 2'print 'Still bigger'print 'Done with 2'for i in range(5) :print iif i > 2 : print 'Bigger than 2'print 'Done with i', ix = 5if x > 2 :# commentsprint 'Bigger than 2'# dont matterprint 'Still bigger'# but can confuse youprint 'Done with 2'# if you dont line # them upx > 1print 'More than one'x 1 :print 'More than one'if x 1print 'More than one'x 1 :print 'More than one'if x 1print 'More than one'x 1 :print 'More than one'if x 2print 'Bigger'yesnoX = 4print 'Not bigger'print 'All Done'Two-way using else :x = 4if x > 2 :print 'Bigger'else :print 'Smaller'print 'All done'x > 2print 'Bigger'yesnoX = 4print 'Smaller'print 'All Done'Two-way using else :x = 4if x > 2 :print 'Bigger'else :print 'Smaller'print 'All done'x > 2print 'Bigger'yesnoX = 4print 'Smaller'print 'All Done'Multi-wayif x = 2 : print 'Two or more'else :print 'Something else'Which will never print?The try / except StructureYou surround a dangerous section of code with try and except.If the code in the try works - the except is skippedIf the code in the try fails - it jumps to the except section$ cat notry.py astr = 'Hello Bob'istr = int(astr)print 'First', istrastr = '123'istr = int(astr)print 'Second', istr$ python notry.py Traceback (most recent call last):File "notry.py", line 2, in istr = int(astr)ValueError: invalid literal for int() with base 10: 'Hello Bob'The program stops hereAllDone SoftwareInputDevicesCentralProcessingUnitMainMemoryOutputDevicesSecondaryMemoryGenericComputer$ cat tryexcept.py astr = 'Hello Bob'try:istr = int(astr)except:istr = -1print 'First', istrastr = '123'try:istr = int(astr)except:istr = -1print 'Second', istr$ python tryexcept.py First -1Second 123When the first conversion fails - it just drops into the except: clause and the program continues.When the second conversion succeeds - it just skips the except: clause and the program continues.try / exceptastr = 'Bob'astr = 'Bob'try:print 'Hello'istr = int(astr)print 'There'except:istr = -1print 'Done', istrprint 'Hello'print 'There'istr = int(astr)print 'Done', istristr = -1Safety netSample try / except$ python trynum.py Enter a number:42Nice work$ python trynum.pyEnter a number:fourtytwoNot a number$rawstr = raw_input('Enter a number:')try: ival = int(rawstr)except: ival = -1if ival > 0 : print 'Nice work'else: print 'Not a number'ExerciseRewrite your pay computation to give the employee 1.5 times the hourly rate for hours worked above 40 hours.Enter Hours: 45Enter Rate: 10 Pay: 475.0475 = 40 * 10 + 5 * 15ExerciseRewrite your pay program using try and except so that your program handles non-numeric input gracefully.Enter Hours: 20 Enter Rate: nine Error, please enter numeric inputEnter Hours: forty Error, please enter numeric inputSummaryComparison operators = = > < !=Logical operators: and or notIndentationOne Way DecisionsTwo way Decisions if : and else :Nested DecisionsMultiway decisions using elifTry / Except to compensate for errorsShort circuit evaluations