如果你对python中的for循环不是很清楚,请看看这篇文章:”for循环控制语句——菜鸟的Python笔记

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

 #基本的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':'','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','','M','','']
#使用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 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 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
sports:足球,乒乓球,游泳
gender:M
name:hongten
age:20
<zip object at 0x01FA1AA8>
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', '', 'M', '', '']
构造字典后
weight:55
hight:170
gender:M
name:Hongten
age:20
>>>

python开发_python中for循环操作的更多相关文章

  1. python开发_python中字符串string操作

    在python中,对于字符串string的操作,我们有必要了解一下,这样在我们的以后的开发中会给我们带来很多方便 下面是我学习的笔记: #python-string #python中的字符串用单引号' ...

  2. python开发_python中str.format()

    格式化一个字符串的输出结果,我们在很多地方都可以看到,如:c/c++中都有见过 下面看看python中的字符串格式函数str.format(): 1 #使用str.format()函数 2 3 #使用 ...

  3. python开发_python中的Boolean运算和真假值

    python中的真假值: Truth Value Testing Any object can be tested for truth value, for use in an if or while ...

  4. python开发_python中的range()函数

    python中的range()函数的功能hen强大,所以我觉得很有必要和大家分享一下 就好像其API中所描述的: If you do need to iterate over a sequence o ...

  5. python开发_python中的module

    在python中,我们可以把一些功能模块化,就有一点类似于java中,把一些功能相关或者相同的代码放到一起,这样我们需要用的时候,就可以直接调用了 这样做的好处: 1,只要写好了一个功能模块,就可以在 ...

  6. python开发_python中的函数定义

    下面是我做的几个用列: #python中的函数定义,使用和传参 def_str = '''\ python中的函数以如下形式声明: def 函数名称([参数1,参数2,参数3......]): 执行语 ...

  7. python开发_python中的变量:全局变量和局部变量

    如果你在为python中的变量:全局变量和局部变量头疼,我想这篇blog会给你帮助 运行效果: 代码部分: #Python中的变量:全局变量和局部变量 #在很多语言中,在声明全局变量的时候,都喜欢把全 ...

  8. python开发_python中的list操作

    对python中list的操作,大家可以参考: Python list 操作 以下是我个人的笔记: ============================================ Add b ...

  9. python开发_python关键字

    python3.3.2中的关键字如下: The following identifiers are used as reserved words, or keywords of the languag ...

随机推荐

  1. yxy-插入formid-并发/压力(入参的变量化)

    1.抓取接口如下 2.该接口为插入数据库操作,由于前段操作频繁,就简单压测一下 接口中formId.accountId.openId为变量 formId为随机生成插入:accountId.openId ...

  2. Qt jsoncpp 对象拷贝、删除、函数调用 demo

    /*************************************************************************************************** ...

  3. Java 层级的简单理解

    在J2EE项目中,开发的都是分层来做的: 1.service层:用于暴露给网络调用 2.Impl层:统一规范接口 3.bean层:实体对象,也就是表 4.DAO(Data Access Object) ...

  4. Page View Controllers

    Page View Controllers You use a page view controller to present content in a page-by-page manner. A ...

  5. python(十一)、线程

    一.基本概念 进程是资源分配的基本单位,而线程则是CPU调度和分派的基本单位.系统需要执行创建进程.撤销进程和进程切换等任务,但创建进程开销大限制了并发的提高.因此,成百上千个进程会造成内存浪费,并且 ...

  6. MYSQL在当前日期的基础上加上N(年月日分秒)

    //把id为1的那条数据的开始时间改为现在  结束时间改为开始时间的1个月后 update 表明 set  begintime=now(),endtime=date_add(NOW(), interv ...

  7. autoconf 简单demo试用

    1. 安装工具 yum install -y automake   2. 基本项目 a. 项目参考结构 ├── AUTHORS ├── COPYING ├── ChangeLog ├── Makefi ...

  8. Docker-Compose API too old for Windows

    I was working on some code with a Docker Windows container today and ran into this error message: ER ...

  9. 使用 Git 对原理图和线路板时行版本控制

    使用 Git 对原理图和线路板时行版本控制 由于之前一直用 Git 管理代码,我又开始试用 git 来对原理图和线路板时行版本控制. 由于 原理图和 PCB 的文件都是二进制文件,git 管理并不怎么 ...

  10. bzoj1588[HNOI2002]营业额统计——双向链表

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1588 简单Splay.但用双向链表做.很好的思路. 1.(离线)按值排序,记下pre和nxt ...