# 複習 2022 資訊之芽 北區py班 yjrubixcube --- ![](https://i.imgur.com/Uah6bub.jpg) --- # 2022/03/06 ---- ## print ```python= print("hello world") print("Bing", "Chilling", sep="~~~", end="!") ``` ---- ## variables ```python= name = "Rick" age = 56 a, b = 1, 10 a, b = b, a ``` ---- ## input ```python= i = input("input something: ") # str ``` `input`一次就是讀一整行 `input`一次就是讀一整行 `input`一次就是讀一整行 因為還是會有人搞錯所以講三次 ---- ## ~~ntr~~ str ```python= print("1" + "2") print("a" * 10) print('Hello, it\'s me.') print("a\tb\nc") print(f"name: {name}, age: {age}") ``` ---- ## operator ```python= print(2 + 3) print(2 - 3) print(2 * 3) print(2 / 3) print(2 // 3) print(2 % 3) print(2 ** 3) i = 100 i += 1 i -= 1 i *= 3 i /= 4 i //= 4 i %= 4 i **= 2 ``` ---- ## 轉型 ```python= print(int(True)) print(float(1)) print(str(123)) ``` --- # 2022/03/13 ---- ## comparison operators - \> - < - == - \>= - <= - != ---- ## logic operators - and - or - not ---- ## if elif else ```python= if a>b: print("case 1") elif a>c: print("case 2") else: print("case 3") ``` 記得縮排(tab or 4空白) ---- ## nested if ```python= if a>b: if b>c: if c>d: print("pls don't do this") if a>b and b>c and c>d: print("better") ``` ---- ## 酷酷的東西 ```python= condition = False a = 1 if condition else -1 ``` ---- ## while ```python= b = int(input()) i = 1 while i<100: if i == b: break print(i) i += 1 else: print("done") ``` ---- ## while 之會跳舞的乘法表 ```python= i, j = 1, 1 while i<10: if i == 5: i += 1 continue while j<10: if j == 5: j += 1 continue print(f"{i} * {j} = {i*j}", end="\t") j += 1 print() i += 1 ``` --- # 2022/03/20 ---- ## range(start, end, step) ```python= range(10) range(5, 10) range(10, 1, -1) ``` ---- ## for 之會跳舞的程式 ```python= for i in range(10): if i == 5: continue print(i) ``` ---- ## for ```python= b = int(input()) for i in range(10): if i == b: break print(i) else: print("done") ``` ---- ## list + ```python= l = [] l.append(1) l.extend([4, 5, 6]) l.insert(2, 10) ``` ---- ## list - ```python= l = list(range(10)) l.pop(3) l.remove(3) l.clear() ``` ---- ## list method ```python= l = list(range(10)) l.count(1) l.index(7) l.sort() l.reverse() c = l.copy() ``` ---- ## list function ```python= min(l), max(l), sum(l) sorted(l) map(int, l) ``` map回傳的是map object,要把他當list用的話要轉型 --- # 2022/03/27 ---- ## 更多string ```python= s = 'abcdefg' print(s[2:5]) print(s[-1]) print("gg" in s) r = s.replace("abc", "zzz") print(r) ``` ---- ## string比大小 ```python= print("abc" < "abd") print("abc" < "abcd") print("abc" != "def") ``` ---- ## string method ```python= "ABCD1234".isdigit() "ABCD1234".isalpha() "ABCD1234".isalnum() s = ' aaabbcDEFG ' print(s.strip()) print(s.upper()) print(s.lower()) print(s.find("aa")) i = '1 2 3 4 5' print(i.split()) l = ['1', '2', '3', '4', '5'] print("".join(l)) ``` ---- ## dictionary ```python= d = {"shaochun": 100, "rick": 99} print(d["shaochun"]) d["new_person"] = 87 print(d.get("nobody")) d.pop("new_person") d.clear() ``` key 只能放immutable, 沒有排序 ---- ## dictionary for ```python= for k in d.keys(): do something for v in d.values(): do something for k, v in d.items(): do something ``` --- # 2022/04/10 ---- ## function ```python= def func(a, b): global z a += 1 b = 3 z = 100 return a * b x, y, z = 7, 8, 9 print(x, y, z) print(func(x, y)) print(x, y, z) ``` ---- ## function 之[recursion](https://hackmd.io/@VLvbo_-_QjqwJnUcuKdxSQ/r1LyyItEq#/6/2) ```python= def fact(n): if n == 0: return 1 return n * fact(n - 1) ``` --- # 2022/04/17 ---- ## module ```python= import math import numpy as np from math import gcd from math import gcd as ggccXDDDD ```
{"metaMigratedAt":"2023-06-16T23:18:41.218Z","metaMigratedFrom":"YAML","title":"複習","breaks":true,"contributors":"[{\"id\":\"54bbdba3-ffbf-423a-b026-751cb8a77149\",\"add\":4313,\"del\":414}]"}
    308 views