Python 循环
出处:
1、for循环
GUI中的代码:
1 #基本的for循环语句 2 test_list = [2,4.4,'Liu','A',"B",'HelloPy',"Today"] 3 #打印列表的长度 4 print(len(test_list)) 5 for ele in test_list: 6 print(ele) 7 8 print ('#' * 50) 9 test_str = "hello,Python" 10 print("打印字符串:" + test_str) 11 print('遍历字符串:') 12 for s in test_str: 13 print (s) 14 15 print ('#' * 50) 16 test_tuple = [("a",1),("b",2),("c",3),("d",4)] 17 print(test_tuple) 18 print('遍历一个元组') 19 for (i,j) in test_tuple: 20 print(i,j) 21 22 print ('#' * 50) 23 #字典迭代器 24 test_dict = {'name':'Leo','age':'20','gender':'M','sports':'网球'} 25 for key in test_dict: 26 print(key + ':' + test_dict[key]) 27 28 print ('#' * 50) 29 #使用zip将两个列表合并 30 L1 = [1,3,5,7] 31 L2 = [2,4,6,8] 32 print(zip(L1,L2)) 33 for (i,j) in zip(L1,L2): 34 print(i,j) 35 36 print ('#' * 50) 37 L3 = L2[:] 38 L3.remove(8) 39 print('L1,L3列表为:') 40 print(L1) 41 print(L3) 42 for (i,j) in zip(L1,L3): 43 print(i,j) 44 #可以看出来当长度不一的时候,多余的被忽略 45 46 print ('#' * 50) 47 #使用zip来构造一个字典 48 test_keys = ['name','age','gender','weight','hight'] 49 test_values = ['Leo','20','L','65','186'] 50 print('字典中的keys:' ) 51 print(test_keys) 52 print('字典中的key对应的value:') 53 print(test_values) 54 print('构造字典后') 55 test_dic = dict(zip(test_keys,test_values)) 56 for key in test_dic: 57 print( key + ':' + test_dic[key])
运行结果:
1 >>> ================================ RESTART ================================ 2 >>> 3 7 4 2 5 4.4 6 Liu 7 A 8 B 9 HelloPy 10 Today 11 ################################################## 12 打印字符串:hello,Python 13 遍历字符串: 14 h 15 e 16 l 17 l 18 o 19 , 20 P 21 y 22 t 23 h 24 o 25 n 26 ################################################## 27 [('a', 1), ('b', 2), ('c', 3), ('d', 4)] 28 遍历一个元组 29 a 1 30 b 2 31 c 3 32 d 4 33 ################################################## 34 name:Leo 35 age:20 36 sports:网球 37 gender:M 38 ################################################## 39 <zip object at 0x02A55788> 40 1 2 41 3 4 42 5 6 43 7 8 44 ################################################## 45 L1,L3列表为: 46 [1, 3, 5, 7] 47 [2, 4, 6] 48 1 2 49 3 4 50 5 6 51 ################################################## 52 字典中的keys: 53 ['name', 'age', 'gender', 'weight', 'hight'] 54 字典中的key对应的value: 55 ['Leo', '20', 'L', '65', '186'] 56 构造字典后 57 name:Leo 58 age:20 59 hight:186 60 gender:L 61 weight:65 62 >>>
2、while循环
GUI中的代码:
1 num = 1 2 #必须设定初始值 3 while (num < 10): 4 print (num) 5 num = num +1 6 #格式代表了大括号 7 8 print ('#'*50) 9 # continue 和 break 用法 10 i = 1 11 #输出偶数 12 while i < 10: 13 i= i+1 14 if i%2 == 0: 15 print (i) 16 continue 17 print('#'*25) 18 #大于10时跳出循环 19 i = 1 20 while i<10: 21 print (i) 22 i = i + 1 23 if i>10: 24 break
运行结果:
1 >>> ================================ RESTART ================================ 2 >>> 3 1 4 2 5 3 6 4 7 5 8 6 9 7 10 8 11 9 12 ################################################## 13 2 14 4 15 6 16 8 17 10 18 ######################### 19 1 20 2 21 3 22 4 23 5 24 6 25 7 26 8 27 9 28 >>>
3、continue break
GUI中的代码:
1 print ('#'*50) 2 # 在循环的某一次执行中,遇到continue,则跳过这一次执行,进行下一次的操作 3 # 停止执行整个循环 4 for i in range(10): 5 if i == 2: 6 continue 7 print (i) 8 print ('#'*50) 9 for i in range(10): 10 if i == 2: 11 break 12 print (i)
运行结果:
1 ################################################## 2 0 3 1 4 3 5 4 6 5 7 6 8 7 9 8 10 9 11 ################################################## 12 0 13 1 14 >>>