###### tags: `Python 教學` # python 教材 這裡的程式碼,都是可以用的,我會打上個物件的用法以及使用範例 ## print(....) ![](https://i.imgur.com/gBbO3zQ.png) ```python= print(123,456,789) #最後換行,不同參數隔開 ``` ```python= print(123,456,789,sep='',end='') #不換行,不同參數不隔開 ``` 通常用法: 後面可以塞不限數量的參數 ## input() ![](https://i.imgur.com/wd4LmsH.png) 題目:計算BMI 公式:體重/$身高^2$ ```python= #Height 身高 #Weight 體重 Height = float(input("輸入身高")) #宣告Height這個變數,並把輸入資料放入這個資料 #並且我認為身高是一個小數,所以將輸入變成float型態(float小數的意思) Weight = float(input("輸入體重")) #宣告Weight這個變數,並把輸入資料放入這個資料 #並且我認為體重是一個小數,所以將輸入變成float型態(float小數的意思) print((Weight)/(Height*Height)) ``` ## 字串用法 ### 拿文字中其中一個字 ```python= a = '123456789'#index從0開始 print(a[0])#輸出1 print(a[3])#輸出4 print(a[6])#輸出7 ``` ### 拿文字中其中一段文字 [first:end] 印出a[first~end-1] ```python= a = '123456789'#index從0開始 print(a[0:2])#輸出12 print(a[3:6])#輸出456 print(a[6:9])#輸出789 ``` ### 間隔拿取 可以控制範圍,以特定週期拿取 ```python= a = '123456789'#index從0開始 print(a[0:2:1])#輸出12 print(a[3:6:2])#輸出46 print(a[6:9:3])#輸出7 ``` ## 迴圈用法 ### 執行特定次數 for i in range(count): 想要執行的代碼 ```python= for i in range(10): print('123') #輸出10次 for i in range(100): print('123') #輸出100次 ``` ### 執行特定次數,特定範圍 從first 到 end 以step前進 for i in range(first,end,step): 想要執行的代碼 ```python= for i in range(0,10,2): print(i) #輸出0 2 4 6 8 ``` ## List應用 ### 宣告List ```python= a = []#選告a = List型態 ``` ### 新增資料list.append() ```python= a = []#選告a = List型態 a.append(1)#a =[1] a.append(2)#a =[1,2] a.append(100)#a =[1,2,100] print(a)#輸出[1,2,100] ``` ### 插入資料list.insert(index,obj) ```python= a = ["123","456"]#選告a = List型態 a.insert(1,"123")#a =[1] a.insert(2,"")#a =[1,2] a.insert(100)#a =[1,2,100] print(a)#輸出[1,2,100] ``` ### 拿取資料list[] ```python= a = []#選告a = List型態 a.append(1)#a =[1] a.append(2)#a =[1,2] a.append(100)#a =[1,2,100] print(a[0])#輸出1 print(a[1])#輸出2 print(a[2])#輸出100 ``` ### 清除特定位置的資料 方法1 ```python= a = []#選告a = List型態 a.append(1)#a =[1] a.append(2)#a =[1,2] a.append(100)#a =[1,2,100] del a[1]#a =[1,100] print(a[2])#輸出100 ``` 方法2 ```python= a = []#選告a = List型態 a.append(1)#a =[1] a.append(2)#a =[1,2] a.append(100)#a =[1,2,100] a.pop(1)#a =[1,100] print(a[2])#輸出100 ``` ### 清除特定位置範圍的資料 ```python= a = []#選告a = List型態 a.append(1)#a =[1] a.append(2)#a =[1,2] a.append(100)#a =[1,2,100] del a[0:2]#a =[1,100] print(a[0])#輸出100 ``` ### sum() 加總裡面所有參數 ```python= a = []#選告a = List型態 a.append(1)#a =[1] a.append(2)#a =[1,2] a.append(100)#a =[1,2,100] print(sun(a))#輸出103 ``` ### max() min() 輸出所有參數裡面最大以及最小的 ```python= a = []#選告a = List型態 a.append(1)#a =[1] a.append(2)#a =[1,2] a.append(100)#a =[1,2,100] print(max(a))#輸出100 print(min(a))#輸出1 ``` ## while 他是可以執行不固定次數的迴圈 題目:我在外面玩,直到嬤嬤叫我回去 ```python= while(媽媽有沒有叫我回家): 玩遊戲 ``` ## break 可以提早終止while,或不執行特定次數 ```python= while(True): 玩遊戲 if 媽媽叫我回去: break ``` ## continue 描述可以跳過特定次數 題目:我星期七不能玩遊戲,其他天我都要玩 ```python= while(True): if 星期七: continue 玩遊戲 ``` ```python= l = [] while True: l.append(input()) if l[-1] == "End": break print(l) ``` ## 函數 ```python= def f(x,y): return x+y print(f(10,20)) ``` 輸出: 30 ## String ### split(reg) 利用特定符號切割文字 ex: ``` a = "123,456" b = a.split(",")ㄘ print(b) ``` output ``` ['123','456'] ``` ### join(list) 利用特定符號合併文字 ex: ``` a = [123,456] print(','.join(a)) ``` output ``` 123,456 ``` ### replace(a,b) 將字串裡面所有的a,b ex: ``` a = "123,456" print(','.join) ``` output ``` 123,456 ``` ## 文字編輯器 ```python= n = 0 # q = 0 #宣告變數 artice = [] # 放文章的地方 s = input() # 文字載入進來 n = int(s.split(',')[0]) q = int(s.split(',')[1]) for i in range(n): #執行n次 0,1,2,3.....n-1 n次 val = input()#輸入載入 artice.append(val) #輸入放置清單 for i in range(q): #執行q次 0,1,2,3.....q-1 q次 cmd = input() if cmd == "add": line = input() artice.append(line) if cmd == "insert": I = int(input()) line = input() artice.insert(I-1,line) if cmd == "delte": I = int(input()) artice.pop(I-1)# 去除i元素 if cmd == "print": for k in range(len(artice)): print(artice[k]) ``` ## 跟電腦比大小 我們輸入一個骰子數字n,然後電腦也出一個骰子數字,然後如果電腦贏了,輸出coumputer win 如果我們贏了,輸出 win win,平手輸出Tie,不可以輸入重複座標,若輸入重複座標,人重新輸入,電腦重新生成 python in python set python 判斷已經出現過的元素 ## OX遊戲 使用者跟電腦輪流輸入,然後如果電腦贏了,輸出coumputer win 如果我們贏了,輸出 win win,平手輸出Tie(電腦座標是亂數,但已經出現過的座標不可以生成,玩家先手) 畫圖表:https://app.diagrams.net/ ``` 產生座標 座標解釋 座標:a 1 2 3 4 5 6 7 8 9 a[1][1]=5; a[0][2]=3; a[2][0]=7; ``` 輸入1: ``` 0 0 0 1 0 2 ``` 圖表1: ``` X X X - - - O O - ``` 輸出1: ``` win win ``` 輸入2: ``` 0 0 0 1 1 1 ``` 圖表2: ``` X X - - X - O O O ``` 輸出2: ``` coumputer win ``` 輸入3: ``` 0 0 0 1 1 1 2 3 2 0 ``` 圖表3: ``` X X O O X O X O X ``` 輸出3: ``` Tie ``` ## example ![](https://i.imgur.com/0pNcn1o.png) ``` import random table = [[-1, -1, -1], [-1, -1, -1], [-1, -1, -1]] # -1:還沒有下過 0:X 1:O # table[y][x] round = 0 # for while while True: if (table[0][0] == table[0][1] == table[0][1] or table[1][0] == table[1][1] == table[1][1] or table[2][0] == table[2][1] == table[2][1] or table[0][0] == table[0][1] == table[0][2] or table[1][0] == table[1][1] == table[1][2] or table[2][0] == table[2][1] == table[2][1] or table[0][0] == table[1][1] == table[2][2] or table[2][0] == table[1][1] == table[0][2]): if (table[0][0] == table[0][1] == table[0][1] and table[0][0] == 0 or table[1][0] == table[1][1] == table[1][1] and table[1][0] == 0 or table[2][0] == table[2][1] == table[2][1] and table[2][0] == 0 or table[0][0] == table[0][1] == table[0][2] and table[0][0] == 0 or table[1][0] == table[1][1] == table[1][2] and table[1][0] == 0 or table[2][0] == table[2][1] == table[2][1] and table[2][0] == 0 or table[0][0] == table[1][1] == table[2][2] and table[0][0] == 0 or table[2][0] == table[1][1] == table[0][2] and table[2][0] == 0): print('x win') break #程式結束 else: print('O win') break #程式結束 else: if round == 0: s = input("請輸入座標") s = s.split(" ") x = int(s[0]) y = int(s[1]) table[x][y] = 0 else: x = int(random.randint(0, 2)) y = int(random.randint(0, 2)) table[x][y] = 1 ``` ## 函式運用 ### 函式評級 0>分數 或者 100<socre 輸出error 範圍內輸出評級 90~100 A 80~89 B .. .. .. input1 ``` -100 ``` output1 ``` error ``` input2 ``` 90 ``` output3 ``` A ``` ```python= def grade(val): return ? ``` ### 進制轉換 輸入10進位轉n進位 input ``` 10 2 ``` output ``` 1010 ``` ```python= def tran(val,n): return ? ``` ## 印星星 輸出一個高度為n的三角形 輸入1: 3 輸出1: ``` * *** ***** ``` 輸入2: 4 輸出2: ``` * *** ***** ******* ``` ```python= def PrintSpacce(n): print(?) def PrintStar(n): print(?) print() n = int(input()) for ? : ? ? ``` ```python= def PrintSpace(n): # 印n個空白 for i in range(n): print(' ',end='') def PrintStar(n): # 印n個星號 for i in range(n): print('*',end='') n = int(input()) for i in range(n): if i==0 or i==n-1: PrintSpace(n-i) PrintStar(2*i+1) else: PrintSpace(n - i) PrintStar(1) PrintSpace(2*i-1) PrintStar(1) print() ``` ## 通訊綠 ![](https://i.imgur.com/Yl2S3j1.png) ``` #if name == "end": # 如果輸入是end就.... # pass # 程式終結 data = list()#通訊綠 = list() name = input()#輸入名子 while name != "end": if name in data: # 如果這個名子已經存在過就... print(name,'已經存在於通訊錄') #印出警告 else: data.append(name)## 加入新同學 name = input() # 輸入名子 ``` ![](https://i.imgur.com/pRCWkNQ.png) ![](https://i.imgur.com/BaTul3E.png) ```python= list1 = [[1,2,3],[4,5,6],[7,8,9]] i = 0 j = 0 for i in range(3): for j in range(3): print(f'list1[{i}][{j}]={list1[i][j]} ',end='') print() ``` ## DICT ### 水果價錢 請替水果標價建立一個目錄,並以字典的方式儲存 ```python= #使用dict(字典) a = {"香蕉":20,"蘋果":50,"西瓜":30}#標籤 print(a["香蕉"]) ``` ### 獎牌 請建立獎牌與個數的字典資料,金牌26個、 銀牌30個、銅牌34個,並使用keys 與 values的方法顯示出各種獎牌的數量 ```python= a = {"金牌":26,"銀牌":30,"銅牌":34}#標籤 print("金牌數量為"+str(a["金牌"])+"個") print("銀牌數量為"+str(a["銀牌"])+"個") print("銅牌數量為"+str(a["銅牌"])+"個") ``` ## set ### Add ``` set1 = { 1, 2, 3, 4, 5,1,2,11} set1.add(6)# 加入這個集合 print(set1) ``` ### remove ``` set1 = { 1, 2, 3, 4, 5,1,2,11} set1.add(6)# 加入這個集合 set1.remove(1)# 移除出集合 print(set1) ``` ## 聯集/交集 ![](https://i.imgur.com/3S42Sii.png) ![](https://i.imgur.com/XoJBZ76.png) ``` set1 = { "騎腳踏車","寫程式","跑馬拉松"}# 我的興趣 set2 = { "寫程式","打電動","跑步"} # 你的興趣 print('我們同時傭有的興趣',set1 & set2)# & = and print('我們的興趣所有種類',set1 | set2)# | = or ``` ## EX ![](https://i.imgur.com/V09if4s.png) ```python= A = {'電視':15000,"冷氣":28000,"冰箱":23000,"電鍋":3500} # 原始 a = input("輸入產品名稱") while a!="finish": if a in A: print(a,"的價錢是",A[a])#A[a]拿取價錢 else: b = input("輸入產品價錢") A[a]=b#設定物件價錢 a = input("輸入產品名稱") print(A) ``` ## 例外 ![](https://i.imgur.com/whYcps3.png) ```python= a = 10 b = 0 try: print(a / b) # 9/3=3 => 3*3=9 # 10/0=x =>0*x = 10 undefine未定義 except Exception as e: print(e) ``` ## Buble Sort ![](https://i.imgur.com/yXcJeYo.png) ``` list1 = [4,-15,20,13,-6] for i in range(5):#我們要排擠次序 for j in range(4-i):#排序的比較 if list1[j] > list1[j+1]: c = list1[j+1] list1[j+1] = list1[j] list1[j] = c print(list1) ``` ## Sorted/Sort ``` x = ["a","b","c","ab","ac"] #ascii # 排序並建立新的 List y = sorted(x) print(y) # 對原本的 List 排序 x.sort() print(x) ```