列表、元组、字典等遍历
#!/usr/bin/env python # -*- coding:utf-8 -*- #列表遍历 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) #遍历一个字符串 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] print(zip(L1,L2)) for (i,j) in zip(L1,L2): print(i,j) L3=L2[:] L3.remove(8) 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'] print(test_keys) print(test_values) test_dic=dict(zip(test_keys,test_values)) for key in test_dic: print(key+':'+test_dic[key])
#!/usr/bin/env python
#_*_ coding:utf-8 _*_
list01 = [1,2,3,'a','aa']
for i in list01:
print i
for i,n in enumerate(['a','b','c']):
print i,n
li =['手表','汽车','房子']
for item in li:
print item
for item in enumerate(li,1):
print item[0],item[1]
#------------------------------------------------
list02 = (1,2,3,4,5)
for i in list02:
print i
#------------------------------------------------
list03 = {"name":'yy','age':'18'}
for key in list03:
print key+':'+list03[key]
#------------------------------------------------
i=iter(range(5))
print i.next()
print i.next()
print i.next()
print i.next()
print i.next()
#迭代key
s={'one':1,'two':2,'three':3}
m = iter(s)
print m.next()
print m.next()
print m.next()
#------------------------------------------------
#while
a = input('请输入a:')
b = input("请输入b: ")
while True:
if a == b:
print "=="
break
print "!="
else:
print 'over'
count = 0
while(count < 9):
print count
count += 1
#------------------------------------------------
# for
for i in range(1,5):
print i
else:
print 'over'
list = ['a','b','c','b']
for i in range(len(list)):
print list[i]