Python- 快快樂樂寫程式 酷學園教學團隊  雨蒼
個人背景 雨蒼 目前為 [email_address] 窗口 完全沒摸過程式語言 曾去巨 x 學過 C++ 但是跟他不熟 netman 老師教的 bash shell 偷看過 python 教學影片 加入 lazybuntu 邊寫邊學
Python 的核心思維 There is only one way to do it. 要做好一件事,一種方法就夠了。 Everything is object. 萬物皆物件 Readability counts. 可讀性優先 Explicit is better than implicit. 明顯比隱晦好 Simple is better than complex. 簡單比複雜好
Python 的 優點 簡單易讀 易於協同開發 ‏ 很快就可以上手 記憶體回收機制 養成良好習慣
Python 的缺點 速度仍然比 C 慢 跨平台 - 使用 Python 模擬器 (PVM) ‏ 有些超級老的 cpu 不能跑 有些模組比較肥 (xml 相關 ) ‏
誰在用 Python Google Youtube BitTorrent NASA OLPC Plurk
 
執行第一個程式 副檔名: py 兩種作法:直譯器和執行 script $ python  開啟互動介面 python *.py #!/usr/bin/python #!/usr/bin/env python chmod a+x *.py ./*.py 中文注意:固定檔案編碼為 UTF-8 # -*- coding: utf-8 -*-
如何寫出第一個程式 打開 IDLE Print "hello, world"
思考:為什麼要加上雙引號?
為什麼要加上雙引號 (&quot;) ‏ $ python print6.py  6 6 到底 6 要當成字看還是要當成數字? 7 Traceback (most recent call last):   File &quot;print6.py&quot;, line 4, in <module>   print &quot;6&quot; + 1 TypeError: cannot concatenate 'str' and 'int' objects
關於變數
數值 (numbers) ‏ 整數 (int) ‏ 浮點 (float) ‏ 長整數 (long) ‏ 八進位與十六進位 複數 (complex) ‏ 布林值 (bool) ‏
數值運算與相關工具 + - * / ** //... 遵守四則運算規則 數學函式: pow abs... Modules : random math... 轉換進制: oct() hex().... from decimal import Decimal Decimal() ‏ 集合 set() ‏
字串 (string) ‏ 字串 (str) ‏ Raw Unicode byte(in Python 3.0) ‏
字串運算與相關工具 + *... len()  slice notation Replace ,Upper....
Slice 0 1 2 3 4 5 [: :] [  起使:終止:步進  ] E C I L S
List [ 'abc', 123 , [ 'a' , 'b' ] ] 可任意巢狀化,不限定型態
List 運算與相關工具 + * len()  subscript and slice notation extend , del , pop , sort range() ‏
Tuple ( 'abc', 123 , [ 'a' , 'b' ] ) ‏ 內含物不可變更之 list
辭典 (Dictionary) ‏ { 'name' : 'billy3321' ,'jobs' : ['student', 'maintainer'], 'develop' : {'name' : 'lazybuntu', 'OS' : 'Ubuntu' }  } Key : Value 無序集合體,以 key 存取 以 hash table 實作
辭典運算與相關工具 keys values items update() pop() del.....
檔案存取 myfile = open('myfile', 'w') myfile.write('hello study area\nhello sa taipei') myfile.close() ‏ myfile = open('myfile', 'r') myfile.readline() myfile.readline() myfile.readline() myfile.readlines() myfile.close
思考:如何儲存變數?
使用 pickle 直接儲存物件 D={'a':1 , 'b':2 , 'c':3} import pickle file = open('datafile.txt', 'w') pickle.dump(D, file) file.close() F = open('datafile.txt', 'r') F.readlines() E = pickle.load(F) print E
思考:為什麼整數,數字不能改?我明明就可以改! 為什麼 List 自己會改變?
為什麼 Python 支援動態定型? 整數,字串是不能改的?我明明就可以改他啊! A = 3 B = A print A , B A = 'hello' print A , B Refrence( 參照值 ) -> object( 物件 ) ‏ 屬性是屬於物件的,而變數就是參照值 [ 'abc' , [(1, 2), ([3],4)],'def']
[ 'abc' , [(1, 2), ([3],4)],'def'] ' abc ' (1, 2) ([3],4) 'd ef ' 1 2 [3] 4 3
共用參照值與複製 L1 = [ 2 , 3 , 4 ] L2 = L1 L1[0] = 24 L1 = [ 2 , 3 , 4 ] L2 = L1[:] L1[0] = 24
小型整數或字串會重複使用 X = 32 Y = 32 X == Y X is Y import sys sys.getrefcount(1) ‏
關於語法
思考:為什麼要縮排?
案例:兩個 if 的故事 if ( x ) if ( y ) statement1; else statement2;  { { { } } } }
Python 語法 多了: 縮排 少了 ( ) { } ; 對你的鍵盤好一點 Readability counts.
if if a > b:   print a, &quot;>&quot;, b elif a < b:   print a, &quot;<&quot;, b else :   print a, &quot;=&quot;, b if not a: 縮排建議固定使用空白
兩個 if 的故事 -Python 篇 if x : if y : statement1 else: statement2   else:   statement2
思考:為什麼要這樣寫程式?
為什麼我一定要這樣寫程式? 可讀性優先 ; readability counts 把習慣養好 方便大家協同作業,大家的程式碼一目瞭然。
While while true:   print &quot;Spam&quot; else   print &quot;sa tainan&quot; while i < 5:   i = i + 1   print i countinue break pass else
For for i in lists:   print i for line in open('file'): 如果要反覆特定次數:搭配 range() for i in range(5)   print &quot;Spam&quot;
函式 def print_sa():   print &quot;Study Area&quot; print_sa() ‏ 利用 return 回傳值 注意:變數所存在之範圍 內建>廣域>函式>區域
函式 引數 廣域變數 檔案 / 串流 區域變數 Return 可變更引數 廣域變數 檔案 / 串流 輸入 輸出
 
模組 找到檔案>編譯為 pyc >執行程式碼建立物件 為了增加速度,模組匯入時會進行編譯。編譯為 pyc 搜尋路徑: sys.path
 
關於說明文件
help() dir() Pydoc 說明文件和程式碼放在一起
網路資源 PyTUG www. python.org.tw irc.://irc.freenode.net/#python.tw Ptt Python 討論版
Python 的核心思維 There is only one way to do it. 要做好一件事,只有一種方法。 Everything is object. 萬物皆物件 Readability counts. 可讀性優先 Explicit is better than implicit. 明顯比隱晦好 Simple is better than complex. 簡單比複雜好 Import this – The Zen of Python
 
 

Python Basic