Python语言实例开发代码分析
导读:本文共9329.5字符,通常情况下阅读需要31分钟。同时您也可以点击右侧朗读,来听本文内容。按键盘←(左) →(右) 方向键可以翻页。
摘要:1<2<3#=>True2<3<2#=>FalseStringsarecreatedwith"or'使用"或'来创建字符串"Thisisastring."'Thisisalsoastring.'Stringscanbeaddedtoo!字符串也可以相加!"Hello... ...
目录
(为您整理了一些要点),点击可以直达。- (1):2<3<2#=>False
- (2):"Thisisastring."
- (3):'Thisisalsoastring.'
- (5):"Thisisastring"[0
- (10):"etc"isNone#=>
- (26):1inli#=>True
- (34):2intup#=>True
- (43):"one"infilled_dic
- (44):1infilled_dict#=>False
- (60):2infilled_set#=>True
- (61):10infilled_set#=>False
- (62):###########################
- (63):###########################
- (71):"""
- (79):"""
- (82):"""
- (89):0
- (90):1
- (91):2
- (92):3
- (93):"""
- (96):"""
- (101):0
- (102):1
- (103):2
- (104):3
- (105):"""
- (114):###########################
- (115):###########################
- (130):"""
- (132):(1,2)
- (134):"""
- (146):(lambdax:x>2)(3)#=>Tr
- (151):###########################
- (152):###########################
- (174):###########################
- (175):###########################
1<2<3#=>True
2<3<2#=>False
"Thisisastring."
'Thisisalsoastring.'
"Hello"+"world!"#=>"Helloworld!"
"Thisisastring"[0]#=>'T'
"%scanbe%s"%("strings","interpolated")
"{0}canbe{1}".format("strings","formatted")
"{name}wantstoeat{food}".format(name="Bob",food="lasagna")
None#=>None
"etc"isNone#=>False
NoneisNone#=>True
li.append(2)#liisnow[1,2]
li.append(4)#liisnow[1,2,4]
li.append(3)#liisnow[1,2,4,3]
li.pop()#=>3andliisnow[1,2,4]
li.append(3)#liisnow[1,2,4,3]again.
li[0]#=>1
li[-1]#=>3
li[4]#RaisesanIndexError
li[1:3]#=>[2,4]
li[2:]#=>[4,3]
li[:3]#=>[1,2,4]
delli[2]#liisnow[1,2,3]
li+other_li#=>[1,2,3,4,5,6]-Note:liandother_liisleftalone
li.extend(other_li)#Nowliis[1,2,3,4,5,6]
1inli#=>True
len(li)#=>6
tup=(1,2,3)
tup[0]#=>1
tup[0]=3#RaisesaTypeError
len(tup)#=>3
tup+(4,5,6)#=>(1,2,3,4,5,6)
tup[:2]#=>(1,2)
2intup#=>True
a,b,c=(1,2,3)#aisnow1,bisnow2andcisnow3
d,e,f=4,5,6
e,d=d,e#disnow5andeisnow4
empty_dict={}
filled_dict={"one":1,"two":2,"three":3}
filled_dict["one"]#=>1
filled_dict.keys()#=>["three","two","one"]
filled_dict.values()#=>[3,2,1]
"one"infilled_dict#=>True
1infilled_dict#=>False
filled_dict["four"]#KeyError
filled_dict.get("one")#=>1
filled_dict.get("four")#=>None
filled_dict.get("one",4)#=>1
filled_dict.get("four",4)#=>4
filled_dict.setdefault("five",5)#filled_dict["five"]issetto5
filled_dict.setdefault("five",6)#filled_dict["five"]isstill5
empty_set=set()
some_set=set([1,2,2,3,4])#some_setisnowset([1,2,3,4])
filled_set={1,2,2,3,4}#=>{1,2,3,4}
filled_set.add(5)#filled_setisnow{1,2,3,4,5}
other_set={3,4,5,6}
filled_set&other_set#=>{3,4,5}
filled_set|other_set#=>{1,2,3,4,5,6}
{1,2,3,4}-{2,3,5}#=>{1,4}
2infilled_set#=>True
10infilled_set#=>False
####################################################
####################################################
some_var=5
ifsome_var>10:
print"some_varistotallybiggerthan10."
elifsome_var<10:#Thiselifclauseisoptional.
print"some_varissmallerthan10."
else:#Thisisoptionaltoo.
print"some_varisindeed10."
"""
Forloopsiterateoverlists
for循环可以遍历列表
prints:
如果要打印出:
dogisamammal
catisamammal
mouseisamammal
"""
foranimalin["dog","cat","mouse"]:
print"%sisamammal"%animal
"""
range(number)
returnsalistofnumbers
fromzerotothegivennumber
range(数字)
会返回一个数字列表,
这个列表将包含从零到给定的数字。
prints:
如果要打印出:
0
1
2
3
"""
foriinrange(4):
printi
"""
Whileloopsgountilaconditionisnolongermet.
while循环会一直继续,直到条件不再满足。
prints:
如果要打印出:
0
1
2
3
"""
x=0
whilex<4:
printx
x+=1#Shorthandforx=x+1
try:
raiseIndexError("Thisisanindexerror")
exceptIndexErrorase:
pass#Passisjustano-op.Usuallyyouwoulddorecoveryhere.
####################################################
####################################################
defadd(x,y):
print"xis%sandyis%s"%(x,y)
returnx+y#Returnvalueswithareturnstatement
add(5,6)#=>printsout"xis5andyis6"andreturns11
add(y=6,x=5)#Keywordargumentscanarriveinanyorder.
defvarargs(*args):
returnargs
varargs(1,2,3)#=>(1,2,3)
defkeyword_args(**kwargs):
returnkwargs
keyword_args(big="foot",loch="ness")#=>{"big":"foot","loch":"ness"}
defall_the_args(*args,**kwargs):
printargs
printkwargs
"""
all_the_args(1,2,a=3,b=4)prints:
(1,2)
{"a":3,"b":4}
"""
args=(1,2,3,4)
kwargs={"a":3,"b":4}
all_the_args(*args)#equivalenttofoo(1,2,3,4)
all_the_args(**kwargs)#equivalenttofoo(a=3,b=4)
all_the_args(*args,**kwargs)#equivalenttofoo(1,2,3,4,a=3,b=4)
defcreate_adder(x):
defadder(y):
returnx+y
returnadder
add_10=create_adder(10)
add_10(3)#=>13
(lambdax:x>2)(3)#=>True
map(add_10,[1,2,3])#=>[11,12,13]
filter(lambdax:x>5,[3,4,5,6,7])#=>[6,7]
[add_10(i)foriin[1,2,3]]#=>[11,12,13]
[xforxin[3,4,5,6,7]ifx>5]#=>[6,7]
####################################################
####################################################
classHuman(object):
species="H.sapiens"
definit(self,name):
self.name=name
defsay(self,msg):
return"%s:%s"%(self.name,msg)
@classmethod
defget_species(cls):
returncls.species
@staticmethod
defgrunt():
return"grunt"
i=Human(name="Ian")
printi.say("hi")#printsout"Ian:hi"
j=Human("Joel")
printj.say("hello")#printsout"Joel:hello"
i.get_species()#=>"H.sapiens"
Human.species="H.neanderthalensis"
i.get_species()#=>"H.neanderthalensis"
j.get_species()#=>"H.neanderthalensis"
Human.grunt()#=>"grunt"
####################################################
####################################################
importmath
printmath.sqrt(16)#=>4
frommathimportceil,floor
printceil(3.7)#=>4.0
printfloor(3.7)#=>3.0
frommathimport*
importmathasm
math.sqrt(16)==m.sqrt(16)#=>True
importmath
dir(math)
以上就是关于“Python语言实例开发代码分析”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。
Python语言实例开发代码分析的详细内容,希望对您有所帮助,信息来源于网络。