# 111-08-20 Python入門實作班上課記錄 ###### tags: `python` #### 比較與邏輯運算子 ``` print(True and False) a = 3 b = 4 print(a > b) print(not True) print(a == 3 or b == 9) # print('3' > 2) # 無法比較 print('!跟$比較', '!' > '$') print('A' > 'a') print('ab' > 'aa') print('XX', 'Ac' > 'Ba') ``` #### 指派運算子 ``` a = 3 a += 3 # a = a + 3 print('a=', a) b = 4 b = b ** 2 print('b=', b) ``` > 所有的算數運算子都有對應的指派運算子 #### 索引切片 ``` a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] # list, set, dict, tuple print(a[3:5]) # 從索引取到索引5, 但是不包含索引5 print(a[1:4]) print(a[:4]) # [start:end] # 但是不包含終點 print(a[6:]) print(a[2:8:2]) # [start:end:step] print(a[::2]) print(a[::1]) print(a[::-1]) print(a[2:7:-1]) print(a[7:2:-2]) ``` > **注意** > 當起始索引比結束索引小,而step又是負值的時候,將取不到任何資料 #### 寫一程式,讓使用者輸入5個數字,取得最大和最小數字後,顯示在畫面上 ``` a = [] a.append( int( input('請輸入第1個數字:') ) ) a.append( int( input('請輸入第2個數字:') ) ) a.append( int( input('請輸入第3個數字:') ) ) a.append( int( input('請輸入第4個數字:') ) ) a.append( int( input('請輸入第5個數字:') ) ) a.append( int( input('請輸入第6個數字:') ) ) a.append( int( input('請輸入第7個數字:') ) ) a.sort() # 由小到大排序 print(a[::len(a) - 1]) ``` #### 猜數字遊戲 ``` import random # 引入亂數模組 # 猜數字遊戲,讓玩家猜1~5之間一個數字,然後告知有沒有猜對 anwser = random.randint(1, 5) # 產生1~5之間的隨機整數 player = int(input('請猜一個數(1~5之間):')) if anwser == player: print('猜對了') else: print('猜錯了QQ') ``` #### 猜數字(猜兩次版本) ``` import random # 引入亂數模組 # 猜數字遊戲,讓玩家猜1~5之間一個數字,然後告知有沒有猜對 anwser = random.randint(1, 5) # 產生1~5之間的隨機整數 player = int(input('請猜一個數(1~5之間):')) if anwser == player: print('猜對了') else: print('猜錯了QQ') ``` #### 多重判斷式 ``` import random # 亂數模組 import datetime # 日期模組 wday = datetime.date.today().weekday() # 0 = 星期一 ~ 6 = 星期日 print(wday) # 測試用 if wday == 0: print('今天是星期一') if wday == 1: print('今天是星期二') if wday == 2: print('今天是星期三') if wday == 3: print('今天是星期四') if wday == 4: print('今天是星期五') if wday == 5: print('今天是星期六') if wday == 6: print('今天是星期日') # 組合 if wday == 0: print('今天是星期一') elif wday == 1: # else if print('今天是星期二') elif wday == 2: print('今天是星期三') elif wday == 3: print('今天是星期四') elif wday == 4: print('今天是星期五') elif wday == 5: print('今天是星期六') elif wday == 6: print('今天是星期日') ``` #### 一個算命程式 ``` import random result = random.randint(1, 5) # 抽籤 if result == 1: print('今天會撿到錢') elif result == 2: print('明天加薪') elif result == 3: print('今天中彩卷') elif result == 4: print('今天摸彩抽到iPhone') else: print('今天踩到大便') ``` #### 還是判斷式 ``` a = 4 b = 9 c = 6 # 判斷式一號 if a > b and b > c: print('ok1') else: print('not ok1') # V # 判斷式二號 if a > b or b > c: print('ok2') # V else: print('not ok2') # 判斷式三號 if a > b > c: # if a > b and b > c: print('ok3') else: print('not ok3') # V # 判斷式四號 # 巢狀判斷式 if a > b: if b > c: print('ok4') else: print('not ok4') # V ``` #### 判斷奇偶數 ``` user = int(input('請輸入一個數字:')) print('你輸入的是:', end='') # if user % 2 == 0: # print('偶數') # else: # print('奇數') print('偶數' if user % 2 == 0 else '奇數') ``` #### 單行if ``` import random # 引入亂數模組 # 猜數字遊戲,讓玩家猜1~5之間一個數字,然後告知有沒有猜對 anwser = random.randint(1, 5) # 產生1~5之間的隨機整數 print(anwser) player = int(input('請猜一個數(1~5之間):')) # if anwser == player: # print('猜對了') # else: # print('猜錯了QQ') print( '猜對了' if anwser == player else '猜錯了QQ' ) ``` #### 取出list元素 ``` a = [1, 2, 3, 4, 5] count = 0 while count < 5: print(a[count]) count += 1 ``` #### 算出list內總和 ``` a = [1, 2, 3, 4, 5] count = 0 while count < 5: print(a[count]) count += 1 ``` while計算過程 ``` # 0, 2, 4, 6, 8 a = 0 s = 0 while a < 10: # 第一次: 第二次 第三次 第四次 第五次 第六次 # a = 0 a = 2 a = 4 a = 6 a = 8 n/a # s = 0 s = 0 s = 2 s = 6 s = 12 # s = s + a (s =0 ) s = 2 s = 6 s = 12 s = 20 print('a=', a, ", s=", s, ', s+=a後:', s + a) s += a # s = s + a, a += 2 print(s) ``` #### 只顯示偶數數字 ``` # 請顯示下面list內偶數數字 a = [33, 45, 67, 88, 100, 23, 9] count = 0 while count < len(a): if a[count] % 2 == 0: print(a[count]) count += 1 ``` #### 快篩世紀剩餘量 ``` import csv with open('Fstdata.csv', encoding='utf8') as csvfile: rows = csv.reader(csvfile) for row in rows: # 一一筆資料取出來 print(row[1], row[7]) ``` ## 課後練習 1. 請問break放在if裡面作用是什麼? **答:** ``` ``` 2. 請問continue放在while裡面的作用是什麼? **答:** ``` ``` 3. 請使用range()函式寫出可以產生0, 2, 4, 6, 8數字的程式碼。 **答:** ``` ``` 4. 請問if內的程式碼為什麼一定要縮排? **答:** ``` ``` 5. while迴圈可以做到的事的都可以使用for-in取代嗎? **答:** ``` ``` 6. 請問else這個關鍵字的用途是什麼? **答:** ``` ``` 7. 如何判斷一個數字是不是偶數? **答:** ``` ``` 8. 如果要寫出一個完整的九九乘法表,會需要幾層的迴圈? **答:** ``` ``` 9. Python有哪幾種迴圈? **答:** ``` ``` 10. 寫一個程式,將['aaron', 'apple', 'amanda']內的每個元素顯示到畫面上,並在每個元素前面顯示他的索引值。 輸出範例: ``` 0: aaron 1: apple 2: amanda ``` **答:** ``` ``` 11. 請問下面迴圈的print()會執行幾次? ``` i = 0 j = 100 while i < j: if i % 10 == 0: break print(i) ``` **答:** ``` ``` 1. 如何刪除List內元素? ``` ``` 2. `['a', 'b', 'c', 'd', 'e', 'f']`清單中,如何用索引切片取得`['d', 'e', 'f']`? ``` ``` 3. 何謂r字串? ``` ``` 4. `print('我' + 18 '歲')`有何錯誤? 該如何修正? ``` ``` 5. 如何建立一個空的set? ``` ``` 6. `3 if 3 > 3 else 3 - 3`結果為何? ``` ``` 7. 單行if有何限制? ``` ``` 8. ``` n = 0 while n > 10: print(n) ``` 該迴圈會執行幾次? ``` ``` 9. 如何取得1~1000之間的隨機整數? ``` ``` 10. 下面哪些是錯誤的變數命名? - `abc` - `_A` - `a123` - `6ab` - `_xo_` - `OK-0` ``` ```