查看python 关键字(保留字):
>>> import keyword >>> print(keyword.kwlist) ['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
枚举
#! /urs/bin/evn python # -*- coding:utf-8 -*- index = 0 for i in range(1, 6): print(index, i) index += 1 0 1 1 2 2 3 3 4 4 5
#! /urs/bin/evn python # -*- coding:utf-8 -*- for index, i in enumerate(range(1, 6)): print(index, i) 0 1 1 2 2 3 3 4 4 5
#! /urs/bin/evn python # -*- coding:utf-8 -*- for index, i in enumerate([1, 2, 3, 4, 5]): print(index, i) 0 1 1 2 2 3 3 4 4 5
一些循环
1 #! /urs/bin/evn python 2 # -*- coding:utf-8 -*- 3 4 values = ["a", "b", "c"] 5 for i in range(len(values)): 6 print(i) 7 for i in range(len(values)): 8 print(i, values[i]) 9 valuesa = ["a", "b", "c"] 10 for i in range(len(valuesa)): 11 valuesa[i] = "x" # 解决翻倍问题 12 print(valuesa) 13 valuesa1 = [1, 5, 9] 14 for i in range(len(valuesa1)): 15 valuesa1[i] = 2 * valuesa1[i] # 解决翻倍问题 16 print(valuesa1) 17 valuesa2 = [1, 5, 9] 18 for i in range(len(valuesa2)): 19 valuesa2[i] *= 2 # 解决翻倍问题 20 print(valuesa2) 21 print("*" * 50) 22 for i in enumerate("abc"): 23 print(i) 24 print("*" * 50) 25 for i in enumerate([10, 20, 30]): 26 print(i) 27 28 print("@" * 50) 29 va = [1, 3, 6] 30 for i, v in enumerate(va): 31 32 va[i] = 2 * v 33 print(va) 34 35 a, b, c = [1, 2, 3] # (1, 2, 3)或 1, 2, 3 36 print(a) 37 print(b) 38 print(c) 39 40 times = [ 41 [19, 89, 34], 42 [13, 89, 47, 385], 43 [56, 34] 44 ] 45 for i in times: 46 for j in i: 47 print(j, end=" ") 48 print() 49 for i in times: 50 for j in i: 51 print(j, end=" ") 52 print() 53 for i in times: 54 for j in i: 55 print(j, end=" ") 56 print() 57 58 for i in times: 59 for j in i: 60 print(j, end=" ") 61 62 63 输出结果: 64 65 0 66 1 67 2 68 0 a 69 1 b 70 2 c 71 ['x', 'x', 'x'] 72 [2, 10, 18] 73 [2, 10, 18] 74 ************************************************** 75 (0, 'a') 76 (1, 'b') 77 (2, 'c') 78 ************************************************** 79 (0, 10) 80 (1, 20) 81 (2, 30) 82 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ 83 [2, 6, 12] 84 1 85 2 86 3 87 19 89 34 88 13 89 47 385 89 56 34 90 19 89 34 91 13 89 47 385 92 56 34 93 19 89 34 94 13 89 47 385 95 56 34 96 19 89 34 13 89 47 385 56 34