获取字典中任意的键-值对
>>> x={'a':1,'b':2}>>> key,value=x.popitem()>>> key,value('a', 1)>>> del x[key]Traceback (most recent call last): File "", line 1, in del x[key]KeyError: 'a'>>> x{'b': 2}>>> x[key]=value>>> x{'a': 1, 'b': 2}>>> del x[key]
登录后复制
增量赋值
>>> x=2>>> x+=1>>> x*=2>>> x>>> fnord='foo'>>> fnord+='bar'>>> fnord*=2>>> fnord'foobarfoobar'
登录后复制
条件执行if语句
>>> name=raw_input('?')?Yq Z>>> if name.endswith('Z'): print 'Hello,Mr.Z'Hello,Mr.Z
登录后复制
else子句
>>> name=raw_input('what is your name?')what is your name?Yq Z>>> if name.endswith('Z'): print 'Hello,Mr.Z'else: print 'Hello,stranger' Hello,Mr.Z
登录后复制
elif子句
立即学习“Python免费学习笔记(深入)”;
>>> num=input('Enter a number: ')Enter a number: 5>>> if num>0: print 'The number is position'elif num条件嵌套语句
>>> name=raw_input('What is your name?')What is your name?Yq Z>>> if name.endswith('Yq'): if name.startswith('Z'): print 'Hello,Yq Z' elif name.startswith('K'): print 'Hello,Zyq' else: print 'Hello,Yq'else: print 'Hello,stranger' Hello,stranger登录后复制
>>> number=input('Enter a number between 1 and 10:')Enter a number between 1 and 10:6>>> if number=1: print 'Great!'else: print 'Wrong!' Great!登录后复制
>>> age=10>>> assert 0>> age=-1>>> assert 0", line 1, in assert 0while循环
>>> x=1>>> while x>>> while not name: name=raw_input('Please enter your name:') print 'Hello,%s !' % name Please enter your name:zyqHello,zyq !登录后复制
for循环
>>> words=['this','is','an','ex','parrot']>>> for word in words: print word thisisanexparrot>>> range(0,10)[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>>> for i in range(1,8): print i246登录后复制
字典循环(迭代)
>>> d={'x':1,'y':2,'z':3}>>> for key in d: print key,'corresponds to',d[key] y corresponds to 2x corresponds to 1z corresponds to 3登录后复制
并行迭代
>>> names=['Anne','Beth','George','Damon'] >>> ages=[12,19,18,20]>>> for i in range(len(names)): print names[i],'is',ages[i],'years old' Anne is 12 years oldBeth is 19 years oldGeorge is 18 years oldDamon is 20 years old登录后复制
>>> zip(names,ages)[('Anne', 12), ('Beth', 19), ('George', 18), ('Damon', 20)]>>> for name,age in zip(names,ages): print name,'is',age,'years old' Anne is 12 years oldBeth is 19 years oldGeorge is 18 years oldDamon is 20 years old>>> zip(range(5),xrange(100))[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)]登录后复制
编号迭代
>>> d[1, 2, 4, 4]>>> for x in d: if x==4: d[d.index(x)]=6 >>> d[1, 2, 6, 6]>>> S=['skj','kiu','olm','piy']>>> index=0>>> for s1 in S: if 'k' in s1: S[index]='HH' index+=1 >>> S['HH', 'HH', 'olm', 'piy']>>> for index,s2 in enumerate(S): #enumerate函数提供索引-值对 if 'H' in s2: S[index]='DF' >>> S['DF', 'DF', 'olm', 'piy']登录后复制
翻转、排序迭代
>>> sorted([4,3,6,8,3])[3, 3, 4, 6, 8]>>> sorted('Hello,world!')['!', ',', 'H', 'd', 'e', 'l', 'l', 'l', 'o', 'o', 'r', 'w']>>> list(reversed('Hello,world!'))['!', 'd', 'l', 'r', 'o', 'w', ',', 'o', 'l', 'l', 'e', 'H']>>> ''.join(reversed('Hello,world!'))'!dlrow,olleH'登录后复制
break跳出循环
>>> for n in range(99,0,-1): m=sqrt(n) if m==int(m): print n break登录后复制
while True/break
>>> while True: word=raw_input('Please enter a word:') if not word:break print 'The word was '+word Please enter a word:fThe word was fPlease enter a word:登录后复制
循环中的else语句
>>> for n in range(99,81,-1): m=sqrt(n) if m==int(m): print m breakelse: print 'h' h登录后复制
列表推导式-轻量级循环
>>> [x*x for x in range(10)][0, 1, 4, 9, 16, 25, 36, 49, 64, 81]>>> [x*x for x in range(10) if x%3==0][0, 9, 36, 81]>>> [(x,y) for x in range(3) for y in range (3)][(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]>>> result=[]>>> for x in range(3): for y in range(3): result.append((x,y))>>> result [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]>>> girls=['Alice','Bernice','Clarice']>>> boys=['Chris','Arnold','Bob']>>> [b+'+'+g for b in boys for g in girls if b[0]==g[0]]['Chris+Clarice', 'Arnold+Alice', 'Bob+Bernice']登录后复制
pass
>>> if name=='Nsds': print 'Welcome!'elif name=='UK': #还没完 passelif name=='Bill': print 'Access Denied'else: print 'Nobody!'登录后复制
del x和y同时指向一个列表,但是删除x并不会影响y。删除的只是名称,不是列表本身(值)
>>> x=['Hello','world']>>> y=x>>> y[1]='Python'>>> x['Hello', 'Python']>>> del x>>> y['Hello', 'Python']登录后复制
exec
>>> exec "print 'Hello,world!'"Hello,world!>>> from math import sqrt>>> exec "sqrt=1">>> sqrt(4)Traceback (most recent call last): File "", line 1, in sqrt(4)TypeError: 'int' object is not callable#增加一个字典,起到命名空间的作用>>> from math import sqrt>>> scope={}>>> exec 'sqrt=1' in scope>>> sqrt(4)2.0>>> scope['sqrt']登录后复制
注意:命名空间,称作作用域。可以把它想象成保存变量的地方,类似于不可见的字典。执行 x=1这类赋值语句时,就将键x和值1放在当前的命名空间内,这个命名空间一般来说都是全局命名空间。
>>> len(scope)2>>> scope.keys()['__builtins__', 'sqrt']登录后复制
eval 求值
>>> scope={}>>> scope['x']=2>>> scope['y']=3>>> eval('x*y',scope)>>> scope={}>>> exec 'x=2' in scope>>> eval('x*x',scope)登录后复制
以上就是python中条件、循环等介绍说明的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2277629.html