Python 快速入門(一)

- 中國WEB開發者網絡 (http://www.webasp.net)
-- 技術教程 (http://www.webasp.net/article/)
--- Python 快速入門(一) (http://www.webasp.net/article/8/7722.htm)
-- 作者:未知
-- 發佈日期: 2003-09-13
原作者:xina

Python概要

Python是一門解釋性的、面向對象的、動態語義特徵的高層語言。它的高層次的內建數據結構,以及動態類型和動態綁定,這一切使得它非常適合於快速應用開發,也適合於作為膠水語言連接已有的部件。Python的簡單而易於閱讀的語法強調了可讀性,因此降低了程序維護的費用。Python支持模塊和包,並鼓勵程序模塊化和代碼重用。Python的解釋器和標準擴展庫的源碼和二進制格式在各個主要平台上都可以免費得到,而且可以免費分發。

通常,程序員愛上Python是因為它能增加生產力。由於沒有編譯過程,編輯-測試-調試週期相當快。調試Python程序很簡單:一個錯誤永遠不會導致一個段錯誤。當解釋器發現錯誤時,它就引發一個異常。當程序沒有捕捉到異常,解釋器就打印一個堆棧跟蹤。一個源碼級調試器允許我們檢查局部和全局變量,計算表達式,設置斷點,單步跟蹤等等。調試器是用Python寫的,這證明了Python的能力。另外,最快的調試程序的方法是增加幾條打印語句:快捷的編輯-測試-調試週期使得這個簡單的辦法十分有效。

基本的運算式

我們直接切入正題,直接簡單的教你使用 Python。 我假設讀者己有其它語言的基礎,可以直接切入語法重點。


1 a = 0
2 b = 7
3 a = a + 1
4 a = b * a
5 print a

結果顯示 : 7



上面就是 python 的簡單例子,相信如果你學過其它的語言(如 C/C++, Java),就能很容易的瞭解。

A+B A 加 B
A-B A 減 B
A*B A 乘 B
A/B A 除 B
A%B 取 A/B 的餘數(如 8 % 3 == 2)
-A 取 A 的反數( 若 A == 7, -A == -7)
String



1 a = 'hello'
2 b = "world"
3 c = a + ' ' + b + '!!'
4 print c

結果顯示 : hello world!!



string 可以使用 ' or " 符號括起來表示。在行 3,是合併四個 string object 的例子, 將四個 string 依順連接成單一的 string。


1 a = '%s=%d' % ('test', 16)
2 print a

結果顯示 : test=16



類似於 C/C++ 的 printf 或 sprintf,在行 1,python 提供 string format 的功能。 字串 '%s=%d' 指定 string 的 format,而後在字串後接著 % 然後是 format 的參數, 這些參數會依序取代 format 裡的 %s 和 %d。%s 代表要取代字串,%d 則是取代成整數。


a = 'This is a rather long string containing\n\
several lines of text just as you would do in C.\n\
Note that whitespace at the beginning of the line is\
significant.\n'



string 可以延伸到數行,但在每一行的最後必需要有escape \ 以忽略掉 newline。 另外也可以使用 """ 或 '''


a = '''This is a rather long string containing
several lines of text just as you would do in C.
Note that whitespace at the beginning of the line is
significant.'''



使用 ''' 或 """ 就不需要在每一行結數時 escape,但 newline 會被包含入 string 內容。

List



1 a = []
2 a[0] = 'aoo'
3 a[1:3] = [10, 11]
4 b = [1, 2, 3, 'foo']
5 print a, b, b[:3], b[1:]

結果顯示 : [9, 10, 11] [1, 2, 3, 'foo'] [1, 2, 3] [2, 3, 'foo']



上面是 list 的使用範例。list 是一個 sequence data type, 類於 C/C++ 的 array, 但 array 是 fixed length 而 list 不是, 其長度是可以隨時改變的。行 1 就 bind a 為一個空的 list。 行 2 則指定 index 0 為 'aoo' string object。行 3 為 list 的 slice 的使用範例。 將 index 1 和 index 3 之間的 item(index 1 和 2) 代換成 10 和 11。行 5 的 b[:3] 則相當於 b[0:3], 而 b[1:] 相當於 b[1:4]。list 內的 item 不需是相同的 type, 如上例在一個 list object 裡可以同時包含整數和 string 不同 type 的 item。


1 a = [1, 2, 3] + [4, 5, 6]
2 print a

結果顯示 : [1, 2, 3, 4, 5, 6]



和 string 一樣,list 也可以進行合併的動作(concatenate)。

slice



1 a = [ 1, 2, 3, 4, 5]
2 print a[3], a[3:], a[:2], a[:-1], a[-2]

結果顯示 : 4 [4, 5] [1, 2] [1, 2, 3, 4] 4



index 可為負數,負數則從 list 的結束位置往回數。

list 的 index 可以比作下圖,每一個 item 指 list 裡的一個 object。而 | 則為 item 之間的分界, 其下方則為其 index。


| item 0 | item 1 | item 2 | .... | item n |
0 1 2 3 n n+1
-n -n+1 -n+2 -n+3 -1



當指定 item k (0 <= k < n+1), 則 index 設為 item k 的前一個分界 index k。 當指定 item k ~ item p (0 <= k <= p < n+1) 時,則 slice 指定為 [k:p+1] (意指 index k 和 index p+1 兩個分界之間所有的 item) 或 [k:p-n](注: p < n, p - n < 0 為負數)。

Methods for List



1 a = []
2 a.append(1)
3 a.append(2)
4 a.insert(1, 3)
5 print a

結果顯示 : [1, 3, 2]



上面是 list 的 append() 和 insert() 兩個 method 的使用範例,append 用以新增一個 item 到 list 的最後面。 insert 用以在指定的位置插入一個新的 item。行 4即在 list 的 index 1 的位置(即 item 0 和 item 1 之間)插入一個新 item。


1 a = [1, 3, 2]
2 a.sort()
3 print a,
4 a.reverse()
5 print a

結果顯示 : [1, 2, 3] [3, 2, 1]



sort() method 能將 list 進行從小到大的排序,reverse() method 則進行從大至小的反向排序。

webasp.net