Last active
September 20, 2021 15:15
-
-
Save mdecourse/c7d4da756f42ed82f472a4eb2ba904e2 to your computer and use it in GitHub Desktop.
Python 範例收集
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # global 可以讓函式中所設定的 x 有效範圍擴及函式外圍 | |
| # 請試著關閉或開啟 myfun() 中的 global x 設定, 就可以了解如何使用 global 關鍵字 | |
| def myfun(): | |
| global x | |
| x = 2 | |
| print(x) | |
| def myfun2(): | |
| print(x) | |
| myfun() | |
| x = 1 | |
| print(x) | |
| myfun() | |
| myfun2() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from browser import document | |
| from browser import html | |
| import string | |
| # 利用 html 建立 canvas 超文件物件 | |
| key_in = html.INPUT(type="text", id="input") | |
| another_div = html.DIV(id="output") | |
| # 頁面中已經有 div 其 id = "brython_div" | |
| # 將此 div 以 document 設定與 brython_div 變數對應 | |
| brython_div = document["brython_div"] | |
| # 將此 input 插入 brython_div | |
| brython_div <= key_in | |
| brython_div <= another_div | |
| # 字串模組中的小寫英文字母字串 | |
| # 'abcdefghijklmnopqrstuvwxyz' | |
| #print(string.ascii_lowercase) | |
| # 利用 list() 將字串轉為字串 | |
| lower_abc_list = list(string.ascii_lowercase) | |
| #print(lower_abc_list) | |
| for i in lower_abc_list: | |
| print(i,ord(i)) | |
| # ord("a") is 97 | |
| # chr(97) is "a" | |
| print(chr(97)) | |
| def keyDown(ev): | |
| # 將 trace 變數與 div id="output" 位置對應 | |
| trace = document["output"] | |
| # 使用者經由事件按下特定按鍵後, 以 chr() 轉為字母 | |
| # 在 output 印出 keyCode 與 keyDown 名稱 | |
| trace.text = f"event {ev.type}, keyCode : {ev.keyCode}" | |
| #ev.stopPropagation() | |
| # 在 input 區域中將事件 keydown 與 keyDown 函式對應 | |
| document["input"].bind("keydown", keyDown) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 一開始 mylist 為空數列, 以中括號標示 | |
| mylist = [] | |
| # thelist 數列內容有奇數與偶數 | |
| thelist = [2, 3, 4, 6, 12, 45, 32] | |
| # 利用 for 迴圈一一取出 thelist 的元素 (element) | |
| for i in thelist: | |
| # 利用 i 除以 2 所得到的餘數為零, | |
| # 判斷是否所取出的數值為偶數 | |
| if i%2 == 0: | |
| # 若 i 為偶數, 則利用 append() 放入 mylist | |
| mylist.append(i) | |
| # 最後印出 mylist 數列內容 | |
| print(mylist) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # thelist 數列內容有奇數與偶數 | |
| thelist = [2, 3, 4, 6, 12, 45, 32] | |
| # 將整數 5 插入 thelist 索引值為 3 的位置 | |
| thelist.insert(3, 5) | |
| # 確認是否按照要求完成資料 insert(index, value) | |
| print(thelist) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # thelist 數列內容有奇數與偶數 | |
| thelist = [2, 3, 4, 6, 12, 45, 32] | |
| # 利用 len() 可以列出 thelist 的長度 (元素個數) | |
| print(len(thelist)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 一開始 mylist 為空數列, 以中括號標示 | |
| mylist = [] | |
| # thelist 數列內容有奇數與偶數 | |
| thelist = [2, 3, 4, 6, 12, 45, 32] | |
| index = 0 | |
| # 利用 for 迴圈一一取出 thelist 的元素 (element) | |
| for i in thelist: | |
| # 利用 i 除以 2 所得到的餘數為零, | |
| # 判斷是否所取出的數值為偶數 | |
| if i%2 == 0: | |
| # 若 i 為偶數, 則利用 append() 放入 mylist | |
| #mylist.append(i) | |
| # 另外一種去除奇數的方法是 | |
| # 當判斷確定該數為偶數後 pass | |
| pass | |
| else: | |
| # 然後在該索引順序中將該奇數"去除"(推出數列) | |
| thelist.pop(index) | |
| index += 1 | |
| # 最後印出 mylist 數列內容 | |
| #print(mylist) | |
| # 最後印出只剩下內容為偶數的數列 | |
| print(thelist) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # i 起始值設為 0 | |
| i = 0 | |
| # 因為 i 變數已經使用, for 迴圈的索引值改用 j | |
| for j in range(10): | |
| # i += 1 其實就是 i = i +1 | |
| # 亦即執行這一行程式之前的 i 對應值, 加上 1 就是執行此行程式後 i 的對應值 | |
| i += 1 | |
| print("j 的值:", j) | |
| print("j 的資料型別:", type(j)) | |
| print("str(j) 的資料型別:", type(str(j))) | |
| # 若要以合併字串的方式列印出 j 的值, 原先型別為整數的 j 就必須透過 str() 轉為字串 (string) | |
| print("j 的值:" + str(j)) | |
| print("i 的值:", i) | |
| print("i 的值:" + str(i)) | |
| # 假如有字串要轉為整數,使用 int() 轉為浮點數 float(), 轉為數列 list(), 轉為集合 set(), 轉為 dict(), 轉為 tuple() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 導入 random 模組 | |
| import random | |
| # 利用 for 重複回圈連續產生包含頭尾 0~10 的隨機整數 | |
| for i in range(10): | |
| # 列出包含 0 與 10 的隨機整數 random integer | |
| print(random.randint(0, 10)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import random | |
| mylist = [1, 2, 3, 4, 5, 6, 7] | |
| # before random.shuffle() | |
| print(mylist) | |
| # after random.shuffle() | |
| random.shuffle(mylist) | |
| print(mylist) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def greeting(name: str) -> str: | |
| return 'Hello ' + name | |
| print(greeting("Static Typing")) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from typing import NewType | |
| UserId = NewType('UserId', int) | |
| some_id = UserId(524313) | |
| def get_user_name(user_id: UserId) -> str: | |
| return user_id | |
| # typechecks | |
| user_a = get_user_name(UserId(42351)) | |
| print(user_a) | |
| # does not typecheck; an int is not a UserId | |
| user_b = get_user_name(-1) | |
| print(user_b) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment