Input, print and numbers - Learn Python 3 - Snakify

文章推薦指數: 80 %
投票人數:10人

To cast (convert) the string of digits into an integer number, we can use the function int() . For example, int('23') gives an int object with value 23 . Given ... Togglenavigation                Forteachers          Needatutor? English español Português Русский 日本語 한국어 français Deutsch polski العربيّة Türkçe 1.Input,printandnumbers Sumofthreenumbers HiJohn Square Areaofright-angledtriangle Hello,Harry! Applesharing Previousandnext Twotimestamps Schooldesks 2.Integerandfloatnumbers 3.Conditions:if,then,else 4.Forloopwithrange 5.Strings 6.Whileloop 7.Lists 8.Functionsandrecursion 9.Two-dimensionallists(arrays) 10.Sets 11.Dictionaries 12.JavaScript 13.HTML5andCSS 14.ResponsiveDesignwithBootstrap 15.jQuery Adplace Lesson1Input,printandnumbers Theory Steps Problems 1.HowtoreadandwriteinPython Everyprogramiseventuallyadataprocessor,soweshouldknowhowtoinputandoutputdatawithinit.Thereexistsafunction,print(),tooutputdatafromanyPythonprogram. Touseit,passacommaseparatedlistofarguments thatyouwanttoprinttotheprint()function. Let'sseeanexample.Press"run"andthen"next"toseehowtheprogram isbeingexecutedlinebyline: None print(5+10) print(3*7,(17-2)*8) print(2**16)#twostarsareusedforexponentiation(2tothepowerof16) print(37/3)#singleforwardslashisadivision print(37//3)#doubleforwardslashisanintegerdivision #itreturnsonlythequotientofthedivision(i.e.noremainder) print(37%3)#percentsignisamodulusoperator #itgivestheremainderoftheleftvaluedividedbytherightvalue Toinputdataintoaprogram,weuseinput().Thisfunctionreadsasinglelineoftext,asaString. Here'saprogramthatreadstheuser'snameandgreetsthem: John print('Whatisyourname?') name=input()#readasinglelineandstoreitinthevariable"name" print('Hi'+name+'!') AdvertisingbyGoogle,maybebasedonyourinterests 2.Sumofnumbersandstrings Let'strytowriteaprogramthatinputstwonumbersandprintstheirsum.Wereadthetwonumbers andstoretheminthevariablesaandbusingtheassignmentoperator=. Ontheleftsideofanassignmentoperatorweputthenameofthevariable.Thenamecouldbeastringoflatincharacters(A-Z,a-z,0-9,_) butmuststartwithaletterintherangeA-Zora-z. OntherightsideofanassignmentoperatorweputanyexpressionthatPythoncanevaluate. Thenamestartspointingtotheresultoftheevaluation. Readthisexample,runitandlookattheoutput: 5 7 a=input() b=input() s=a+b print(s) Afterrunningtheexamplewecanseethatitprints57.Asweweretaughtinschool, 5+7gives12.So,theprogramiswrong,andit'simportanttounderstandwhy. Thethingis,inthethirdlines=a+bPythonhas"summed"twostrings,ratherthantwonumbers. ThesumoftwostringsinPythonworksasfollows:theyarejustgluedoneafteranother.It'salsosometimes called"stringconcatenation". Doyouseeinthevariableinspector,ontherighthandside,thatthevaluesboundtovariablesa and b arewrappedinquotes?Thatmeansthatthevaluestherearestring,notnumbers.Stringsandnumbers arerepresentedinPythondifferently. AllthevaluesinPythonarecalled"objects".Everyobjecthasacertaintype.Thenumber2correspondstoanobject"number2"oftype"int" (i.e.,anintegernumber).Thestring'hello'correspondstoanobject"string'hello'"oftype"str". Everyfloating-pointnumberisrepresentedasanobjectoftype"float".Thetype ofanobjectspecifieswhatkindofoperationsmaybeappliedtoit. Forinstance,ifthetwovariables"first"and"second"arepointingtotheobjectsoftypeint,Pythoncanmultiplythem.However,iftheyarepointingtotheobjectsoftypestr,Pythoncan'tdothat: None first=5 second=7 print(first*second) #youcanusesingleordoublequotestodefineastring first='5' second="7" print(first*second) Tocast(convert)thestringofdigitsintoanintegernumber,wecanusethefunction int().Forexample, int('23')givesanintobject withvalue23. Giventheinformationabove,wecannowfixtheincorrectoutputandoutputthesumofthetwonumberscorrectly: 5 7 a=int(input()) b=int(input()) s=a+b print(s) AdvertisingbyGoogle,maybebasedonyourinterests Playagameaboutdifferentimagesofthesamegraph



請為這篇文章評分?