资源描述:
《single line comments start with a number symbol》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、#Singlelinecommentsstartwithanumbersymbol."""Multilinestringscanbewrittenusingthree"s,andareoftenusedascomments"""######################################################1.PrimitiveDatatypesandOperators#####################################################Youhavenumbers3#=>3#Mathiswhatyouwou
2、ldexpect1+1#=>28-1#=>710*2#=>2035/5#=>7.0#Resultofintegerdivisiontruncateddownbothforpositiveandnegative.5//3#=>15.0//3.0#=>1.0#worksonfloatstoo-5//3#=>-2-5.0//3.0#=>-2.0#Theresultofdivisionisalwaysafloat10.0/3#=>3.3333333333333335#Modulooperation7%3#=>1#Exponentiation(x**y,xtotheythpower
3、)2**4#=>16#Enforceprecedencewithparentheses(1+3)*2#=>8#Booleanvaluesareprimitives(Note:thecapitalization)TrueFalse#negatewithnotnotTrue#=>FalsenotFalse#=>True#BooleanOperators#Note"and"and"or"arecase-sensitiveTrueandFalse#=>FalseFalseorTrue#=>True#NoteusingBooloperatorswithints#Falseis0an
4、dTrueis1#Don'tmixupwithbool(ints)andbitwiseand/or(&,
5、)0and2#=>0-5or0#=>-50==False#=>True2==True#=>False1==True#=>True-5!=False!=True#=>True#Equalityis==1==1#=>True2==1#=>False#Inequalityis!=1!=1#=>False2!=1#=>True#Morecomparisons1<10#=>True1>10#=>False2<=2#=>True2>=2#=>True#Comparisonscan
6、bechained!1<2<3#=>True2<3<2#=>False#(isvs.==)ischecksiftwovariablesrefertothesameobject,but==checks#iftheobjectspointedtohavethesamevalues.a=[1,2,3,4]#Pointaatanewlist,[1,2,3,4]b=a#Pointbatwhataispointingtobisa#=>True,aandbrefertothesameobjectb==a#=>True,a'sandb'sobjectsareequalb=[1,2,3,4
7、]#Pointbatanewlist,[1,2,3,4]bisa#=>False,aandbdonotrefertothesameobjectb==a#=>True,a'sandb'sobjectsareequal#Stringsarecreatedwith"or'"Thisisastring."'Thisisalsoastring.'#Stringscanbeaddedtoo!Buttrynottodothis."Hello"+"world!"#=>"Helloworld!"#Stringscanbeaddedwithoutusing'+'"Hello""world!"
8、#=>"Helloworld!"#Astringcanbetreatedlikealistofcharacters"Thisisastring"[0]#=>'T'#Youcanfindthelengthofastringlen("Thisisastring")#=>16#.formatcanbeusedtoformatstrings,likethis:"{}canbe{}".format("Strings","interpolated")#=>"Stringscanbeinterpolated"#Youcanrepeatthe