Input, print and numbers - Learn Python 3 - Snakify
文章推薦指數: 80 %
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
延伸文章資訊
- 1How can I read inputs as numbers? - python - Stack Overflow
For this kind of input manipulation, you can either num1, num2 = map(int, input().split()) if you...
- 2Input, print and numbers - Learn Python 3 - Snakify
To cast (convert) the string of digits into an integer number, we can use the function int() . Fo...
- 3技巧3:與電腦溝通的方法— input() 及print()(字幕、襯樂、練習)
昨天教了一堆變數的資料型態,分別有整數(int)、浮點數(float)、字串(str)、布林值(bool),如果還是搞不清楚記得再回去看每個人都該學的30個Python技巧|技巧2:Python...
- 4Input an integer (n) and computes the value of n+nn+nnn
Python Exercises, Practice and Solution: Write a Python program that accepts an integer (n) and c...
- 5Python Input - CodesDope
We just saw that input is always read as a string. So what if we want to read an integer? We can ...