#练习1:从键盘输入两个数,并比较其大小,直到输入e/E退出程序
while 1:
input_number=raw_inut("please input two numbers,enter "e" or "E" to exit:")
try:
if input_number.isdigit():
lst=[]
lst_new=lst.append(input_number)
if lst_new[0]>lst_new[1]:
print "%s大于%s",%(lst_new[0],lst_new[1])
elif st_new[0]<lst_new[1]:
print "%s小于%s",%(lst_new[0],lst_new[1])
else:
print "%s等于%s",%(lst_new[0],lst_new[1])
else:
raise TypeError
if input_number=="e" or input_number=="E":
break
except TypeError:
print "input is not a number,try again" #练习2:将列表元素交替地作为建和值来创建字典
lst=[1:"a",2:"b",3:"c"]
dicts={}
for i in range(0,len(lst),2):
dicts[lst[i]]=lst[i+1]
print dicts #练习3:分别输出字符串中奇数坐标和偶数坐标的字符
num=["a","b","c","d",1,2,3,4]
oddindex_number=[]
evenindex_number=[]
for i in range(0,len(str)):
if i%2!=0:
oddindex_number.append(num[i])
else:
evenindex_number.append(num[i])
print "奇数坐标的字符为:",oddindex_number
print "偶数坐标的字符为:",evenindex_number #练习4:将一个字典的 key和value 互换
dicts={1:4,2:5,3:6}
new_dicts={}
for k,v in dicts.items():
if isinstance(v,(str,float,tuple,int,complex)):
#print k,v
new_dicts[v]=k
else:
print u"value %s不可哈希" %v
print new_dicts #这是个递归的一个例子
def fib(num):
result=[0,1]
for i in range(num-2):
result.append(result[-2]+result[-1])
return result ptint fib(10)
04-15 11:29