python开发之for循环操作实例详解


Posted in Python onNovember 12, 2015

本文实例讲述了python开发之for循环操作。分享给大家供大家参考,具体如下:

下面是我做的一些学习记录供大家参考:

#基本的for循环语句
test_list = [2,"Jone",3,6,7,'hongten','hanyuan','good',"Tom"]
#打印列表的长度
print(len(test_list))
#遍历列表
for i in test_list:
  print(i)
test_str = "hello,i'm hongten"
print('打印字符串:' + test_str)
#遍历一个字符串
print('遍历一个字符串')
for i in test_str:
  print(i)
test_tuple = [("a",1),("b",2),("c",3),("d",4)]
print(test_tuple)
#遍历一个元组
print('遍历一个元组')
for (i,j) in test_tuple:
  print(i,j)
test_dict = {'name':'hongten','age':'20','gender':'M','sports':'足球,乒乓球,游泳'}
#字典迭代器
for key in test_dict:
  print(key + ':' + test_dict[key])
L1 = [1,3,5,7]
L2 = [2,4,6,8]
#使用zip将两个列表合并
print(zip(L1,L2))
for (i,j) in zip(L1,L2):
  print(i,j)
print('#######################################################')
L3 = L2[:]
L3.remove(8)
print('L1,L3列表为:')
print(L1)
print(L3)
for (i,j) in zip(L1,L3):
  print(i,j)
#可以看出来当长度不一的时候,多余的被忽略
test_keys = ['name','age','gender','weight','hight']
test_values = ['Hongten','20','M','55','170']
#使用zip来构造一个字典
print('字典中的keys:')
print(test_keys)
print('字典中的key对应的value:')
print(test_values)
print('构造字典后')
test_dic = dict(zip(test_keys,test_values))
for key in test_dic:
  print( key + ':' + test_dic[key])

运行效果:

Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
9
2
Jone
3
6
7
hongten
hanyuan
good
Tom
打印字符串:hello,i'm hongten
遍历一个字符串
h
e
l
l
o
,
i
'
m
 
h
o
n
g
t
e
n
[('a', 1), ('b', 2), ('c', 3), ('d', 4)]
遍历一个元组
('a', 1)
('b', 2)
('c', 3)
('d', 4)
gender:M
age:20
name:hongten
sports:足球,乒乓球,游泳
[(1, 2), (3, 4), (5, 6), (7, 8)]
(1, 2)
(3, 4)
(5, 6)
(7, 8)
#######################################################
L1,L3列表为:
[1, 3, 5, 7]
[2, 4, 6]
(1, 2)
(3, 4)
(5, 6)
字典中的keys:
['name', 'age', 'gender', 'weight', 'hight']
字典中的key对应的value:
['Hongten', '20', 'M', '55', '170']
构造字典后
gender:M
age:20
name:Hongten
weight:55
hight:170
>>>

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
python将多个文本文件合并为一个文本的代码(便于搜索)
Mar 13 Python
浅谈Python基础之I/O模型
May 11 Python
python 自动去除空行的实例
Jul 24 Python
opencv实现静态手势识别 opencv实现剪刀石头布游戏
Jan 22 Python
python读取有密码的zip压缩文件实例
Feb 08 Python
Python3几个常见问题的处理方法
Feb 26 Python
python使用pip安装SciPy、SymPy、matplotlib教程
Nov 20 Python
Jupyter notebook 启动闪退问题的解决
Apr 13 Python
django 数据库返回queryset实现封装为字典
May 19 Python
Python使用scapy模块发包收包
May 07 Python
详细总结Python常见的安全问题
May 21 Python
python 如何用terminal输入参数
May 25 Python
python开发之IDEL(Python GUI)的使用方法图文详解
Nov 12 #Python
Python中pygame的mouse鼠标事件用法实例
Nov 11 #Python
Python基于pygame实现的font游戏字体(附源码)
Nov 11 #Python
python中pygame针对游戏窗口的显示方法实例分析(附源码)
Nov 11 #Python
python基于pygame实现响应游戏中事件的方法(附源码)
Nov 11 #Python
Python基于pygame实现的弹力球效果(附源码)
Nov 11 #Python
Python中pygame安装方法图文详解
Nov 11 #Python
You might like
PHP通过COM使用ADODB的简单例子
2006/12/31 PHP
ThinkPHP与PHPExcel冲突解决方法
2011/08/08 PHP
win2003服务器使用WPS的COM组件的一些问题解决方法
2012/01/11 PHP
PHP 中 Orientation 属性判断上传图片是否需要旋转
2015/10/16 PHP
PHP实现的oracle分页函数实例
2016/01/25 PHP
php自动加载方式集合
2016/04/04 PHP
动手学习无线电
2021/03/10 无线电
SUN的《AJAX与J2EE》全文译了
2007/02/23 Javascript
改写一个简单的菜单 弹性大小
2010/12/02 Javascript
jquery动态添加元素事件失效问题解决方法
2014/05/23 Javascript
jquery实现鼠标滑过小图时显示大图的方法
2015/01/14 Javascript
IE下支持文本框和密码框placeholder效果的JQuery插件分享
2015/01/31 Javascript
bootstrap布局中input输入框右侧图标点击功能
2016/05/16 Javascript
JavaScript操作选择对象的简单实例
2016/05/16 Javascript
js实现网页的两个input标签内的数值加减(示例代码)
2017/08/15 Javascript
VsCode插件整理(小结)
2017/09/14 Javascript
electron中使用bootstrap的示例代码
2018/11/06 Javascript
简述vue-cli中chainWebpack的使用方法
2019/07/30 Javascript
微信小程序实现一张或多张图片上传(云开发)
2019/09/25 Javascript
Vue实现PC端靠边悬浮球的代码
2020/05/09 Javascript
[02:22:36]《加油!DOTA》总决赛
2014/09/19 DOTA
在Python 中同一个类两个函数间变量的调用方法
2019/01/31 Python
在Keras中CNN联合LSTM进行分类实例
2020/06/29 Python
python实现逻辑回归的示例
2020/10/09 Python
Python 利用Entrez库筛选下载PubMed文献摘要的示例
2020/11/24 Python
python装饰器代码深入讲解
2021/03/01 Python
复古风格的女装和装饰品:ModCloth
2017/12/29 全球购物
人力资源经理的岗位职责
2014/03/02 职场文书
庆七一活动总结
2014/08/27 职场文书
2014年村计划生育工作总结
2014/11/14 职场文书
2014年技术工作总结范文
2014/11/20 职场文书
2014年社区个人工作总结
2014/12/02 职场文书
教师年度考核个人总结
2015/02/12 职场文书
化工厂员工工作总结
2015/10/15 职场文书
导游词之桂林
2019/08/20 职场文书
Windows中Redis安装配置流程并实现远程访问功能
2021/06/07 Redis