python之路-------python中的控制流语句
2015-08-06 18:24
701 查看
编写函数对象,测试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="weojslkjdfvcnzd"
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()
#!/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="weojslkjdfvcnzd"
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()
相关文章推荐
- python中ConfigParse模块的用法
- Python 简单爬虫抓取糗事百科
- Python处理中文字符
- python创建多进程
- python 入门神图
- python练习题(四)
- python 的日志logging模块学习
- 机器学习实战(python)一 k临近算法。
- 【Python】生成器和迭代器
- python 连接超时判断subprocess
- 用Python抓取全站中的404错误
- Python 2 与Python 3的区别
- Windows 下针对python脚本做一个简单的进程保护
- python独立脚本调用django settings
- ArcGIS制作Python脚本时字符编码问题
- python处理HTML转义字符
- Python 时间格式化
- python
- python中__new__ 的作用
- python中**dict