Python语言实例开发代码分析(python,编程语言)

时间:2024-04-29 21:25:01 作者 : 石家庄SEO 分类 : 编程语言
  • TAG :

    Python%E8%AF%AD%E8%A8%80%E5%AE%9E%E4%BE%8B%E5%BC%80%E5%8F%91%E4%BB%A3%E7%A0%81%E5%88%86%E6%9E%90

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语言实例开发代码分析的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:Ruby语言实例代码分析下一篇:

4 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18