如果你对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. 使用HslCommunication实现PLC数据的远程客户端监视,以及web端实时监视,远程操作设备示例

    前言 本文主要是演示一个例子,服务器后台程序从PLC采集数据,并推送给在线客户端显示,以及推送给web端进行实时的显示,还支持远程操作,支持安卓端的同步监视和远程操作,关于HslCommunicati ...

  2. caffe官网的部分翻译及NG的教程

    Caffe原来叫:Convolutional Architecture for Fast Feature Embedding 官网的个人翻译:http://blog.csdn.net/fengbing ...

  3. vue样式穿透

    在一次这样的需求中,需要实现滑倒底部时自动请求数据,需要动态创建节点然后追加到某元素中,这期间遇到的问题就是在动态创建节点后,类名也已经加上了 ,但是样式就是没有生效,最后发现原因的产生竟然是< ...

  4. phpcms打印数据

    <?php echo '<pre>'; var_dump($data); echo '</pre>'; ?>

  5. Java [Leetcode 167]Two Sum II - Input array is sorted

    题目描述: Given an array of integers that is already sorted in ascending order, find two numbers such th ...

  6. selenium-java,UI自动化截图方法

    截图方法: import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; impor ...

  7. HDU2896 病毒侵袭 【AC自动机】

    HDU2896 病毒侵袭 Problem Description 当太阳的光辉逐渐被月亮遮蔽,世界失去了光明,大地迎来最黑暗的时刻....在这样的时刻,人们却异常兴奋--我们能在有生之年看到500年一 ...

  8. Redis构建全局并发锁

    Redis构建全局并发锁 https://www.cnblogs.com/FG123/p/9990336.html 谈起Redis的用途,小伙伴们都会说使用它作为缓存,目前很多公司都用Redis作为缓 ...

  9. 如何向map和reduce脚本传递参数,加载文件和目录

    本文主要讲解三个问题:       1 使用Java编写MapReduce程序时,如何向map.reduce函数传递参数.       2 使用Streaming编写MapReduce程序(C/C++ ...

  10. web.xml中配置classpath:和classpath*:的区别和意思

    首先 classpath是指 WEB-INF文件夹下的classes目录 解释classes含义: 1.存放各种资源配置文件 eg.init.properties log4j.properties s ...