--- ###### tags: `資訊之芽` --- # 1st Stage Review 資芽北區2023 講師: 洪郁凱Kain --- ## Outline - Review Syntax - What's next? --- ## Review Syntax 練習用python內建的manual整理自己的重點 `help(...)` ![](https://i.imgur.com/P2242DO.png =40%x) ---- ## Review Syntax ``` basic if--else list string while for dictionary function import ``` ---- ## Review Syntax basic 加減乘除 ```python # Between int/float/complex # + - * ** / // % # __add__, __sub__,... print(1 + 1, 2 - 1.0, 3 * 3, 4+5j / 3) print(1.2e3 // 3, 5**4, 3%2) # +=, -=, *=, /= a = 5 print(a) a += 3 print(a) ``` ![](https://i.imgur.com/EAu2DWX.png) ---- ## Review Syntax basic 輸入 ```python str_a = input() # 字串 a, b = str_a[:2], str_a[2:] #切下前兩個字分給a,剩下分給b num = int(input()) # 單個數字 num = eval(input()) # 單個數字(可輸入:7.0,1+5j...) strs_a = input().split(" ") # 用" "分隔的多個字 以list裝 n, m = map(int, input().split()) # 兩個數字 nums = list(map(int, input().split())) # 多個數字 以list裝 ``` ---- ## Review Syntax basic 輸出 ```python print(1, 2, 3,sep=', ',end=".") # "1, 2, 3." num = 12 print(f"{num}\t{num*10}\t{num*100}") # 用Tab對齊 print(" "*(5-len(str(num)))+str(num)) # " 12" 向右靠齊 長度為5 print(str(num).rjust(5)) # " 12" 向右靠齊 長度為5 print(f"{num:05}") # "00012" 補0 長度為5 print(str(num).center(5)) # " 12 " 置中 長度為5 ``` <!-- .element: class="wrap-code" --> ---- ## Review Syntax basic 輸出 ```python a=17 print(bin(a), oct(a), hex(a)) # 0b10001 0o21 0x11 print(bin(a)[2:], oct(a)[2:], hex(a)[2:]) # 10001 21 11 ``` --- ## Review Syntax if-elif-else ```python= a = int(input()) if a%3 == 0: print("三的倍數") elif a%3 == 1: print("減一後是三的倍數") else: # 不用條件 print("減二後是三的倍數") ``` ---- ## Review Syntax Boolean ```python # Between int/float print(1.5>0,1<0,1==0) # Between strings lists /sets print("aa">"ab","ba">"abbbbb") # logic: and, or, not, (xor) print(3>2 and 3<2, 3>2 or 3<2, not 3<2) ``` --- ## Review Syntax while (Collatz conjecture) ```python a = 17 while a>1: print(a) if a % 2: a = 3*a + 1 else: a = a // 2 if a == 928: print("講師的生日!") break # 跳出loop # continue ``` --- ## Review Syntax for (讓程式跳舞) ```python for i in range(1,10): if i == 5: continue print(i) ``` --- ## Review Syntax list ```python list_a = [1,4.2,3+4j,"5",lambda x:x%3] # 可以裝幾乎所有東東 print(list("Baby Shark")) # ['B', 'a', 'b', 'y',... print(["Baby Shark"]) # ['Baby Shark'] print(list_a[2:4]) # slice 取index = 2~3的部分 print(list_a[0]) # 取值 從0開始 print(list_a[4](list_a[1])) # 取值 結合 lambda function list_b = [1,5,2,4,3] print(sorted(list_b)) # 會回傳排好的list 但不會更改本來的list list_b.sort(key=list_a[-1]) # key指定排序規則 list可用-1來取值 sort會將本來的list改變 print會噴None print(list_b) ``` ---- ## Review Syntax list 增刪修 ```python # 增 list_a = [i*10 for i in range(3)] # [0, 10, 20] list_a.append(30) # [0, 10, 20, 30] list_a.extend([i*10 for i in range(3,6)]) # [0, 10, 20, 30, 30, 40, 50] # 刪除 list_a.pop(1) # [0, 20, 30, 30, 40, 50] list_a.remove(30) # [0, 20, 30, 40, 50] del list_a[3:] # [0, 20, 30] # 修 list_a[0] = sum(list_a) # [50, 20, 30] ``` ---- ## Review Syntax list(多維度) ```python list_c = [[i*3 for i in range(1,j)] for j in range(10,2,-2)] print(list_c[2][1]) print(sorted(list_c, key=lambda x:x[-1]%5, reverse=True)) # 根據元素[-1]來排列元素 由大到小 list_d = list(range(1,10,3)) # 另一個生大list的方法 sorted(list_c, key=lambda x:len(x))# 根據元素的長度(元素數量)排序 由小到大 ``` <!-- .element: class="wrap-code" --> ---- ## Review Syntax list + for ```python list_d = [[[i*3 for i in range(1,j)] for j in range(10,k,-2)] for k in range(2,5)] for i in list_d: for j in i: print(j) for k in j: print(k) ``` --- ## Review Syntax string ```python print("Kain"+","+"the student") print("Kain"*3) print(("Kain"*2)[2:5]) # 取值的運算子比乘法優先 print(','.join(["a","b","c"])) # 用join把字串黏起來 print(" Kain ".strip()) # 去掉前後空白 ``` ---- ## Review Syntax string 之前的[練習](https://hackmd.io/@s3131212/rkZYT74lh#/11/2) ```python lyrics = """Never gonna give you up Never gonna let you down Never gonna run around and desert you """ positions = [] start = 0 value = "gonna" while True: ret = lyrics.find(value, start) if ret == -1: break positions.append(ret) start = ret + len(value) print(positions) ``` ---- ## Review Syntax string 太多了真的列不完... ```python help(str) ``` --- ## Review Syntax dictionary ```python d = {1:2, 3.0:4, '5':6+7j} # {key: value...} print(len(d)) # Key-Value pairs/元素數量 亦可用在list/str print(d[1], d['3']) # 取值 print(d.get("Dior","不存在!")) # 避免取到不存在的Key # 檢查 key / combo skill if 3 in d: print(d[3]) # 增/刪/修 d[8] = "nine" del d[1] d.pop(8) d['5'] = 1011 ``` ---- ## Review Syntax dictionary 遍歷 ```python for k, v in d.items(): print("key = {}, value = {}".format(k,v)) # format string ``` --- ## Review Syntax function ```python def func_a(a,b,c,/,d,*,e = 3): # 預設值從後向前 print(a,b,c,d,e) return a,c,e,b,e # return自動包成tuple func_a(10,20,30,40,e=50) func_a(10,20,30,e=40,d=50) func_a(*[10*i for i in range(4)]) # 用list餵參數 func_a(*[10*i for i in range(3)],**{"d":123,"e":456}) # list-> args, dict-> kwargs ``` --- ## Review Syntax import ```python import math import numpy as np from random import randint print(math.sin(math.pi/6)) print(np.array([1,2,3])) print(randint(5,10)) ``` --- ## What's next? Debug 的萬用工具 ![](https://i.imgur.com/jGLwM2k.png =50%x) ---- ## What's next? 基本語法完成之後,可在第二階段學習不同套件,達成更多運用 ---
{"metaMigratedAt":"2023-06-17T20:53:43.010Z","metaMigratedFrom":"Content","title":"1st Stage Review","breaks":true,"contributors":"[{\"id\":\"9a14c697-53b2-4214-a12a-512e946e0176\",\"add\":5895,\"del\":490}]"}
    322 views