Python例题

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

Python的三种结构顺序、分支、循环某单次测试的流程:测试结果?True处理接下来False:else处理yesno例子:#coding=utf-8__author__='zyt'#if-elif-elsex=0ifx0:print'xlessthan0'elifx==0:print'xequalsto0'else:print'xlargethan0'#whilex=0whilex5:printxx+=1else:x=0print'whilestatementover,nowx=%d'%x#for-inforyinrange(10,19,3):printy,#print默认会每行增加一个换行符,print句后加一个逗号就不会了else:print'\nforstatementover'运行结果:E:\python_workspacepythontest.pyxequalsto001234whilestatementover,nowx=0101316forstatementover12345678910111213141516171819202122232425262728293031323334353637383940try…语句语法“try”“:”suite(“except”[expression[(“as”|“,”)identifier]]“:”suite)+[“else”“:”suite][“finally”“:”suite]Theoptionalelseclauseisexecutedifandwhencontrolflowsofftheendofthetryclause.*try异常?尝试成功的处理*else处理*finally*except捕获的处理yesno例子:#coding=utf-8__author__='zyt'defmy_func(denominator):try:a=1/denominatorprint'trystatement'except:print'exceptstatement'a=999999else:print'elsestatement'finally:print'finallystatement'print'functionend'returnaif__name__=='__main__':printmy_func(1)print'---------'printmy_func(0)运行结果:E:\python_workspacepythontest.pytrystatementelsestatementfinallystatementfunctionend1---------exceptstatementfinallystatementfunctionend999999

1 / 5
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功