python学习-------python中的控制流语句

2015-11-21 02:09:20 · 作者: · 浏览: 7

编写函数对象,测试for语句,使用range函数打印一个字符串偶数项的字母,使用zip函数进行并行迭代操作,使用zip将两个列表构造成字典

#!/usr/bin/pythone

#encoding:utf8 ##避免中文乱码报错:SyntaxError: Non-ASCII character '\xe6' in file

def testwhile(var1):
cnt=1;
while cnt<=var1:
print(cnt)
cnt=cnt+1
else:
print(cnt,"is done")
def testfor():
list1=[(1,'a'),(3,'b'),(4,'c')]
dict1=dict(list1)
for key in dict1:
print(key,'=>',dict1[key])
else:
print("testfor dict is done")
#list2=list(dict.items())
for (a,b) in list(dict.items(dict1)):
print(a,"=>",b)
else:
print("testfor items is done")
def testrange():
for cnt in range(-3,2):
print(cnt,str(cnt)+"aaa")
else:
print("testrange is done")
##打印一个字符串偶数项的字母,使用len()函数
strs="weojslkjdf
vc
nzd" def print2char(): print(range(1,len(strs),2)) newstr='' for pos in range(1,len(strs),2): print(pos,"->",strs[pos]) newstr += strs[pos] else: print("new word is ",newstr) print("print2char is done") ##使用zip进行并行迭代操作,使用zip将两个列表构造成字典 key=[1,3,5,7] value=['a1','a2','a3','a4'] def double_iteration(): print(zip(key,value)) print("double_iteration is done") """每个模块都有个名为__name__的内置属性,python会自动设置该属性;如果文件是以顶层程序文件执行,在启动时,__name__就会设置为字符串__main__""" if __name__=='__main__': testwhile(10) testfor() testrange() print2char() double_iteration()

?