Hello Python3 數據類型以及傳參 內置函數 正則表達式 文件操作 異常處理 類和對象 MySQL操作 Socket編程 線程 HTTP請求 JSON 日期時間 感想 解決問題的思路才是關鍵,編程語言只是語法不同而已。 習慣性以分號結尾;哈哈哈!!! ...
Hello Python3
1 print("Hello Python!") 2 #print("Hello, Python!"); 3 '''a=1 4 b=2 5 c=a+b 6 print(c) 7 ''' 8 9 #列表:存儲多個元素的東西,列表裡面的元素是可以重新賦值的 10 a=[1,"a"] 11 a[0]=2 12 #元組:存儲多個元素的東西,元組裡面的元素不可用重新賦值 13 b=(1,"b") 14 #字典 15 #{key1:value1, key2:value} 16 #集合 17 #去重 18 19 #if語句 20 a=10 21 b=1 22 if(a > 9): 23 print(a) 24 if(b < 9): 25 print(b) 26 27 age=18 28 if(age <= 10): 29 print("兒童") 30 elif(age > 10 and age <= 20): 31 print("青少年") 32 elif(age > 20): 33 print("青年") 34 else: 35 print("小伙子") 36 37 #while語句 38 a=0 39 while(a<10): 40 print("hello") 41 a += 1 42 43 #for語句:遍歷列表 44 a=["aa", "bb", "cc", "dd"] 45 for i in a: 46 print(i) 47 #for語句:常規迴圈 48 for i in range(0, 10): 49 print(i) 50 for i in range(0, 10): 51 print("AAA") 52 #中斷退出:continue,break 53 for i in a: 54 if(i=="cc"): 55 break 56 print(i) 57 for i in a: 58 if(i=="cc"): 59 continue 60 print(i) 61 #乘法口訣 62 #end=""表示不換行輸出 63 for i in range(1, 10): 64 for j in range(1, i+1): 65 print(str(i) + "*" + str(j) + "=" + str(i*j), end=" ") 66 print() 67 print("-----------------------") 68 for i in range(9, 0, -1): 69 for j in range(1, i+1): 70 print(str(i) + "*" + str(j) + "=" + str(i*j), end=" ") 71 print() 72 73 ''' 74 #作用域 75 i=10 76 def func(): 77 j=20 78 print(j) 79 print(j) 80 ''' 81 82 #函數 83 #函數定義格式: 84 #def 函數名(參數): 85 # 函數體 86 87 def abc(): 88 print("abcdef") 89 #調用函數:函數名(參數) 90 abc() 91 92 def func2(a, b): 93 if(a > b): 94 print("a>b") 95 else: 96 print("a<=b") 97 func2(1,2) 98 func2(5,4) 99 100 #模塊導入 101 import cgi 102 cgi.closelog() 103 from cgi import closelog 104 105 #文件操作 106 #打開 open(文件路徑, 操作形式) 107 ''' 108 w:寫 109 r:讀 110 b:二進位 111 a:追加 112 ''' 113 file = open("D:/python/1.txt", "r") 114 data=file.read() 115 print(data) 116 data=file.readline() 117 print(data) 118 file.close() 119 #文件寫入 120 data2="一起學python" 121 file2=open("D:/python/2.txt", "w") 122 file2.write(data2) 123 file2.close() 124 data2="Java" 125 file2=open("D:/python/2.txt", "a+") 126 file2.write(data2) 127 file2.close() 128 129 #異常處理 130 try: 131 a = 1 / 0 132 except Exception as ex: 133 print(ex) 134 135 #類 136 class User: 137 def __init__(self): 138 print("無參構造函數") 139 a=User() 140 141 class Person: 142 def __init__(self, name, age): 143 print("帶參數的構造函數,name=" + name + ", age=" + age) 144 c1=Person("zhangsan", str(20)) 145 c2=Person("lisi", str(22)) 146 147 class Rabbit: 148 def __init__(self, name, color): 149 self.name = name 150 self.color = color 151 def func1(self): 152 print("aaa"); 153 def func2(self, age): 154 print("這個兔子已經" + str(age) + "歲了") 155 def toString(self): 156 print("Rabbit [name=" + self.name + ", color=" + self.color + "]") 157 r1 = Rabbit("流氓兔", "白色") 158 r1.func1() 159 r1.func2(3) 160 r1.toString() 161 162 #繼承 163 class Father(): 164 def write(self): 165 print("寫作") 166 class Son(Father): 167 pass 168 class Mother(): 169 def draw(self): 170 print("畫畫") 171 def makeDinner(self): 172 print("做飯") 173 class Daughter(Father, Mother): 174 def draw(self): 175 print("素描") 176 son = Son() 177 son.write() 178 daughter = Daughter() 179 daughter.write() 180 daughter.draw() 181 daughter.makeDinner() 182 183 184 print("Hello\n" * 3) 185 186 #str是關鍵字是函數名不能隨便用 187 tmp = input("請輸入一個數字:") 188 print(tmp) 189 190 #遞歸 191 def factorial(n): 192 if(n == 1): 193 return 1 194 else: 195 return n * factorial(n-1) 196 number = int(input("請輸入一個正整數:")) 197 result = factorial(number) 198 print("%d的階乘是%d"%(number, result)) 199 200 201 def fab(n): 202 if(n == 1 or n == 2): 203 return 1 204 else: 205 return fab(n-1) + fab(n-2) 206 number = int(input("請輸入一個正整數:")) 207 result = fab(number) 208 print("總共" + str(result) + "對兔子")
數據類型以及傳參
1 # 數據類型 2 num = 123 3 print(type(num)) 4 f = 2.0 5 f = 5.0 / 2 6 print(f) 7 str1 = 'hello world' 8 print(str1) 9 str2 = "hello world" 10 print(str2) 11 str3 = "hello \"world\"" 12 str4 = "hello 'world'" 13 print(str3) 14 print(str4) 15 str5 = "Jack: \nI love you" 16 print(str5) 17 mail = """ 18 Rose: 19 I'am Jack 20 goodbye 21 """ 22 print(mail) 23 str6 = "abcdef" 24 print(str6[0]) 25 print(str6[1]) 26 print(str6[0] + str6[1]) 27 print(str6[1:4]) 28 print(str6[4:]) 29 print(str6[:4]) 30 print(str6[2:]) 31 print(str6[::2]) 32 print(str6[-1]) 33 print(str6[-4:-1]) 34 str7 = "1234" 35 print(id(str7)) 36 str7 = "1234" 37 print(id(str7)) 38 39 # 元組 40 t = ("Jack", 19, "male") 41 print(t) 42 print(t[0]) 43 print(type(t)) 44 t2 = (3,) 45 print(type(t2)) 46 print(len(t2)) 47 print(len(str6)) 48 a,b,c=(1,2,3) 49 print(a) 50 print(b) 51 print(c) 52 a,b,c=t 53 print(a) 54 print(b) 55 print(c) 56 57 # 列表 58 list1 = ["jack", 21, True, [1,2,3]] 59 print(list1[0]) 60 print(type(list1)) 61 print(list1[3]) 62 print(len(list1)) 63 list1[0]="rose" 64 list1[1]="jerry" 65 print(list1) 66 list1.append(3) 67 print(list1) 68 list1.remove(3) 69 del(list1[2]) 70 print(list1) 71 list2 = [1,2,3] 72 list2.append(4) 73 74 # 查看幫助 75 help(list) 76 help(list.remove) 77 help(len) 78 79 # 字典 80 dict1 = {"name":"jack", "age":25, "gender":"male"} 81 print(dict1["name"]) 82 print(dict1.keys()) 83 print(dict1.values()) 84 dict1["phone"] = "13712345678" 85 dict1["phone"] = "15923423455" 86 print(dict1) 87 del dict1["phone"] 88 print(dict1) 89 print(dict1.pop("age")) 90 91 # 條件語句 92 if 1 < 2: 93 print(1234) 94 if(2 > 1): 95 print("hhh") 96 if(True): 97 print("ok") 98 if(1+1): 99 print("yes") 100 101 102 # for迴圈 103 print(range(1,10)) 104 print(range(1, 10, 2)) 105 print(range(5)) 106 arr = ['a', 'b', 'c', 'd'] 107 for i in arr: 108 print(i) 109 for i in range(len(arr)): 110 print(arr[i]) 111 num = 0 112 for i in range(101): 113 num += i 114 print(num) 115 116 for i in range(3): 117 print(i) 118 else: 119 print("finish") 120 121 # 遍歷字典 122 d = {1:111, 2:222, 3:333} 123 for x in d: 124 print(d[x]) 125 for k,v in d.items(): 126 print(k) 127 print(v) 128 d2 = {"name":"zhangsan", "age":20} 129 for k,v in d2.items(): 130 print(k) 131 print(v) 132 133 # 函數 134 def func(): 135 print("123") 136 func() 137 138 def func2(x, y): 139 print(x) 140 print(y) 141 func2(3,4) 142 143 # 參數的預設值 144 def func3(x=5,y=6): 145 print("x=" + str(x) + ", y=" + str(y)) 146 func3() 147 func3(7,8) 148 149 def func4(x, y=9): 150 print("x=" + str(x) + ", y=" + str(y)) 151 func4(10) 152 func4(11, 12) 153 154 def func5(): 155 x = input("Please Enter A Number: ") 156 print(x) 157 func5() 158 159 160 def func6(): 161 global a 162 a = "good" 163 func6() 164 print(a) 165 166 # 傳參:元組 167 # 傳元組 168 def f(x,y): 169 print("x=%s, y=%s" % (x,y)) 170 f(1,2) 171 f(*(3,4)) 172 t = ("Tom", "Jerry") 173 f(*t) 174 175 def f1(x, y): 176 print("x=%s, y=%s" % (x, y)) 177 t = (20, "Jack") 178 f1(*t) 179 180 # 傳字典 181 def f2(name, code, age): 182 print("name=%s, code=%s, age=%s" % (name, code, age)) 183 d = {"name": "zhangsan", "code": "10001", "age": 22} 184 f2(**d) 185 d["age"] = 28 186 f2(**d) 187 f2(d["name"], d["code"], d["age"]) 188 189 # 參數冗餘 190 # 多餘的實參 191 def f3(x, *args): 192 print(x) 193 print(args) 194 f3(1) 195 f3(1,2) 196 f3(1,2,3,4) 197 def f4(x, *args, **kwargs): 198 print(x) 199 print(args) 200 print(kwargs) 201 f4(1) 202 f4(1, 2) 203 f4(1, 2, 3) 204 f4(1, y=3, z=4) 205 f4(1, **d) 206 f4(1, 2, 3, 4, y=6, z=7, xy=8) 207 208 # lambda表達式 209 f6 = lambda x,y:x*y 210 print(f6(3, 4)) 211 212
內置函數
1 # Python內置函數 2 print(abs(2)) 3 print(abs(-2)) 4 r = range(11) 5 print(max(r)) 6 print(min(r)) 7 print(max([3,2,1,9])) 8 print(len(r)) 9 print(divmod(5,3)) 10 print(pow(2,10)) 11 print(round(2.5)) 12 print(type(r)) 13 a = "1234" 14 print(int(a)) 15 b = 99 16 print(str(b)) 17 print(hex(b)) 18 print(list([1,2,3,4])) 19 20 # 字元串函數 21 # str.capitalize() 首字母大寫 22 # str.replace() 字元替換 23 # str.split() 分割 24 help(str.capitalize) 25 help(str.replace) 26 help(str.split) 27 s = "hello" 28 print(s.capitalize()) 29 y = "123123123123" 30 print(y.replace("12", "AB")) 31 print(y.replace("12", "AB", 1)) 32 print(y.replace("12", "AB", 2)) 33 ip = "192.168.12.93" 34 print(ip.split(".")) 35 print(ip.split(".