Python

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

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

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

资源描述

IntroductiontoComputingUsingPythonImperativeProgrammingPythonProgramsInteractiveInput/OutputOne-WayandTwo-WayifStatementsforLoopsUser-DefinedFunctionsAssignmentsRevisitedandParameterPassingIntroductiontoComputingUsingPythonPythonprogramAPythonprogramisasequenceofPythonstatements•StoredinatextfilecalledaPythonmodule•ExecutedusinganIDEor“fromthecommandline”line1='HelloPythondeveloper...'line2='WelcometotheworldofPython!'print(line1)print(line2)line2='WelcometotheworldofPython!'print(line1)print(line2)line1='HelloPythondeveloper...'hello.py$pythonhello.pyline1='HelloPythondeveloper...'line2='WelcometotheworldofPython!'print(line1)print(line2)line1='HelloPythondeveloper...'line2='WelcometotheworldofPython!'print(line1)print(line2)line1='HelloPythondeveloper...'line2='WelcometotheworldofPython!'print(line1)print(line2)line1='HelloPythondeveloper...'line2='WelcometotheworldofPython!'print(line1)print(line2)$pythonhello.pyHelloPythondeveloper…WelcometotheworldofPython!$pythonhello.pyHelloPythondeveloper…APythonprogramisasequenceofPythonstatements•StoredinatextfilecalledaPythonmodule•ExecutedusinganIDEor“fromthecommandline”IntroductiontoComputingUsingPythonBuilt-infunctionprint()Functionprint()printsitsinputargumenttotheIDLEwindow•Theargumentcanbeanyobject:aninteger,afloat,astring,alist,…oStringsareprintedwithoutquotesand“tobereadbypeople”,ratherthan“tobeinterpretedbyPython”,•The“stringrepresentation”oftheobjectisprintedprint(0)0print(0.0)0.0print('zero')zeroprint([0,1,'two'])[0,1,'two']IntroductiontoComputingUsingPythonBuilt-infunctioninput()first=input('Enteryourfirstname:')last=input('Enteryourlastname:')line1='Hello’+first+''+last+'…'print(line1)print('WelcometotheworldofPython!')name=input('Enteryourname:')Enteryourname:Michaelname'Michael'=========RESTART=============Enteryourfirstname:MichaelEnteryourlastname:JordanHelloMichaelJordan...WelcometotheworldofPython!input.pyname=input('Enteryourname:')Enteryourname:name=input('Enteryourname:')name=input('Enteryourname:')Enteryourname:Michaelname=input('Enteryourname:')Enteryourname:Michaelname'Michael'first=input('Enteryourfirstname:')last=input('Enteryourlastname:')line1='Hello’+first+''+last+'…'print(line1)print('WelcometotheworldofPython!')first=input('Enteryourfirstname:')last=input('Enteryourlastname:')line1='Hello’+first+''+last+'…'print(line1)print('WelcometotheworldofPython!')first=input('Enteryourfirstname:')last=input('Enteryourlastname:')line1='Hello’+first+''+last+'…'print(line1)print('WelcometotheworldofPython!')name=input('Enteryourname:')Enteryourname:Michaelname'Michael'=========RESTART=============Enteryourfirstname:MichaelEnteryourlastname:name=input('Enteryourname:')Enteryourname:Michaelname'Michael'=========RESTART=============Enteryourfirstname:name=input('Enteryourname:')Enteryourname:MichaelFunctioninput()requestsandreadsinputfromtheuserinteractively•It’s(optional)inputargumentistherequestmessage•TypicallyusedontherightsideofanassignmentstatementWhenexecuted:Whenexecuted:1.TheinputrequestmessageisprintedWhenexecuted:1.Theinputrequestmessageisprinted2.Theuserenterstheinput3.ThestringtypedbytheuserisassignedtothevariableontheleftsideoftheassignmentstatementWhenexecuted:1.Theinputrequestmessageisprinted2.TheuserenterstheinputIntroductiontoComputingUsingPythonBuilt-infunctioneval()Functioninput()evaluatesanythingtheuserentersasastringWhatifwewanttheusertointeractivelyenternon-stringinputsuchasanumber?•Solution1:Usetypeconversion•Solution2:Usefunctioneval()oTakesastringasinputandevaluatesitasaPythonexpressionage=input('Enteryourage:')Enteryourage:18age'18'int(age)18eval('18')18eval('age')'18'eval('[2,3+5]')[2,8]eval('x')Traceback(mostrecentcalllast):Filepyshell#14,line1,inmoduleeval('x')Filestring,line1,inmoduleNameError:name'x'isnotdefinedage=input('Enteryourage:')Enteryourage:18age'18'int(age)18age=input('Enteryourage:')Enteryourage:18age'18'Functioninput()evaluatesanythingtheuserentersasastringWhatifwewanttheusertointeractivelyenternon-stringinputsuchasanumber?Functioninput()evaluatesanythingtheuserentersasastringFunctioninput()evaluatesanythingtheuserentersasastringWhatifwewanttheusertointeractivelyenternon-stringinputsuchasanumber?•Solution1:UsetypeconversionIntroductiontoComputingUsingPythonExerciseWriteaprogramthat:1.Requeststheuser’sname2.Requeststheuser’sage3.Computestheuser’sageoneyearfromnowandprintsthemessageshownIntroductiontoComputingUsingPythonExerciseWriteaprogramthat:1.Requeststheuser’sname2.Requeststheuser’sage3.PrintsamessagesayingwhethertheuseriseligibletovoteornotNeedawaytoexecuteaPythonstatementifaconditionistrueIntroductiontoComputingUsingPythoniftemp86:print('Itishot!')print('Besuretodrinkliquids.')print('Goodbye.')Falsetemp86:print('Itishot!')print('Besuretodrinkliquids.')Print('Goddbye.')TrueOne-wayifstatementiftemp86:print('Itishot!')print('Besuretodrinkliquids.')print('Goodbye.')iftemp86:print(

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

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

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

×
保存成功