摘要:或者輸入調用模塊模塊也就是源文件進入交互命令模式參數傳遞命令行的參數會賦值給模塊的變量。操作符是向下取數,小數位為。每一個腳本文件稱之為一個模塊。
Python是比較熱的語言,個人興趣參考官方文檔:https://docs.python.org/2/tut...
邊看邊翻譯了一下(后面會持續更新),有興趣的可以看看。
Python具有高級的數據結構,能高效地面向對象編程。它的優雅的語法、動態類型、解釋屬性使它能快速地在大部分平臺開發應用程序。它的第三方包、工具和庫都可以在這里找到:https://www.python.org/。
Python很容易通過C/C++函數或數據類型進行擴展。寫擴展參看:擴展指導, C/C++的Python API
更多文檔參看Python標準庫,Python的API
通過這個入門教程,你將學習到Pyton的一些常用特性和標準庫的一些方法或模塊。
寫在前面對于每天在電腦面前工作的人來說,可能你需要做一些自動化的工作,比如批量替換,查找更改等,或者你想寫一些界面程序,游戲等。
而對于專業的軟件開發人員來說,通過C/C++/Java庫去開發
、測試、編譯等又太繁瑣了。
這時候,Python是最佳的選擇。
你可以寫一些shell腳本或者bat腳本去移動文件或者修改數據,但不是很適合寫界面程序或者游戲。C/C++/Java又太繁瑣了。而Python比較適合快速的在各個平臺(windows, Mac, Unix)完成你要的工作。
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 "", line 1, in 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 ame" # here means newline! C:some ame >>> print r"C:some ame" # note the r before the quote C:some ame
多行字符串:三個"""或者"""
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 "", line 1, in 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"Hellou0020World !" u"Hello World !"
0x0020標識空格
支持原始模式: 字符串前面加入ur
python的默認編碼方式是:ASCII碼,如果Unicode字符串被打印或者寫到文件或者是str()方法轉化都會默認轉為ASCII碼,如果字符串不在0-127范圍就會報錯
>>> u"abc" u"abc" >>> str(u"abc") "abc" >>> u"??ü" u"xe4xf6xfc" >>> str(u"??ü") Traceback (most recent call last): File "", line 1, in ? UnicodeEncodeError: "ascii" codec can"t encode characters in position 0-2: ordinal not in range(128)
轉換為特定編碼:方法的參數為小寫
>>> u"??ü".encode("utf-8") "xc3xa4xc3xb6xc3xbc"數組
定義一個數組
>>> 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>>> 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 "", line 1, in 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 "二維數組", line 1, in [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 "", line 1, in 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
和導入一個模塊,并且把__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 " >>> hellos = repr(hello) >>> print hellos "hello, world " >>> # 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‘b’標識二進制形式,跨平臺
文件對象的方法讀文件:f.read(size)
>>> f.read() "This is the entire file. " >>> f.read() ""帶換行:
>>> f.readline() "This is the first line of the file. " >>> f.readline() "Second line of the file " >>> 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 ")將其他類型寫入文件需先轉為字符串:
>>> 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 "", line 1, in 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 "執行異常", line 1 while True print "Hello world" ^ SyntaxError: invalid syntax >>> 10 * (1/0) Traceback (most recent call last): File "處理異常", line 1, in ZeroDivisionError: integer division or modulo by zero >>> 4 + spam*3 Traceback (most recent call last): File " ", line 1, in NameError: name "spam" is not defined >>> "2" + 2 Traceback (most recent call last): File " ", line 1, in 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): ... passimport 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] raisetry...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 ...("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 "", line 1, in NameError: HiThere 不處理異常,直接將異常拋出:
>>> try: ... raise NameError("HiThere") ... except NameError: ... print "An exception flew by!" ... raise ... An exception flew by! Traceback (most recent call last): File "自定義異常", line 2, in 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 "", line 1, in __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 "", line 2, in 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 "預清理", line 1, in File " ", 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
定義init方法,設置默認屬性
MyClass.__doc_ => “A simple example class”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):. . . 或(moduleName是導入的模塊)
class DerivedClassName(moduleName.BaseClassName):如果引用的屬性沒有在當前類中找到,會找他的基類。繼承的類可以重寫基類的方法。
多繼承
有兩個內置的函數很有用:
判斷實例的類型:isinstance(obj, int) 判斷是否是繼承自int類 ( 如果
obj.__class__ 是int或者繼承自int類 返回true)
issubclass(bool, int): 判斷bool是否是int的子類class DerivedClassName(Base1, Base2, Base3):私有變量和類內引用. . . 私有變量一般是以_下劃線開頭
內部調用方法__雙下劃線開頭: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>>> it.next() "a" >>> it.next() "b" >>> it.next() "c" >>> it.next() Traceback (most recent call last): File " ", line 1, in 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)>>> help(os) 文件操作:
>>> 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 ") Warning, log file not found starting a new one字符串匹配>>> import re >>> re.findall(r"f[a-z]*", "which foot or hand fell fastest") ["foot", "fell", "fastest"] >>> re.sub(r"([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日期
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 "Lists相關工具集", line 1, in d["primary"] File "C:/python26/lib/weakref.py", line 46, in __getitem__ o = self.data[key]() KeyError: "primary" 隊列:
>>> 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
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/41278.html
摘要:想爬點數據來玩玩,我想最方便的工具就是了。這框架把采集需要用到的功能全部封裝好了,只要寫寫采集規則其他的就交給框架去處理,非常方便,沒有之一,不接受反駁。首先,大概看下這門語言。如果文檔看不懂的話,推薦看看這個教程爬蟲教程 想爬點數據來玩玩, 我想最方便的工具就是Python scrapy了。 這框架把采集需要用到的功能全部封裝好了,只要寫寫采集規則,其他的就交給框架去處理,非常方便,...
摘要:簡介是一個用于快速生成文檔的工具,非常適合生成文檔。稱之為主文檔,它被作為歡迎頁面。由于是默認的域,所以并不需要特別指出所屬的域來。自動生成文檔注釋支持從源代碼中提取文檔注釋信息,然后生成文檔,我們將這稱之為。 簡介 sphinx是一個用于快速生成文檔的工具,非常適合生成Python文檔。 它具有以下優點: 支持多種輸出格式, 如html,Latex,ePub等。 豐富的擴展 結構化...
摘要:例如改成例如改成以上兩種開發方式都可以結合原生平臺打包成獨立應用。 繼上一篇一張腦圖看懂BUI Webapp移動快速開發框架【上】--框架與工具、資源 大綱 在線查看大綱 思路更佳清晰 1. 框架設計 框架介紹 簡介 BUI 是用來快速構建界面交互的UI交互框架, 專注webapp開發, 開發者只需關注業務的開發, 界面的布局及交互交給BUI, 開發出來的應用, 可以嵌入平臺 ( Li...
摘要:對于瀏覽器,的值可能是可以通過調用函數,判斷用戶代理是否為瀏覽器。處理表單處理表單的方式很方便,可以使用超全局變量獲得數據。使得之中的特殊字符被正確的編碼,從而不會被使用者在頁面注入標簽或者代碼。 曾經簡單的學習過PHP,看的是《PHP和MySQL Web開發》,還有萬能的搜索引擎的幫助。這次準備系統的學習一下,參考資料是PHP Manual。 PHP能做什么 PHP主要用于服務端的腳...
閱讀 2879·2021-10-14 09:50
閱讀 1227·2021-10-08 10:21
閱讀 3661·2021-10-08 10:16
閱讀 3067·2021-09-27 14:02
閱讀 3142·2021-09-23 11:21
閱讀 2127·2021-09-07 10:17
閱讀 413·2019-08-30 14:00
閱讀 2117·2019-08-29 17:26