Python 101

本文是初學 Python 遇到的一些問題留下的筆記:

一開始進入 Python 的世界,除了程式語言本身外還有許多基本的工具要先了解:

Python 2 V.S Python 3 : python 語言的版本easy_install V.S PIP : 套件安裝工具virtualenv : 虛擬工作環境Python 2 V.S Python 3

剛進行 Python 的世界,一開始就要面臨的抉擇,就是要用 Python 2 或 Python 3,因為是初學者,沒有任何包袱,當然是選 Python 3。 (也有人建議一律用2.7的語法撰寫,這樣可以同時相容於2跟3)

Mac Os 10.8 (mountain lion)預裝的是 Python 2.7.2,要自行昇級成 Python 3.x (目前 2013/07/07 3.3.2是 release 版)可以透過在 Mac OS 的terminal 下執行 python -V 取得版號

Easy_install V.S PIP

一般的 lib 會提供 python setup.py install

easy_install 跟 pip 兩者都是套件安裝工具, easy_install 沒有反安裝套件的功能,而 pip 有反安裝套件的功能,所以建議是裝pip

Mac Os 10.8 (mountain lion) 只有預裝 easy_install,要使用 pip,可以透過 easy_install 來安裝 pip安裝 pip 的指令如下 $ easy_install pip,如果無法安裝的話,可以直接抓 source 下來裝下載的url為 :

必須先安裝setuptools

$ curl -O https://bitbucket.org/pypa/setuptools/raw/0.8/ez_setup.py$ python ez_setup.py --user 

才能再裝 pip

$ curl -O https://pypi.python.org/packages/source/p/pip/pip-1.3.tar.gz$ tar xzf pip-1.3.tar.gz$ cd pip-1.3$ sudo python setup.py install

裝完後檢查一下執行一下 pip 指令,如果不能執行,可能是Path沒設好,Path變數是在 ~/.bash_profile 如果沒這個檔,可以自已新增一個

export PATH=/System/Library/Frameworks/Python.framework/Versions/3.3/bin:/Users/kent/Library/Python/3.3/bin:$PATH

pip 的使用方式

pip list   列出目前安裝的libpip search 搜尋libpip install 安裝lib   

ex:

sudo pip install beautifulsoup4   # 安裝 beautifulsoup lib (一個 html parser)   

Testing

python的 test framework有好幾套,可以自行選擇適合的

unittest 內建的,不用另外再抓libnosetest 可獨立執行某個 test method 或一整個 test suite 的功能doctest 測試 python doc 的邏輯初學者必備技能

python Interpreter (直譯器)直譯式語言的優點就是會有直譯器可以用,在直譯器執行程式,可以立即得到執行的結果,在不了解API的用法時,我通常會在執譯器上先做測試,或者直接用 unittest 寫Learning Test。平常沒事也可以掛著查 API doc

help([object])列出說明,會說明該 object 有那些 methods 及使用方式

type(variable)取得 object 的型別,撰寫弱型別語言最大的痛苦就是不知道變數是什麼型別,這 type 至少可以在runtime取得變數的型別dir() 列出 object 內所有的 methodspprint() 列出物件結構Python Language連算子沒有增量 ++,減量 -- 運算子+=,-= 這是合法的運算子除法用/ , 整數除法用 //一個*是乘,兩個*是幂(次方)英文為power 2 * 3 =6 , 2 ** 3 = 8字串字串可用單引號或雙引號,單引號較常用字串前面加 r 表示為 raw string, ex: raw = r’this is a \t\n raw string’字串前面加 u 表示為 unicode string, ex: ustring = u’A unicode \u018e string \xf1’條件式結構

 if speed >= 80:    print 'License and registration please'    if mood == 'terrible' or speed >= 100:      print 'You have the right to remain silent.'    elif mood == 'bad' or speed >= 90:      print "I'm going to have to write you a ticket."      write_ticket()    else:      print "Let's try to keep it under 80 ok?"

較簡潔的語法

if speed >= 80: print 'You are so busted'else: print 'Have a nice day'         

python不是用 {},也不是用begin/end,而是用縮排跟冒號:來做為區塊結構的範圍不是用 else if 而是 elifList, Tuple, Dictionary

a = [ 1, 3.4, 'hello' ] # A listb = ( 10, 20, 30 )      # A tuple   c = { 'a': 3, 'b': 42 } # A dictionary

list 可視為 array,語法如下

list = [‘larry’, ‘curly’, ‘moe’]list = [1, 2, 3.4, ‘hello’, ‘word’] # 可以放不同類型的內容

list 還有一個很特別的一點是可以直接在 list 裡執行 expression,叫 List Comprehensions,語法為 [expr for var in list ]

範例:

nums = [1, 2, 3, 4]squares = [ n * n for n in nums ]   ## [1, 4, 9, 16]

tuple 很像 list,但tuple是不可變的,語法如下 tuple = (1, 2, 'hi')

a = ()      # 0-tuple (empty tuple) b = (item,) # 1-tuple (note the trailing comma) c = item,   # 1-tuple (note the trailing comma)tuple裡的值,可以容易再被獨立指派成變數,ex:full_name = ('kent','chiu')first_name, last_name = full_name # 把full_name的兩個值指派給 first_name, last_nameprint(first_name)                 # kentprint(last_name)                  # chiu

TBDyielddecorator (@staticmethod)list Ellipsisclass method (__xxx__)學習資源https://developers.google.com/edu/python/ – Google Python Coursehttp://getpython3.com/diveintopython3 – Dive Into Python 3 免費電子書http://chimera.labs.oreilly.com/books/1234000000754/index.html – Test-Driven Web Development with Pythonhttp://chimera.labs.oreilly.com/books/1230000000393/index.html – orally python cookbook (問題-解法導向線上電子書)http://docs.python.org/2/library/ – python 標準函式庫文件

Python 101

相关文章:

你感兴趣的文章:

标签云: