3 Python非正式導引
在本節的例子中,以提示符>>>, … ,開始的是輸入,否則為輸出, #後為python的注釋 Table of Contents 1 把Python當作計算機 1.1 數字 1.2 字串 1.3 Unicode 編碼的字串 1.4 List 2 初步程式設計 1 把Python當作計算機 1.1 數字
首先進入互動模式,將Python當作計算機
$ pythonPython 2.7.3 (default, Aug 1 2012, 05:16:07) [GCC 4.6.3] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> 1 + 23>>> 1 - 2-1>>> 2 * 36>>> 2 / 40>>> 2 / 0Traceback (most recent call last): File "<stdin>", line 1, in <module>ZeroDivisionError: integer division or modulo by zero>>> (1+2)*39
還可以給變數賦值,然後參與運算
>>> x = 3>>> y = 4>>> (x + y) * 535
還可以連續賦值,但不能使用沒有定義過的變數
>>> x = y = z = 3.4>>> x3.4>>> y3.4>>> cTraceback (most recent call last): File "<stdin>", line 1, in <module>NameError: name 'c' is not defined>>> z3.4
同時,Python還支援複數,可以用 a + bj,或者complex(a,b)表示
>>> 1 + 2j(1+2j)>>> 2j * 3j(-6+0j)>>> complex(1+2j) * complex(1-2j)(5+0j)
同時,還可以將複數賦給變數,然後提取實部,虛部
>>> x = (3+4j)>>> x.real3.0>>> x.imag4.0
float(),int(),可以將值分別轉換為浮點型和整型,但對複數不適用,複數可以用abs()得出其絕對值,或稱為模。
>>> int(3.4)3>>> int(3.5)3>>> float (3)3.0>>> a = 1 + 2j>>> a(1+2j)>>> float(a)Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: can't convert complex to float>>> abs(a)2.23606797749979>>> abs(3+4j)5.0
互動模式下可以用 _ 代表上次計算的結果
>>> 1 + 2 3>>> 4 + _7>>> 5 * _35
1.2 字串
除了數學外,Python還可以操作字串,注意逸出字元,單引號,雙引號,試幾個例子,體會一下
>>> 'hello,python''hello,python'>>> "hello,python"'hello,python'>>> 'Don't' File "<stdin>", line 1 'Don't' ^SyntaxError: invalid syntax>>> 'Don\'t'"Don't">>> "Dont't""Dont't">>> "Don\'t""Don't"
字串可以分為多行,並用print函數列印
>>> hello = "This is a string which is \n\... used to test multi\... lines.">>> print helloThis is a string which is used to test multilines.
注意分行符號\n,以及多行編輯\這兩個符號的使用。
也可以用三引號,這樣就不必寫這兩個逸出字元。
>>> hello = """... Usage: thingy [OPTIONS]... -h Display this usage message... -H hostname Display host name... """>>> print helloUsage: thingy [OPTIONS]-h Display this usage message-H hostname Display host name
如果不需要逸出字元,即\n就表示\n,不表示轉義,可以以r作用字串起始
>>> hello = r"this is \n a test line">>> print hellothis is \n a test line>>> hello = "this is \n a test line">>> print hellothis is a test line
字串可以被串連,甚至可以被“乘”(其實也是串連啦~)
>>> words = 'A' + ' hello ' + 'B'>>> print wordsA hello B>>> '<' + words*3 + '>''<A hello BA hello BA hello B>'
字串也可以被提取,下標從0開始,下標可以為負數,表示倒數 str[i:j]表示從str下標i到下標(j-1),共j-i個字元。 str[_:3]表示str[0:3] str[2:_ ]表示下標2開始到最後
>>> words = "hello">>> words[0]'h'>>> words[0:3]'hel'>>> words[1:2]'e'>>> words[:3]'hel'>>> words[2:]'llo'>>> words="hello">>> words[-1]'o'>>> words[-2]'l'
字串中字母不能被修改,只能再造字串
>>> words'hello'>>> words[0] = 'a'Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: 'str' object does not support item assignment>>> words="hi">>> words'hi'
字串長度可用len函數得到
>>> words="hello">>> len(words)5
1.3 Unicode 編碼的字串
使用u首碼來定義Unicode 編碼的字串 使用unicode函數將其它形式編碼轉化為unicode形式 使用encode可以將unicode形式編碼轉化為其它形式
>>> u'Hello python !'u'Hello python !'>>> unicode('\xc3\xa4\xc3\xb6\xc3\xbc', 'utf-8')u'\xe4\xf6\xfc'>>> u"äöü".encode('utf-8')'\xc3\xa4\xc3\xb6\xc3\xbc'
1.4 List
Python有一系列資料結構,可以用於將多個值組合在一起,其中最多才多藝的要數List
>>> a = ['spam', 3, 4.5, "hi"]>>> a['spam', 3, 4.5, 'hi']>>> a[0]'spam'>>> a[1]3>>> a[-1]'hi'>>> a[1:-1][3, 4.5]>>> 2*a[:3] + ['xxx']['spam', 3, 4.5, 'spam', 3, 4.5, 'xxx']
與字串不同,List的各元素可以被改變
>>> a['spam', 3, 4.5, 'hi']>>> a[1] = 6>>> a['spam', 6, 4.5, 'hi']
同時List還可以方便地進行插入,替換等操作
>>> a['spam', 6, 4.5, 'hi']>>> a[1:1] = ['xxx','yyy']>>> a['spam', 'xxx', 'yyy', 6, 4.5, 'hi']>>> a = ['x','y','z']>>> a['x', 'y', 'z']>>> a[0:2] = ['a','b']>>> a['a', 'b', 'z']
len同樣可得到List的元素個數
>>> a['a', 'b', 'z']>>> len(a)3
2 初步程式設計
從上一節可以看出,Python相當地靈活易用,下面開始進入程式設計~ 第一個例子,來自著名的Fibonacci數列:
>>> # Fibonacci series ... # the sum of two elements defines the next... a, b = 0, 1>>> while b < 10:... print b... a, b = b, a+b... 112358
上面的程式很簡單,不細說,從中也可以體會到python編寫的程式簡潔易懂,短小精悍 需要注意的是while程式塊沒有像c,java那些以括弧包圍,而是使用了縮排 如果不想在輸出時換行,只需要在print b後加逗號~
>>> a, b = 0, 1>>> while b < 10:... print b,... a, b = b, a+b... 1 1 2 3 5 8
作為一個python初學者,我只能說,我的心已經深深地跌進了湖水裡…….
註:原文:http://docs.python.org/2/tutorial/introduction.html