Python入門指引

Python是比較熱的語言,我的興趣參考官方文檔:https://docs.python.org/2/tut...
邊看邊翻譯了一下(後面會持續更新),有興趣的能夠看看。javascript

Python具備高級的數據結構,能高效地面向對象編程。它的優雅的語法、動態類型、解釋屬性使它能快速地在大部分平臺開發應用程序。它的第三方包、工具和庫均可以在這裏找到:https://www.python.org/html

Python很容易經過C/C++函數或數據類型進行擴展。寫擴展參看:擴展指導C/C++的Python APIjava

更多文檔參看Python標準庫Python的APIpython

經過這個入門教程,你將學習到Pyton的一些經常使用特性和標準庫的一些方法或模塊。linux

寫在前面

對於天天在電腦面前工做的人來講,可能你須要作一些自動化的工做,好比批量替換,查找更改等,或者你想寫一些界面程序,遊戲等。git

而對於專業的軟件開發人員來講,經過C/C++/Java庫去開發
、測試、編譯等又太繁瑣了。算法

這時候,Python是最佳的選擇。shell

你能夠寫一些shell腳本或者bat腳本去移動文件或者修改數據,但不是很適合寫界面程序或者遊戲。C/C++/Java又太繁瑣了。而Python比較適合快速的在各個平臺(windows, Mac, Unix)完成你要的工做。express

Python是一門真正的編程語言,它有高級的數據結構,好比可靈活操做的數組和字典。編程

Python是模塊化的,它的標準庫裏面有你可用的一些模塊,這些模塊提供了好比文件I/O,sockets等功能。

Python能夠寫出比C/C++/Java更簡潔的程序,緣由有三:
1 有高級的數據結構
2 經過縮進進行區分聲明塊而不是括號
3 不須要變量或者參數聲明

Python是可擴展的。你能夠把Python解釋器嵌入到C程序中。

使用Python解釋器

運行python解釋器

方式1:
Python解釋器在linux下通常安裝在/usr/local/bin/python目錄下,切換到該目錄下,輸入如下命令能夠運行python解釋器

python

windows下通常安裝在C:Python27目錄下,win+r快捷鍵,而後輸入一下cmd,打開dos界面,輸入如下命令,設置環境變量:

set path=%path%;C:\python27

則任意目錄下,輸入python命令便可打開python解釋器
方式2:

python -c command [arg] ..

由於命令行可能有一些特殊字符或者空白,最好用單引號引發來。
退出python解釋器:
Unix下在命令行界面輸入快捷鍵:Control-D, Windows下輸入Control-Z。
或者輸入:

quit()

調用python模塊(模塊也就是python源文件):

python -m module [arg] ...

進入交互命令模式:

python -i ...

參數傳遞

命令行的參數會賦值給sys模塊的argv變量。能夠經過import sys訪問參數。argv的長度至少有1。當沒有參數的時候,sys.argv[0]是一個空串。當腳本的名字爲"-",則sys.argv[0]是"-",當用了-c命令,則sys.argv[0]的值爲"-c"。當用了-m,sys.argv[0]的值爲模塊的名字。-c和-m後面的參數,python解釋器不會處理。

交互模式

多行模式前面是... 單行是>>>

>>> the_world_is_flat = 1
>>> if the_world_is_flat:
...     print "Be careful not to fall off!"
...

解釋器和環境

設置代碼編碼

通常狀況是不用設置的 默認爲utf-8

#!/usr/bin/env python
# -*- coding: cp-1252 -*-

Python介紹

開頭標識註釋,>>>和...開頭標識python語句

>>>
>>> #這是註釋
... a = 1;#這是註釋
>>> print a
1

把python當作計算器

數字

>>> 2 + 2
4
>>> 50 - 5*6
20
>>> (50 - 5.0*6) / 4
5.0
>>> 8 / 5.0
1.6

這裏2是int 5.0是float,/的結果是什麼類型是根據參與計算的兩個數,若是有一個數是float則返回float類型。
//操做符是向下取數,小數位爲0。

>>> 12//7.0
1.0
>>>

%是求餘
**是屢次方

>>> 5**2
25
>>>

聲明變量n=12
若是使用一個未聲明的變量會報錯

>>> n
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'n' is not defined
>>>

多項式計算,會自動進行數據類型的轉換:int和float一塊兒計算,int會自動轉爲float

交互模式下,最後一個打印的變量會賦值給_

>>> tax = 12.5 / 100
>>> price = 100.50
>>> price * tax
12.5625
>>> price + _
113.0625
>>> round(_, 2)
113.06

_是隻讀的,不能被賦值。

字符串

單引號或者雙引號裏表示字符串,用來轉義
若是不想要轉義:字符串前加一個r

>>> print 'C:\some\name'  # here \n means newline!
C:\some
ame
>>> print r'C:\some\name'  # note the r before the quote
C:\some\name

多行字符串:三個"""或者'''

print """\
Usage: thingy [OPTIONS]
     -h                        Display this usage message
     -H hostname               Hostname to connect to
"""

標識去掉換行,沒有輸出是這樣的:

>>> print """
... aef
... asdf
... """

aef
asdf

字符串拼接:+ 字符串重複:*

>>> "un"*2 +" awef"
'unun awef'
>>>

自動拼接:

>>> 'Py' 'thon'
'Python'

獲取字符串的單個字符

>>> a = "python"
>>> a[0]
'p'

負數標識從尾部開始讀取: -0等於0 最後一個字符是-1

>>> a = "python"
>>> a[-1]
'n'
>>>

取區間:

>>> a = "python"
>>> a[0:2]
'py'
>>> a[2:]
'thon'
>>> a[:4]
'pyth'
>>> a[-2:]
'on'
>>>

越界訪問數組會報錯:

>>> word[42]  # the word only has 6 characters
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: string index out of range

可是取不存在的區間不會報錯:

>>> a[-2:45]
'on'
>>>

字符串沒法被修改:

>>> word[0] = 'J'
  ...
TypeError: 'str' object does not support item assignment
>>> word[2:] = 'py'
  ...
TypeError: 'str' object does not support item assignment
unicode字符串

支持unicode字符串:字符串前加u

>>> u'Hello World !'
u'Hello World !'
>>> u'Hello\u0020World !'
u'Hello World !'

0x0020標識空格
支持原始模式: 字符串前面加入ur
python的默認編碼方式是:ASCII碼,若是Unicode字符串被打印或者寫到文件或者是str()方法轉化都會默認轉爲ASCII碼,若是字符串不在0-127範圍就會報錯

>>> u"abc"
u'abc'
>>> str(u"abc")
'abc'
>>> u"äöü"
u'\xe4\xf6\xfc'
>>> str(u"äöü")
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)

轉換爲特定編碼:方法的參數爲小寫

>>> u"äöü".encode('utf-8')
'\xc3\xa4\xc3\xb6\xc3\xbc'

數組

定義一個數組

>>> squares = [1, 4, 9, 16, 25]
>>> squares
[1, 4, 9, 16, 25]

獲取數組內元素

>>> squares[0]  # indexing returns the item
1
>>> squares[-1]
25
>>> squares[-3:]  # slicing returns a new list 例子三
[9, 16, 25]

獲取數組內片斷,好比上面例子三,會返回一個新的數組拷貝,原數組不會發生改變
數組合並:

>>> squares + [36, 49, 64, 81, 100]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

字符串的內容是不能被更改的,而數組是能夠被更改的:

>>> cubes = [1, 8, 27, 65, 125]  # something's wrong here
>>> 4 ** 3  # the cube of 4 is 64, not 65!
64
>>> cubes[3] = 64  # replace the wrong value
>>> cubes
[1, 8, 27, 64, 125]

給數組添加元素:

>>> cubes.append(216)  # add the cube of 6
>>> cubes.append(7 ** 3)  # and the cube of 7
>>> cubes
[1, 8, 27, 64, 125, 216, 343]

能夠賦值給截取的數組:

>>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> letters
['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> # replace some values
>>> letters[2:5] = ['C', 'D', 'E']
>>> letters
['a', 'b', 'C', 'D', 'E', 'f', 'g']
>>> # now remove them
>>> letters[2:5] = []
>>> letters
['a', 'b', 'f', 'g']
>>> # clear the list by replacing all the elements with an empty list
>>> letters[:] = []
>>> letters
[]

獲取數組的長度:

>>> letters = ['a', 'b', 'c', 'd']
>>> len(letters)
4

數組的元素也能夠是一個數組:

>>> a = ['a', 'b', 'c']
>>> n = [1, 2, 3]
>>> x = [a, n]
>>> x
[['a', 'b', 'c'], [1, 2, 3]]
>>> x[0]
['a', 'b', 'c']
>>> x[0][1]
'b'

終於開始編程了!

如何實現一個斐波那契:

>>> # 這是一個註釋
... a, b = 0, 1  #分別給a賦值爲0 b賦值爲1 
>>> while b < 10:#這是一個循環
...     print b  #打印b的值(而且這裏的代碼前面有空格(也就是行縮進))
...     a, b = b, a+b #a賦值爲b,b賦值爲a+b的和
...
1
1
2
3
5
8

以前說過,行縮進標識接下來是一個代碼塊。
print方法,能夠控制格式,好比增長空格:

>>> i = 256*256
>>> print 'The value of i is', i
The value of i is 65536

在print語句最後加一個逗號,避免打印結果換行:

>>> a, b = 0, 1
>>> while b < 1000:
...     print b,
...     a, b = b, a+b
...
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987

控制流

if語句

>>> x = int(raw_input("Please enter an integer: "))
Please enter an integer: 42
>>> if x < 0: #冒號能夠開啓多行模式
...     x = 0
...     print 'Negative changed to zero'
... elif x == 0:
...     print 'Zero'
... elif x == 1:
...     print 'Single'
... else:
...     print 'More'
...

if…elif..else(不是必須的)…

for

Python的for能夠遍歷數組和字符串(這個和C語言的for語句有略微不一樣)

>>> # Measure some strings:
... words = ['cat', 'window', 'defenestrate']
>>> for w in words:
...     print w, len(w)
...
cat 3
window 6
defenestrate 12

在循環內修改一個數組:首先經過截取數組的方法對原數組進行拷貝(這個知識點以前有提過)

>>> for w in words[:]:  # words[:]能夠對原數組進行拷貝
...     if len(w) > 6:
...         words.insert(0, w)
...
>>> words
['defenestrate', 'cat', 'window', 'defenestrate']
range()函數

range函數能根據算法建立一個數組

>>> range(5, 10) #建立全部元素爲5到10區間並遞增的數組
[5, 6, 7, 8, 9]
>>> range(0, 10, 3)#遞增3
[0, 3, 6, 9]
>>> range(-10, -100, -30)#遞減30
[-10, -40, -70]

遍歷數組的索引:

>>> a = ['Mary', 'had', 'a', 'little', 'lamb']
>>> for i in range(len(a)):
...     print i, a[i]
...
0 Mary
1 had
2 a
3 little
4 lamb

退出循環

break語句執行會退出裏層的for循環;continue會跳事後面語句的執行(和C語言用法同樣)。

>>> for n in range(2, 10):
...     for x in range(2, n):
...         if n % x == 0:
...             print n, 'equals', x, '*', n/x
...             break
...     else:
...         # loop fell through without finding a factor
...         print n, 'is a prime number'
...
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3

pass語句

pass語句不會作任何事,只是用來佔位用的

>>> class MyEmptyClass:
...     pass
...
>>> def initlog(*args):
...     pass   # Remember to implement this!
...

定義函數和調用函數

>>> def fib(n):    # def關鍵字標識定義函數,這裏函數名爲fib
...     """Print a Fibonacci series up to n.""" #
...     a, b = 0, 1
...     while a < n:
...         print a,
...         a, b = b, a+b
...
>>> # Now call the function we just defined:
... fib(2000)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597

注意函數體要代碼要縮進
函數能夠被賦值(fib會被加入符號表):

>>> fib
<function fib at 10042ed0>
>>> f = fib # f也會被加入符號表
>>> f(100)
0 1 1 2 3 5 8 13 21 34 55 89

即使函數沒有return,也會返回一個None

>>> fib(0)
>>> print fib(0)
None

return後面沒有跟任何東西也是返回None

>>> def fib2(n):  # return Fibonacci series up to n
...     result = []
...     a, b = 0, 1
...     while a < n:
...         result.append(a)    # 這裏是調用數組的append方法
...         a, b = b, a+b
...     return result
...
>>> f100 = fib2(100)    # call it
>>> f100                # write the result
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

定義帶參數的函數

參數帶默認值:retries的默認值爲4

def ask_ok(prompt, retries=4, complaint='Yes or no, please!'):
    while True: # True是關鍵字
        ok = raw_input(prompt) #raw_input是內置的函數,用於IO輸入
        if ok in ('y', 'ye', 'yes'):
            return True
        if ok in ('n', 'no', 'nop', 'nope'):
            return False
        retries = retries - 1
        if retries < 0:
            raise IOError('refusenik user') # raise是關鍵字 拋出異常
        print complaint

默認值能夠爲變量:i是一個變量

i = 5

def f(arg=i):
    print arg

i = 6
f()

默認參數若是是一個可變的對象,會被賦值屢次:

def f(a, L=[]):
    L.append(a)
    return L

print f(1)
print f(2)
print f(3)
會打印出:

[1]
[1, 2]
[1, 2, 3]

若是你不想L被改變,你能夠這麼作:

def f(a, L=None):
    if L is None:
        L = []
    L.append(a)
    return L

若是隻接受一個參數,可是傳遞了兩個參數會報錯:

>>> def function(a):
...     pass
...
>>> function(0, a=0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: function() got multiple values for keyword argument 'a'
**kewords接收字典參數:

def cheeseshop(kind, *arguments, **keywords):
    print "-- Do you have any", kind, "?"
    print "-- I'm sorry, we're all out of", kind
    for arg in arguments:
        print arg
    print "-" * 40
    keys = sorted(keywords.keys()) #按字典順序 
    for kw in keys:
        print kw, ":", keywords[kw]

*arg接受不肯定個數的參數:

def write_multiple_items(file, separator, *args):
    file.write(separator.join(args))

自動解析參數:

>>> range(3, 6)             # 正常狀況調用方式
[3, 4, 5]
>>> args = [3, 6]
>>> range(*args)            # 從一個數組裏解析參數
[3, 4, 5]
>>> def parrot(voltage, state='a stiff', action='voom'):
...     print "-- This parrot wouldn't", action,
...     print "if you put", voltage, "volts through it.",
...     print "E's", state, "!"
...
>>> d = {"voltage": "four million", "state": "bleedin' demised", "action": "VOOM"}
>>> parrot(**d)
-- This parrot wouldn't VOOM if you put four million volts through it. E's bleedin' demised !

文檔字符串:

>>> def my_function():
...     """Do nothing, but document it.
...
...     No, really, it doesn't do anything.
...     """
...     pass
...
>>> print my_function.__doc__
Do nothing, but document it.

    No, really, it doesn't do anything.

Lambda表達式一個匿名函數,lambda a, b: a+b. a和b是兩個參數,結果返回a和b的和:

>>> def make_incrementor(n):
...     return lambda x: x + n
...
>>> f = make_incrementor(42)
>>> f(0)
42
>>> f(1)
43

lambda也能夠做爲參數傳遞:

>>> pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]
>>> pairs.sort(key=lambda pair: pair[1])
>>> pairs
[(4, 'four'), (1, 'one'), (3, 'three'), (2, 'two')]

編碼格式建議

不用Tab縮進,用4倍空格縮進
必要時換行(避免單行超出79個字符)
用空格區分函數或者類或者函數內部的一大段代碼
代碼前面加上必要的註釋
用文檔字符串
操做符liagn兩邊或者逗號後面必須空格
函數採用lower_case_width_underscore方式命令,類用駝峯(CanekCase)方式命名;老是用self看成類的第一個方法的參數
不要用特殊的編碼格式(ASCII是兼容全部的)

數據結構

數組

python數據默認有一些經常使用方法:好比append, extend, insert等等

做爲堆棧使用

>>> stack = [3, 4, 5]
>>> stack.append(6)
>>> stack.append(7)
>>> stack
[3, 4, 5, 6, 7]
>>> stack.pop()
7
>>> stack
[3, 4, 5, 6]
>>> stack.pop()
6
>>> stack.pop()
5
>>> stack
[3, 4]

做爲隊列使用

>>> from collections import deque
>>> queue = deque(["Eric", "John", "Michael"])
>>> queue.append("Terry")          # Terry arrives
>>> queue.append("Graham")          # Graham arrives
>>> queue.popleft()                # The first to arrive now leaves
'Eric'
>>> queue.popleft()                # The second to arrive now leaves
'John'
>>> queue                          # Remaining queue in order of arrival
deque(['Michael', 'Terry', 'Graham'])

一些經常使用的方法

filter(function, sequence) : 返回function的值爲true的全部值

>>> def f(x): return x % 3 == 0 or x % 5 == 0
...
>>> filter(f, range(2, 25))
[3, 5, 6, 9, 10, 12, 15, 18, 20, 21, 24]

map(function, sequence): 返回處理後的值

>>> def cube(x): return x*x*x
...
>>> map(cube, range(1, 11))
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]

傳遞兩個數組: 分別從一個數組裏取出一個數 返回相加後的結果

>>> seq = range(8)
>>> def add(x, y): return x+y
...
>>> map(add, seq, seq)
[0, 2, 4, 6, 8, 10, 12, 14]

reduce(function, sequence) :把數組的第一個和第二個參數想加的和和第三個數再加。。若是數組爲空,會返回異常

>>> def add(x,y): return x+y
...
>>> reduce(add, range(1, 11))
55

reduce能夠指定開始的第一個數的索引:

>>> def sum(seq):
...    def add(x,y): return x+y
...    return reduce(add, seq, 0)
...
>>> sum(range(1, 11))
55
>>> sum([])
0

建立數組的幾種形式:

>>> squares = []
>>> for x in range(10):
...    squares.append(x**2)
...
>>> squares
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
squares = [x**2 for x in range(10)]
squares = map(lambda x: x**2, range(10))

更復雜點的例子:x,y做爲一個總體 必須加上括號

>>> [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]

更多例子:

>>> freshfruit = ['  banana', '  loganberry ', 'passion fruit  ']
>>> [weapon.strip() for weapon in freshfruit]
['banana', 'loganberry', 'passion fruit']
>>> [x, x**2 for x in range(6)]
  File "<stdin>", line 1, in <module>
    [x, x**2 for x in range(6)]
              ^
SyntaxError: invalid syntax
>>> # flatten a list using a listcomp with two 'for'
>>> vec = [[1,2,3], [4,5,6], [7,8,9]]
>>> [num for elem in vec for num in elem]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> from math import pi
>>> [str(round(pi, i)) for i in range(1, 6)]
['3.1', '3.14', '3.142', '3.1416', '3.14159']

二維數組

>>> matrix = [
...    [1, 2, 3, 4],
...    [5, 6, 7, 8],
...    [9, 10, 11, 12],
... ]

複雜點的例子:

>>> [[row[i] for row in matrix] for i in range(4)]
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]

至關於:

>>> transposed = []
>>> for i in range(4):
...    transposed.append([row[i] for row in matrix])
...
>>> transposed
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]

刪除數組內元素:del

>>> a = [-1, 1, 66.25, 333, 333, 1234.5]
>>> del a[0]
>>> a
[1, 66.25, 333, 333, 1234.5]
>>> del a[2:4]
>>> a
[1, 66.25, 1234.5]
>>> del a[:]
>>> a
[]

刪除整個數組:

>>> del a

新類型:元組。

輸入能夠加括號,也能夠不加。輸出都是帶括號的。

>>> t = 12345, 54321, 'hello!'  # 輸入 沒加括號
>>> t[0]
12345
>>> t
(12345, 54321, 'hello!')  # 輸出 帶括號
>>> # Tuples may be nested:
... u = t, (1, 2, 3, 4, 5)
>>> u
((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))
>>> # 沒法被修改
... t[0] = 88888
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> # 內部的元素能夠是可變的類型 好比數組等
... v = ([1, 2, 3], [3, 2, 1])
>>> v
([1, 2, 3], [3, 2, 1])

空元組和只有一個元素的元組:

>>> empty = ()
>>> singleton = 'hello',    # <-- note trailing comma
>>> len(empty)
0
>>> len(singleton)
1
>>> singleton
('hello',)

逆序元素:

>>> t = (12345, 54321, 'hello!')
>>> x, y, z = t

新的類型:集合

建立空集合:set()

>>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
>>> fruit = set(basket)              # 建立集合
>>> fruit
set(['orange', 'pear', 'apple', 'banana'])
>>> 'orange' in fruit                # 測試是否oranage是不是集合fruit內部
True
>>> 'crabgrass' in fruit
False

集合a, b 之間的交集 並集

>>> a = set('abracadabra')
>>> b = set('alacazam')
>>> a                                  # unique letters in a
set(['a', 'r', 'b', 'c', 'd'])
>>> a - b                              # letters in a but not in b
set(['r', 'd', 'b'])
>>> a | b                              # letters in either a or b
set(['a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'])
>>> a & b                              # letters in both a and b
set(['a', 'c'])
>>> a ^ b                              # letters in a or b but not both
set(['r', 'd', 'b', 'm', 'z', 'l'])
>>> a = {x for x in 'abracadabra' if x not in 'abc'}
>>> a
set(['r', 'd'])

新的類型:字典

字典是根據key索引的,而key數據類型能夠爲數字或者字符串,元組的元素都是不可變的,也能夠做爲key。數組不能做爲key,由於數組可被修改

>>> tel = {'jack': 4098, 'sape': 4139}
>>> tel['guido'] = 4127
>>> tel
{'sape': 4139, 'guido': 4127, 'jack': 4098}
>>> tel['jack']
4098
>>> del tel['sape']
>>> tel['irv'] = 4127
>>> tel
{'guido': 4127, 'irv': 4127, 'jack': 4098}
>>> tel.keys()
['guido', 'irv', 'jack']
>>> 'guido' in tel
True

dict方法直接建立字典:

>>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
{'sape': 4139, 'jack': 4098, 'guido': 4127}
>>> {x: x**2 for x in (2, 4, 6)}
{2: 4, 4: 16, 6: 36}
>>> dict(sape=4139, guido=4127, jack=4098)
{'sape': 4139, 'jack': 4098, 'guido': 4127}

遍歷

經過enumerate方法

>>> for i, v in enumerate(['tic', 'tac', 'toe']):
...    print i, v
...
0 tic
1 tac
2 toe

一次性遍歷多個(這個特性不錯。。

>>> questions = ['name', 'quest', 'favorite color']
>>> answers = ['lancelot', 'the holy grail', 'blue']
>>> for q, a in zip(questions, answers):
...    print 'What is your {0}?  It is {1}.'.format(q, a)
...
What is your name?  It is lancelot.
What is your quest?  It is the holy grail.
What is your favorite color?  It is blue

逆序遍歷:reversed

>>> for i in reversed(xrange(1,10,2)):
...    print i
...
9
7
5
3
1

對數組排序(sorted方法),而後遍歷:

>>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
>>> for f in sorted(set(basket)):
...    print f
...
apple
banana
orange
pear

遍歷字典的時候,得到key和value:

>>> knights = {'gallahad': 'the pure', 'robin': 'the brave'}
>>> for k, v in knights.iteritems():
...    print k, v
...
gallahad the pure
robin the brave

遍歷的時候改變一個數組:

>>> import math
>>> raw_data = [56.2, float('NaN'), 51.7, 55.3, 52.5, float('NaN'), 47.8]
>>> filtered_data = []
>>> for value in raw_data:
...    if not math.isnan(value):
...        filtered_data.append(value)
...
>>> filtered_data
[56.2, 51.7, 55.3, 52.5, 47.8]

更多條件語句

比較運算符:
in和not in判斷是否在序列裏面; is和is not用來比較兩個對象是不是同一個對象;
比較能夠鏈式: a < b == c 判斷a小於b,而且b等於c
布爾操做符:and和or 優先級比比較運算符低 not優先級最高 or最低

布爾運算符,當一個知足條件不會繼續下面的計算

比較結果能夠被賦值:

>>> string1, string2, string3 = '', 'Trondheim', 'Hammer Dance'
>>> non_null = string1 or string2 or string3
>>> non_null
'Trondheim'

模塊

退出解釋器後,全部聲明的函數或者變量都不存在了。因此咱們須要建立一個python腳本,可持續地運行。每個腳本文件稱之爲一個模塊。
好比咱們建立一個文件:fibo.py

# 這是一個模塊

def fib(n):    # 定義函數fib
    a, b = 0, 1
    while b < n:
        print b,
        a, b = b, a+b

def fib2(n):  # 定義函數fib2
    result = []
    a, b = 0, 1
    while b < n:
        result.append(b)
        a, b = b, a+b
    return result

在解釋器裏面導入這個模塊:

>>> import fibo

訪問模塊的函數:

>>> fibo.fib(1000)
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
>>> fibo.fib2(100)
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
>>> fibo.__name__
'fibo'

函數賦給一個變量

>>> fib = fibo.fib
>>> fib(500)
1 1 2 3 5 8 13 21 34 55 89 144 233 377

執行模塊腳本

這樣運行一個模塊

python fibo.py <arguments>

和導入一個模塊,而且把__name__設置爲__main__是同樣的:至關於把下面的代碼放到模塊的底部

if __name__ == "__main__":
    import sys
    fib(int(sys.argv[1]))

這樣單純導入是不會運行這個腳本的:

>>> import fibo
>>>

模塊尋找路徑

1 內置模塊
2 搜索sys.path裏面的全部目錄

sys.path初始化的內容:
當前目錄
PYTHONPATH (目錄路徑,它和環境變量的PATH語法一致)
Python安裝路徑

sys.path會被修改,當前目錄優先於標準庫路徑。

編譯後的Python文件

若是 spam.pyc(編譯後的Python文件)和spam.py共存,會優先用編譯後的文件。spam.pyc保存的編譯時間和spam.py的修改時間不一致,則編譯後的文件會被忽略(也就是用spam.py文件)。spam.pyc文件是平臺獨立的,也就是說能被各個平臺共享。

標準模塊

Python有本身的標準庫。爲了達到更底層的東西,有些模塊已經內置到解析器中。並且有些內置模塊依賴於環境,好比winreg模塊只在window環境下提供。而一個值得關注的模塊是:sys,它被內置到了每一個Python解釋器中。sys.ps1和sys.ps2表示Python的提示符

>>>import sys
>>>sys.ps1
'>>> '
>>> sys.ps2
'... '
>>> sys.ps1 = 'C> '
C> print 'Yuck!'
Yuck!
C>

sys.path的值是解釋器的模塊搜索路徑。咱們能夠增長路徑:

>>> import sys
>>> sys.path.append('/ufs/guido/lib/python')

dir()函數

dir()函數返回一個字符串的數組,它被用來表示一個模塊定義了哪些名字

>>> import fibo, sys
>>> dir(fibo)
['__name__', 'fib', 'fib2']
>>> dir(sys)  
['__displayhook__', '__doc__', '__excepthook__', '__name__', '__package__',
'__stderr__', '__stdin__', '__stdout__', '_clear_type_cache',
'_current_frames', '_getframe', '_mercurial', 'api_version', 'argv',
'builtin_module_names', 'byteorder', 'call_tracing', 'callstats',
'copyright', 'displayhook', 'dont_write_bytecode', 'exc_clear', 'exc_info',
'exc_traceback', 'exc_type', 'exc_value', 'excepthook', 'exec_prefix',
'executable', 'exit', 'flags', 'float_info', 'float_repr_style',
'getcheckinterval', 'getdefaultencoding', 'getdlopenflags',
'getfilesystemencoding', 'getobjects', 'getprofile', 'getrecursionlimit',
'getrefcount', 'getsizeof', 'gettotalrefcount', 'gettrace', 'hexversion',
'long_info', 'maxint', 'maxsize', 'maxunicode', 'meta_path', 'modules',
'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'ps1',
'py3kwarning', 'setcheckinterval', 'setdlopenflags', 'setprofile',
'setrecursionlimit', 'settrace', 'stderr', 'stdin', 'stdout', 'subversion',
'version', 'version_info', 'warnoptions']

不帶參數則返回當前你定義的模塊函數變量名字

>>> a = [1, 2, 3, 4, 5]
>>> import fibo
>>> fib = fibo.fib
>>> dir()
['__builtins__', '__name__', '__package__', 'a', 'fib', 'fibo', 'sys']

dir()不會返回內置的函數和變量。若是要打印內置的話,須要傳遞__builtin__

>>> import __builtin__
>>> dir(__builtin__)  
['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException',
'BufferError', 'BytesWarning', 'DeprecationWarning', 'EOFError',
'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FloatingPointError',
'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning',
'IndentationError', 'IndexError', 'KeyError', 'KeyboardInterrupt',
'LookupError', 'MemoryError', 'NameError', 'None', 'NotImplemented',
'NotImplementedError', 'OSError', 'OverflowError',
'PendingDeprecationWarning', 'ReferenceError', 'RuntimeError',
'RuntimeWarning', 'StandardError', 'StopIteration', 'SyntaxError',
'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'True',
'TypeError', 'UnboundLocalError', 'UnicodeDecodeError',
'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError',
'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning',
'ZeroDivisionError', '_', '__debug__', '__doc__', '__import__',
'__name__', '__package__', 'abs', 'all', 'any', 'apply', 'basestring',
'bin', 'bool', 'buffer', 'bytearray', 'bytes', 'callable', 'chr',
'classmethod', 'cmp', 'coerce', 'compile', 'complex', 'copyright',
'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval',
'execfile', 'exit', 'file', 'filter', 'float', 'format', 'frozenset',
'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input',
'int', 'intern', 'isinstance', 'issubclass', 'iter', 'len', 'license',
'list', 'locals', 'long', 'map', 'max', 'memoryview', 'min', 'next',
'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit',
'range', 'raw_input', 'reduce', 'reload', 'repr', 'reversed', 'round',
'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super',
'tuple', 'type', 'unichr', 'unicode', 'vars', 'xrange', 'zip']

包是組織Python模塊的一種方式,好比A.B 標識包A下有一個子模塊B。
一個包的結構相似以下:

sound/                          Top-level package
      __init__.py              Initialize the sound package
      formats/                  Subpackage for file format conversions
              __init__.py
              wavread.py
              wavwrite.py
              aiffread.py
              aiffwrite.py
              auread.py
              auwrite.py
              ...
      effects/                  Subpackage for sound effects
              __init__.py
              echo.py
              surround.py
              reverse.py
              ...
      filters/                  Subpackage for filters
              __init__.py
              equalizer.py
              vocoder.py
              karaoke.py
              ...

那麼咱們怎麼導入它:

import sound.effects.echo

而後怎麼引用: 必須用全名引用

sound.effects.echo.echofilter(input, output, delay=0.7, atten=4)

另一種引用的方式是:

from sound.effects import echo

這種方式引入,能夠避免全名

echo.echofilter(input, output, delay=0.7, atten=4)

固然,你也能夠引入函數或者變量:

from sound.effects.echo import echofilter

直接調用函數:

echofilter(input, output, delay=0.7, atten=4)

注意:如下方式導入,最後一個(subsubitem)必須是包

import item.subitem.subsubitem

導入包

在sound/effects/__init__.py 這個裏面定義:

__all__ = ["echo", "surround", "reverse"]

那麼:經過如下方式會導入上面all指定的模塊

from sound.effects import *

若是all沒定義,那麼import導入的狀況是不必定的。

import sound.effects.echo
import sound.effects.surround
from sound.effects import *

好比上面這種寫法,會導入echo和surround

不推薦使用*。

內部包引用

可使用相對導入:

from . import echo
from .. import formats
from ..filters import equalizer

輸入和輸出

輸出格式

輸出方法:sys.stdout標準輸出, print write()方法等
格式輸出: str.format()
轉爲字符串用repr()和str()函數 :

>>> s = 'Hello, world.'
>>> str(s)
'Hello, world.'
>>> repr(s)
"'Hello, world.'"
>>> str(1.0/7.0)
'0.142857142857'
>>> repr(1.0/7.0)
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr(x) + ', and y is ' + repr(y) + '...'
>>> print s
The value of x is 32.5, and y is 40000...
>>> # The repr() of a string adds string quotes and backslashes:
... hello = 'hello, world\n'
>>> hellos = repr(hello)
>>> print hellos
'hello, world\n'
>>> # The argument to repr() may be any Python object:
... repr((x, y, ('spam', 'eggs')))
"(32.5, 40000, ('spam', 'eggs'))"

打印表格形式:

>>> for x in range(1, 11):
...    print repr(x).rjust(2), repr(x*x).rjust(3),
...    # Note trailing comma on previous line
...    print repr(x*x*x).rjust(4)
...
1  1    1
2  4    8
3  9  27
4  16  64
5  25  125
6  36  216
7  49  343
8  64  512
9  81  729
10 100 1000

>>> for x in range(1,11):
...    print '{0:2d} {1:3d} {2:4d}'.format(x, x*x, x*x*x)
...
1  1    1
2  4    8
3  9  27
4  16  64
5  25  125
6  36  216
7  49  343
8  64  512
9  81  729
10 100 1000

str.rjust() 對字符串右對齊
str.zfill() 字符串保證位數

>>> '12'.zfill(5)
'00012'
>>> '-3.14'.zfill(7)
'-003.14'
>>> '3.14159265359'.zfill(5)
'3.14159265359'

str.format()的基本使用:

>>> print 'We are the {} who say "{}!"'.format('knights', 'Ni')
We are the knights who say "Ni!"

交換位置:

>>> print '{0} and {1}'.format('spam', 'eggs')
spam and eggs
>>> print '{1} and {0}'.format('spam', 'eggs')
eggs and spam

經過key訪問:

>>> print 'This {food} is {adjective}.'.format(
...      food='spam', adjective='absolutely horrible')
This spam is absolutely horrible.

混合使用:

>>> print 'The story of {0}, {1}, and {other}.'.format('Bill', 'Manfred',
...                                                    other='Georg')
The story of Bill, Manfred, and Georg.

'!s' (調用str()) and '!r' (調用repr()) 打印前進行格式轉換:

>>> import math
>>> print 'The value of PI is approximately {}.'.format(math.pi)
The value of PI is approximately 3.14159265359.
>>> print 'The value of PI is approximately {!r}.'.format(math.pi)
The value of PI is approximately 3.141592653589793.

':' 可控制小數點:

>>> import math
>>> print 'The value of PI is approximately {0:.3f}.'.format(math.pi)
The value of PI is approximately 3.142.

控制表格:

>>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 7678}
>>> for name, phone in table.items():
...    print '{0:10} ==> {1:10d}'.format(name, phone)
...
Jack      ==>      4098
Dcab      ==>      7678
Sjoerd    ==>      4127

經過[]訪問key:

>>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
>>> print ('Jack: {0[Jack]:d}; Sjoerd: {0[Sjoerd]:d}; '
...        'Dcab: {0[Dcab]:d}'.format(table))
Jack: 4098; Sjoerd: 4127; Dcab: 8637678

% 操做符也能夠格式化(老式的)

>>> import math
>>> print 'The value of PI is approximately %5.3f.' % math.pi
The value of PI is approximately 3.142.

讀寫文件

open()打開文件:open(filename, mode)

>>> f = open('workfile', 'w')
>>> print f
<open file 'workfile', mode 'w' at 80a0960

‘b’標識二進制形式,跨平臺

文件對象的方法

讀文件:f.read(size)

>>> f.read()
'This is the entire file.\n'
>>> f.read()
''

帶換行:

>>> f.readline()
'This is the first line of the file.\n'
>>> f.readline()
'Second line of the file\n'
>>> f.readline()
''

讀取一個文件的全部行:

>> for line in f:
        print line,

This is the first line of the file.
Second line of the file

或者list(f) or f.readlines()
字符串寫入文件:

>>> f.write('This is a test\n')

將其餘類型寫入文件需先轉爲字符串:

>>> value = ('the answer', 42)
>>> s = str(value)
>>> f.write(s)

f.tell() 返回一個整數,表示當前文件的位置(計算字節)。好比:

>>> f = open('workfile', 'r+')
>>> f.write('0123456789abcdef')
>>> f.seek(5)      # 到第6個字節
>>> f.read(1)
'5'
>>> f.seek(-3, 2)  # 倒數(2表示倒數)第三個字節位置
>>> f.read(1)
'd'

釋放文件資源:

>>> f.close()
>>> f.read()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: I/O operation on closed file

最佳實踐是帶上with:即使有異常拋出也能釋放文件資源

>>> with open('workfile', 'r') as f:
...    read_data = f.read()
>>> f.closed
True

保存JSON數據

序列化:將json轉爲字符串 反序列化:將字符串轉爲json

>>> import json
>>> json.dumps([1, 'simple', 'list'])
'[1, "simple", "list"]'

將對象序列化到一個文件中:f是一個文件對象

json.dump(x, f)

從文件中讀取:

x = json.load(f)

錯誤和異常

語法錯誤

>>> while True print 'Hello world'
  File "<stdin>", line 1
    while True print 'Hello world'
                  ^
SyntaxError: invalid syntax

執行異常

>>> 10 * (1/0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero
>>> 4 + spam*3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'spam' is not defined
>>> '2' + 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects

處理異常

>>> while True:
...    try:
...        x = int(raw_input("Please enter a number: "))
...        break
...    except ValueError:
...        print "Oops!  That was no valid number.  Try again..."
...

這裏只捕獲了ValueError,若是捕獲更多異常:

... except (RuntimeError, TypeError, NameError):
...    pass
import sys

try:
    f = open('myfile.txt')
    s = f.readline()
    i = int(s.strip())
except IOError as e:
    print "I/O error({0}): {1}".format(e.errno, e.strerror)
except ValueError:
    print "Could not convert data to an integer."
except:
    print "Unexpected error:", sys.exc_info()[0]
    raise

try...except...else..else後面的代碼必定會執行

for arg in sys.argv[1:]:
    try:
        f = open(arg, 'r')
    except IOError:
        print 'cannot open', arg
    else:
        print arg, 'has', len(f.readlines()), 'lines'
        f.close()

拋出異常:

>>> try:
...    raise Exception('spam', 'eggs')
... except Exception as inst:
...    print type(inst)    # 異常實例
...    print inst.args      # arguments 存儲在.args中
...    print inst          # __str__方法使得能直接打印參數而不須要引用它。
...    x, y = inst.args
...    print 'x =', x
...    print 'y =', y
...
<type 'exceptions.Exception'>
('spam', 'eggs')
('spam', 'eggs')
x = spam

函數內部的異常也能捕獲:

>>> def this_fails():
...    x = 1/0
...
>>> try:
...    this_fails()
... except ZeroDivisionError as detail:
...    print 'Handling run-time error:', detail
...
Handling run-time error: integer division or modulo by zero

拋出異常

>>> raise NameError('HiThere')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: HiThere

不處理異常,直接將異常拋出:

>>> try:
...    raise NameError('HiThere')
... except NameError:
...    print 'An exception flew by!'
...    raise
...
An exception flew by!
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
NameError: HiThere

自定義異常

>>> class MyError(Exception):
...    def __init__(self, value):
...        self.value = value
...    def __str__(self):
...        return repr(self.value)
...
>>> try:
...    raise MyError(2*2)
... except MyError as e:
...    print 'My exception occurred, value:', e.value
...
My exception occurred, value: 4
>>> raise MyError('oops!')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
__main__.MyError: 'oops!'

重寫了__init__方法,而且定義了一個value屬性。
通常定義異常的方式是這樣的:

class Error(Exception):
    """Base class for exceptions in this module."""
    pass

class InputError(Error):
    """Exception raised for errors in the input.

    Attributes:
        expr -- input expression in which the error occurred
        msg  -- explanation of the error
    """

    def __init__(self, expr, msg):
        self.expr = expr
        self.msg = msg

class TransitionError(Error):
    """Raised when an operation attempts a state transition that's not
    allowed.

    Attributes:
        prev -- state at beginning of transition
        next -- attempted new state
        msg  -- explanation of why the specific transition is not allowed
    """

    def __init__(self, prev, next, msg):
        self.prev = prev
        self.next = next
        self.msg = msg

清理工做

>>> try:
...    raise KeyboardInterrupt
... finally:
...    print 'Goodbye, world!'
...
Goodbye, world!
KeyboardInterrupt
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>

finally老是會被執行,並且若是異常沒有被處理的話,在finally裏面的代碼執行完後會被從新拋出。
一個更復雜點的例子:

>>> def divide(x, y):
...    try:
...        result = x / y
...    except ZeroDivisionError:
...        print "division by zero!"
...    else:
...        print "result is", result
...    finally:
...        print "executing finally clause"
...
>>> divide(2, 1)
result is 2
executing finally clause
>>> divide(2, 0)
division by zero!
executing finally clause
>>> divide("2", "1")
executing finally clause
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in divide
TypeError: unsupported operand type(s) for /: 'str' and 'str'

預清理

必定要帶上with保證文件資源被釋放

with open("myfile.txt") as f:
    for line in f:
        print line,

Python類

定義類

class MyClass:
    """A simple example class"""
    i = 12345

    def f(self):
        return 'hello world'

引用類的屬性和方法

MyClass.i和 MyClass.f
MyClass.__doc_ => 「A simple example class」

定義init方法,設置默認屬性

def __init__(self):
    self.data = []

傳遞給類的參數會傳遞給init:

>>> class Complex:
...    def __init__(self, realpart, imagpart):
...        self.r = realpart
...        self.i = imagpart
...
>>> x = Complex(3.0, -4.5)
>>> x.r, x.i
(3.0, -4.5)

類共享變量和實例變量

class Dog:

    kind = 'canine'        # 類共享變量

    def __init__(self, name):
        self.name = name    # 實例變量

>>> d = Dog('Fido')
>>> e = Dog('Buddy')
>>> d.kind                  # shared by all dogs
'canine'
>>> e.kind                  # shared by all dogs
'canine'
>>> d.name                  # unique to d
'Fido'
>>> e.name                  # unique to e
'Buddy'

注意引用變量避免被全部實例共享

class Dog:

    tricks = []            # mistaken use of a class variable

    def __init__(self, name):
        self.name = name

    def add_trick(self, trick):
        self.tricks.append(trick)

>>> d = Dog('Fido')
>>> e = Dog('Buddy')
>>> d.add_trick('roll over')
>>> e.add_trick('play dead')
>>> d.tricks                # 這裏tricks被全部實例共享了
['roll over', 'play dead']

正確的用法:

class Dog:

    def __init__(self, name):
        self.name = name
        self.tricks = []    # creates a new empty list for each dog

    def add_trick(self, trick):
        self.tricks.append(trick)

>>> d = Dog('Fido')
>>> e = Dog('Buddy')
>>> d.add_trick('roll over')
>>> e.add_trick('play dead')
>>> d.tricks
['roll over']
>>> e.tricks
['play dead']

函數的定義能夠在類外部(不推薦)

# Function defined outside the class
def f1(self, x, y):
    return min(x, x+y)

class C:
    f = f1

    def g(self):
        return 'hello world'

    h = g

經過self引用函數

class Bag:
    def __init__(self):
        self.data = []

    def add(self, x):
        self.data.append(x)

    def addtwice(self, x):
        self.add(x)
        self.add(x)

每個值都是一個對象,它的對象存儲在object.__class__

繼承

語法:

class DerivedClassName(BaseClassName):
    <statement-1>
    .
    .
    .
    <statement-N>

或(moduleName是導入的模塊)

class DerivedClassName(moduleName.BaseClassName):

若是引用的屬性沒有在當前類中找到,會找他的基類。繼承的類能夠重寫基類的方法。
有兩個內置的函數頗有用:
判斷實例的類型:isinstance(obj, int) 判斷是不是繼承自int類 ( 若是
obj.__class__ 是int或者繼承自int類 返回true)
issubclass(bool, int): 判斷bool是不是int的子類

多繼承

class DerivedClassName(Base1, Base2, Base3):
    <statement-1>
    .
    .
    .
    <statement-N>

私有變量和類內引用

私有變量通常是以_下劃線開頭
內部調用方法__雙下劃線開頭:

class Mapping:
    def __init__(self, iterable):
        self.items_list = []
        self.__update(iterable)

    def update(self, iterable):
        for item in iterable:
            self.items_list.append(item)

    __update = update  # 拷貝一份update方法

class MappingSubclass(Mapping):

    def update(self, keys, values): # 子類能夠重寫update方法 而且不會影響到init方法
        for item in zip(keys, values):
            self.items_list.append(item)

異常也是一個類

語法:

raise Class, instance
raise instance  (raise instance.__class__, instance的簡寫)

好比:

class B:
    pass
class C(B):
    pass
class D(C):
    pass

for c in [B, C, D]:
    try:
        raise c()
    except D:
        print "D"
    except C:
        print "C"
    except B:
        print "B"

遍歷

大部分的對象均可以遍歷

for element in [1, 2, 3]:
    print element
for element in (1, 2, 3):
    print element
for key in {'one':1, 'two':2}:
    print key
for char in "123":
    print char
for line in open("myfile.txt"):
    print line,

內部:for語句調用了iter()方法,而後調用next()方法訪問下一個元素,若是沒有下一個會拋出StopInteration異常

>>> s = 'abc'
>>> it = iter(s)
>>> it
<iterator object at 0x00A1DB50>
>>> it.next()
'a'
>>> it.next()
'b'
>>> it.next()
'c'
>>> it.next()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    it.next()
StopIteration

給類增長遍歷器:

class Reverse:
    """Iterator for looping over a sequence backwards."""
    def __init__(self, data):
        self.data = data
        self.index = len(data)

    def __iter__(self):
        return self

    def next(self):
        if self.index == 0:
            raise StopIteration
        self.index = self.index - 1
        return self.data[self.index]

使用:

>>> rev = Reverse('spam')
>>> iter(rev)
<__main__.Reverse object at 0x00A1DB50>
>>> for char in rev:
...    print char
...
m
a
p
s

標準庫相關

系統接口

>>> import os
>>> os.getcwd()      # 返回當前目錄
'C:\\Python26'
>>> os.chdir('/server/accesslogs')  # 改變工做目錄
>>> os.system('mkdir today')  # 運行shell命令:mkdir
0
>>> import os
>>> dir(os)
<returns a list of all module functions>
>>> help(os)
<returns an extensive manual page created from the module's docstrings>

文件操做:

>>> import shutil
>>> shutil.copyfile('data.db', 'archive.db')
>>> shutil.move('/build/executables', 'installdir')

文件通配符

>>> import glob
>>> glob.glob('*.py')
['primes.py', 'random.py', 'quote.py']

命令行參數

好比咱們跑了這個命令:python demo.py one two three
demo.py裏面的寫法是:

>>> import sys
>>> print sys.argv
['demo.py', 'one', 'two', 'three']

getopt模塊和argparse模塊提供了更多靈活的方式訪問命令行參數

退出程序和打印錯誤

sys.exit()
打印錯誤:

>>> sys.stderr.write('Warning, log file not found starting a new one\n')
Warning, log file not found starting a new one

字符串匹配

>>> import re
>>> re.findall(r'\bf[a-z]*', 'which foot or hand fell fastest')
['foot', 'fell', 'fastest']
>>> re.sub(r'(\b[a-z]+) \1', r'\1', 'cat in the the hat')
'cat in the hat'
>>> 'tea for too'.replace('too', 'two')
'tea for two'

數學

>>> import math
>>> math.cos(math.pi / 4.0)
0.70710678118654757
>>> math.log(1024, 2)
10.0
>>> import random
>>> random.choice(['apple', 'pear', 'banana'])
'apple'
>>> random.sample(xrange(100), 10)  # sampling without replacement
[30, 83, 16, 4, 8, 81, 41, 50, 18, 33]
>>> random.random()    # random float
0.17970987693706186
>>> random.randrange(6)    # random integer chosen from range(6)
4

網絡訪問

>>> import urllib2
>>> for line in urllib2.urlopen('http://tycho.usno.navy.mil/cgi-bin/timer.pl'):
...    if 'EST' in line or 'EDT' in line:  # look for Eastern Time
...        print line

<BR>Nov. 25, 09:43:32 PM EST

>>> import smtplib
>>> server = smtplib.SMTP('localhost')
>>> server.sendmail('soothsayer@example.org', 'jcaesar@example.org',
... """To: jcaesar@example.org
... From: soothsayer@example.org
...
... Beware the Ides of March.
... """)
>>> server.quit()

日期

>>> # dates are easily constructed and formatted
>>> from datetime import date
>>> now = date.today()
>>> now
datetime.date(2003, 12, 2)
>>> now.strftime("%m-%d-%y. %d %b %Y is a %A on the %d day of %B.")
'12-02-03. 02 Dec 2003 is a Tuesday on the 02 day of December.'

>>> # dates support calendar arithmetic
>>> birthday = date(1964, 7, 31)
>>> age = now - birthday
>>> age.days
14368

數據壓縮

>>> import zlib
>>> s = 'witch which has which witches wrist watch'
>>> len(s)
41
>>> t = zlib.compress(s)
>>> len(t)
37
>>> zlib.decompress(t)
'witch which has which witches wrist watch'
>>> zlib.crc32(s)
226805979

性能測試

>>> from timeit import Timer
>>> Timer('t=a; a=b; b=t', 'a=1; b=2').timeit()
0.57535828626024577
>>> Timer('a,b = b,a', 'a=1; b=2').timeit()
0.54962537085770791

質量控制

運行文檔內的測試代碼:

def average(values):
    """Computes the arithmetic mean of a list of numbers.

    >>> print average([20, 30, 70])
    40.0
    """
    return sum(values, 0.0) / len(values)

import doctest
doctest.testmod()  # automatically validate the embedded tests

運行測試集:

import unittest

class TestStatisticalFunctions(unittest.TestCase):

    def test_average(self):
        self.assertEqual(average([20, 30, 70]), 40.0)
        self.assertEqual(round(average([1, 5, 7]), 1), 4.3)
        with self.assertRaises(ZeroDivisionError):
            average([])
        with self.assertRaises(TypeError):
            average(20, 30, 70)

unittest.main()  # Calling from the command line invokes all tests

格式化輸出

>>> import repr
>>> repr.repr(set('supercalifragilisticexpialidocious'))
"set(['a', 'c', 'd', 'e', 'f', 'g', ...])"

自動換行和格式化:

>>> import pprint
>>> t = [[[['black', 'cyan'], 'white', ['green', 'red']], [['magenta',
...    'yellow'], 'blue']]]
...
>>> pprint.pprint(t, width=30)
[[[['black', 'cyan'],
  'white',
  ['green', 'red']],
  [['magenta', 'yellow'],
  'blue']]]

限制寬度輸出:

>>> import textwrap
>>> doc = """The wrap() method is just like fill() except that it returns
... a list of strings instead of one big string with newlines to separate
... the wrapped lines."""
...
>>> print textwrap.fill(doc, width=40)
The wrap() method is just like fill()
except that it returns a list of strings
instead of one big string with newlines
to separate the wrapped lines.

本地化輸出:

>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'English_United States.1252')
'English_United States.1252'
>>> conv = locale.localeconv()          # get a mapping of conventions
>>> x = 1234567.8
>>> locale.format("%d", x, grouping=True)
'1,234,567'
>>> locale.format_string("%s%.*f", (conv['currency_symbol'],
...                      conv['frac_digits'], x), grouping=True)
'$1,234,567.80'

模板

>>> from string import Template
>>> t = Template('${village}folk send $$10 to $cause.')
>>> t.substitute(village='Nottingham', cause='the ditch fund')
'Nottinghamfolk send $10 to the ditch fund.'

substitute會替換模板的關鍵字。若是傳遞的參數不對會報異常,建議用safe_substitute:

>>> t = Template('Return the $item to $owner.')
>>> d = dict(item='unladen swallow')
>>> t.substitute(d)
Traceback (most recent call last):
  ...
KeyError: 'owner'
>>> t.safe_substitute(d)
'Return the unladen swallow to $owner.'

自定義分隔符號:

>>> import time, os.path
>>> photofiles = ['img_1074.jpg', 'img_1076.jpg', 'img_1077.jpg']
>>> class BatchRename(Template):
...    delimiter = '%'
>>> fmt = raw_input('Enter rename style (%d-date %n-seqnum %f-format):  ')
Enter rename style (%d-date %n-seqnum %f-format):  Ashley_%n%f

>>> t = BatchRename(fmt)
>>> date = time.strftime('%d%b%y')
>>> for i, filename in enumerate(photofiles):
...    base, ext = os.path.splitext(filename)
...    newname = t.substitute(d=date, n=i, f=ext)
...    print '{0} --> {1}'.format(filename, newname)

img_1074.jpg --> Ashley_0.jpg
img_1076.jpg --> Ashley_1.jpg
img_1077.jpg --> Ashley_2.jpg

多線程

import threading, zipfile

class AsyncZip(threading.Thread):
    def __init__(self, infile, outfile):
        threading.Thread.__init__(self)
        self.infile = infile
        self.outfile = outfile

    def run(self):
        f = zipfile.ZipFile(self.outfile, 'w', zipfile.ZIP_DEFLATED)
        f.write(self.infile)
        f.close()
        print 'Finished background zip of: ', self.infile

background = AsyncZip('mydata.txt', 'myarchive.zip')
background.start()
print 'The main program continues to run in foreground.'

background.join()    # Wait for the background task to finish
print 'Main program waited until background was done.'

建議用單線程,而後Queue模塊實現多線程的操做,更加容易試錯和設計。

日誌

import logging
logging.debug('Debugging information')
logging.info('Informational message')
logging.warning('Warning:config file %s not found', 'server.conf')
logging.error('Error occurred')
logging.critical('Critical error -- shutting down')

弱引用

>>> import weakref, gc
>>> class A:
...    def __init__(self, value):
...        self.value = value
...    def __repr__(self):
...        return str(self.value)
...
>>> a = A(10)                  # 建立一個引用
>>> d = weakref.WeakValueDictionary()
>>> d['primary'] = a            # 不會建立引用
>>> d['primary']                # 
10
>>> del a                      # 刪除
>>> gc.collect()                # 運行垃圾回收
0
>>> d['primary']                # 這個時候訪問會報錯
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    d['primary']                
  File "C:/python26/lib/weakref.py", line 46, in __getitem__
    o = self.data[key]()
KeyError: 'primary'

Lists相關工具集

隊列:

>>> from collections import deque
>>> d = deque(["task1", "task2", "task3"])
>>> d.append("task4")
>>> print "Handling", d.popleft()
Handling task1

操做排序:已經排序的插入一個元素

>>> import bisect
>>> scores = [(100, 'perl'), (200, 'tcl'), (400, 'lua'), (500, 'python')]
>>> bisect.insort(scores, (300, 'ruby'))
>>> scores
[(100, 'perl'), (200, 'tcl'), (300, 'ruby'), (400, 'lua'), (500, 'python')]

精確的浮點操做:

>>> from decimal import *
>>> x = Decimal('0.70') * Decimal('1.05')
>>> x
Decimal('0.7350')
>>> x.quantize(Decimal('0.01'))  # round to nearest cent
Decimal(huanying guanzhu : '0.74')
>>> round(.70 * 1.05, 2)        # same calculation with floats
0.73
相關文章
相關標籤/搜索