# 1-3 Python Basic --- # Before Beginning ---- ### 把手伸出來 ![](https://i.imgur.com/dVRy77X.jpg) ---- ## 試試看 ``` print(0.1 + 0.2) ``` ---- ## 聲明 --- # 傳教開始 ---- - BBC電視劇——《蒙提·派森的飛行馬戲團》(Monty Python's Flying Circus)的愛好者 ---- # Zen of Python ### Python 之禪 ---- ``` import this ``` [wiki的翻譯](https://zh.wikipedia.org/wiki/Python%E4%B9%8B%E7%A6%85) --- # 程式運行 ---- 1. 程式語言 2. 0101011000001 3. 運算 4. 得到執行結果 --- # 介紹完Python後 --- # Outline ---- - 各種print - 註解 - 變數概念 - 賦值 - 運算子 - 少數幾種type - input --- # Output ---- 1. 可以是什麼形式? 2. 可以藉由什麼裝置? ---- ## 最簡單的output ## 函式print --- ## print #### beginning! ---- ``` print("hello world") ``` ---- ### 等等,那函式(functions)是什麼? ---- #### 你可以把它當作一段特別的關鍵字 - 功用: - 執行特定工作 - 修改參數、引數,重複使用 - 易懂 ---- # 繼續print!! ---- - 以下哪些是會出現error的呢? ``` print("hello" ) ``` ``` print ("hello") ``` ``` print("hello"); ``` ``` print "hello" ``` ``` print(hello) ``` ---- ## answer:第四個、第五個 ---- ``` print("starburst" "stream") ``` ``` print("starburst", "stream") ``` ![](https://i.imgur.com/6g3uV2m.jpg =30%x) ---- ``` print("starburst", "stream",sep="!!") ``` ---- # end ---- ``` print("star", end="") print("burst", end=" ") print("stream", end="!") ``` ---- # 結合 ``` print("starburst", "stream",sep="!!",end=" RRRRRRR") ``` ---- # Question and Practice - 什麼時候會用到end?什麼時候會用到sep? - 玩玩看 sep 和 end --- ## print ### 跳脫字元 ---- ## 試著印出以下這段話 ``` "Gura" is a shark ``` ![](https://i.imgur.com/d8fJzpN.png =50%x) ---- ### 透過反斜線,讓後面的符號、字母有其他意義 ``` print("\"Gura\" is a shark") ## 讓gura前後的引號可以顯現 ``` ``` print("nnn\nnn") ## \n 會換行 ``` ``` print("ttt\ttt") ## \t 會一次空一個tab ``` ``` print("xyz\bc") ## b是backspace,會刪掉前一個字元 ``` ``` print("xyzxyz\rabc") ``` ---- ``` \n 是換行的字元 ``` ``` print(r'\n 是換行的字元') ``` ---- # Question and Practice - 用r會不會有哪裡出問題? - 什麼時候會用到\r? ---- # 同一個引號會出問題! ``` print(r'hello \nworld') ``` ``` print(r'hello 'world') ``` ---- # 計時器 ``` import time for i in range(9): i = 9 - i i = str(i) print("\r" + i,end="秒") time.sleep(1) print("\r" + "0 秒",end="") print("\nHappy New Year!!!!") ``` ---- ``` import time s = "sprout" l = len(s) for i in range(l): print("\r" + s[:l-1-i] + "_", end="") # print(".",end="") time.sleep(1) print("______") ``` --- # 註解(comment) ---- ## 井字號(#) ## 頭尾三個引號(''' or """) ``` #print("hello") ``` ``` ''' print("hello") ''' ``` ``` """ print("hello") """ ``` ---- ### 功能 - 補充說明 - 不想刪除程式碼 ---- # Question --- ### 變數(variable)&& 賦值(=) ---- ## 什麼是變數 ![](https://i.imgur.com/Cq37naJ.jpg =50%x) ---- ### 可以想像成把值(value)丟到箱子裡 ---- # 直接看code! ---- ``` name = "海夢" print(name) print(name, "我__") ``` ![](https://i.imgur.com/4ccuD7H.jpg =50%x) ---- # 賦值(assign) ### = ? ### == ? ---- ``` name = "海夢" ``` - 功能: 1. 新增一個叫做 "name" 的變數 2. 把「海夢」這個字串指派給 "name" 這個變數 ---- ## 不用宣告變數型態喔 ---- # 變數名稱的規則 ---- - 變數名稱的開頭必須是字母或者底線,不能以數字開頭 - 變數名稱只能包含字母、數字與底線 (A-z, 0-9, _ ) - 變數名稱的大小寫不一樣則被視為不同變數 (age, Age 和 AGE 是三個不一樣的變數) - 不能是if,else,while等關鍵字 - 不要用函數名稱當變數名字 ---- ### 哪些變數名稱不合法 - zen-of-python - print - 111 - (1) - -.- - ?! - _abc ---- ### print 和 _abc 都是可以的,但都不建議使用 ---- # Question --- # 運算子 ---- ``` print(1+1) ``` ---- ``` a = 5 print(-a) ``` ---- [更多運算子](https://www.w3schools.com/python/python_operators.asp) ---- print("hello" + "world") ---- # Question and Practice - 用逗號和 sep 達到和下面程式碼一樣的效果 ``` print("hello" + "world") ``` --- # Data Type ---- # type() ---- ``` print(type(1)) ``` ``` print(type(1.0)) ``` ``` print(type("sprout")) ``` ---- # 什麼是 Data Type ---- - 舉例:int, float, string - 不同資料型別能做的事會不太一樣 - 就算對他們做同樣的事,也可能會有不同的結果 - 變數會以不同的資料型態儲存資料 ---- ### try! ``` a = 1 + 1 b = 1 + 1.0 c = "10" + "0" d = "hello" + "world" e = ["a","b"] + ["c","d"] print(type(print)) print(type(type)) print(type(int)) ``` ### 小小偷渡 ---- ### 型別轉換 ``` a = "1" a = int(a) ``` ``` b = 1 b = str(b) ``` ---- ### try ``` int(1.5) int("sprout") ``` ---- - 取一個數的小數可以怎麼做呢? ---- ``` a = 2.5 b = int(a) print(a - b) ``` ---- # Question --- # input ---- - 可以輸入什麼? - 可以用什麼設備輸入 ---- ## 有完整的輸入輸出,就可以達到互動的效果 ---- ``` a = input("可寫可不寫的提示訊息") ``` ``` name = input("輸入你的名字: ") print("hello,", name) ``` ---- Question and Practice ### 完成它 ``` Hi, [name]. You're [age] years old next year. ``` ---- ``` name = input() age = int(input()) age = age + 1 # age += 1 age_str = str(age) print("Hi, " + name + ". You will be " + age_str + " years old next year.") print("Hi, " + name + ". You will be" , age ,"years old next year.") ``` --- # 結語 ---- 綜合練習 & 作業 [neoj 3007](https://neoj.sprout.tw/problem/3007/) [neoj 3008](https://neoj.sprout.tw/problem/3008/) [neoj 3009](https://neoj.sprout.tw/problem/3009/) ---- # Thanks
{"metaMigratedAt":"2023-06-16T20:33:21.031Z","metaMigratedFrom":"Content","title":"1-3 Python Basic","breaks":true,"contributors":"[{\"id\":\"67f64c5d-de88-4a53-9b64-c2cfb2bffa58\",\"add\":5738,\"del\":1004}]"}
    650 views