一直都聽說Python很強大,以前只是瀏覽了一些博客,發現有點像數學建模時使用的Matlab,就沒有深入去了解了。如今Python使用的地方越來越多,最近又在學習機器學習方面的知識,因此想系統的學一學,讓心里踏實一點。以下是本人在看《Python基礎教程第二版》時,使用自己的思維方式進行的總結,自言自語,把知識串起來,想再使用Python時就少翻點資料。
學習一門語言之前我都想知道它到底能干什么,如果它干的事情和我現在已經會的語言相通,可能我就比較著學習,瀏覽一下語法。先看看百度百科對Python的解釋。
Python(英語發音:/?pa?θ?n/), 是一種面向對象、解釋型計算機程序設計語言,由Guido van Rossum于1989年底發明,第一個公開發行版發行于1991年。Python語法簡潔而清晰,具有豐富和強大的類庫。它常被昵稱為膠水語言,它能夠把用其他語言制作的各種模塊(尤其是C/C++)很輕松地聯結在一起。常見的一種應用情形是,使用Python快速生成程序的原型(有時甚至是程序的最終界面),然后對其中有特別要求的部分,用更合適的語言改寫,比如3D游戲中的圖形渲染模塊,性能要求特別高,就可以用C++重寫。
可以看出,首先它是一種解釋型的腳本語言,因此和Shell之類的很像。其次,它有強大的類庫。“類庫”,我只對java熟悉,一聽到類庫聯想到面向對象,類庫的另一種理解就是我們可以省去很多的編碼工作,使用成熟的類庫去解決以前寫C或者匯編時一步步實現的一些功能。再次,它的昵稱是“膠水語言”。“膠水語言”這個很厲害,讓我聯想到在jsp文件里面寫Java的場景,或者Java調用Native庫時的場景,可能比喻得不是很準確,應該和Shell腳本語言比較要貼切點。因此它的“包容”能力巨強,心胸寬廣。最后,使用Python快速生成程序的原型。哇,這豈不是驗證算法的最好方式。最早我只會C語言,寫算法程序的時候還是挺費勁的,從語法上就要考慮怎么能實現這個算法。不知道Python到底能給我帶來怎樣的便利。
閱讀的這本書是使用的是Python2.5,到Python官網http://www.python.org的Downloads下的All Releases下找一找下一個。
>>> from __future__ import division
但此時又想做整除,怎么辦?就要用的//(雙斜線) x = 1//2。即使有浮點數參與運算,即x = 1.0 // 2.0,得到的也是0.0;
| Operation | Result |
|---|---|
| x | y | bitwise or of x and y |
| x ^ y | bitwise exclusive or of x and y |
| x & y | bitwise and of x and y |
| x << n | x shifted left by n bits |
| x >> n | x shifted right by n bits |
| ~x | the bits of x inverted |
該運算和C語言類似。
書上說Python包含6種內建序列,常用的是兩種類型:列表和元組。但到底是哪6種呢?搜遍了網上的博客,都是前面提到的那句話。最后到官網Document找到了。
序列,是指有序集合排列。所有序列都具有索引(-1是最后一個元素)、分片、加、乘、成員資格、長度、最大和最小的內在函數。列表可以修改,元組則不能。一般情況下,列表都可以替代元組。序列和映射(如字典)是兩類主要的容器(container),還有集合(set)也屬于容器。
str,unicode: 這兩種字符串前面提到過了;
list: 列表。如edward = ['Panderen', 12];
tuple: 元組。
buffer: buffer對象,如string, array 和 buffer。使用方式buffer(object[,offset[,size]]);
xrange: 和range()作用相同,但是xrange返回一個xrange對象。range返回一個list對象。官網文檔解釋是This is an opaque sequence type which yields the same values as the corresponding list, without actually storing them all simultaneously. 也就是說xrange不會把數據同時存起來。相反range會把數據存起來。那range就會開辟很大的空間,特別是數據量很大的時候。因此用xrange也許性能會高點喲。
下面這個鏈接把一些常用的類型總結了一下。http://wuyuans.com/2013/04/python-data-type/
| 類型分類 | 類型名稱 | 描述 |
|---|---|---|
| None | Type(None) | null對象None |
| 數字類型 | int | 整數 |
| long | 長整數,任意精度(python2) | |
| float | 浮點數 | |
| complex | 復數 | |
| bool | 布爾值(True或False) | |
| 序列類型 | str | 字符串 |
| unicode | Unicode字符串(python2) | |
| list | 列表 | |
| tuple | 元組 | |
| xrange | xrang()創建的整數范圍 | |
| 映射類型 | dic | 字典 |
| 集合類型 | set | 可變集合 |
| frozenset | 不可變集合 |
以上都是包括序列在內的常用類型。
| Operation | Result |
|---|---|
| x in s | True if an item of s is equal to x, else False |
| x not in s | False if an item of s is equal to x, else True |
| s + t | the concatenation of s and t |
| s * n, n * s | n shallow copies of s concatenated |
| s[i] | i‘th item of s, origin 0 |
| s[i:j] | slice of s from i to j |
| s[i:j:k] | slice of s from i to j with step k |
| len(s) | length of s |
| min(s) | smallest item of s |
| max(s) | largest item of s |
以上是序列的基本通用運算。
| Operation | Result |
|---|---|
| s[i] = x | item i of s is replaced by x |
| s[i:j] = t | slice of s from i to j is replaced by the contents of the iterable t |
| del s[i:j] | same as s[i:j] = [] |
| s[i:j:k] = t | the elements of s[i:j:k] are replaced by those of t |
| del s[i:j:k] | removes the elements of s[i:j:k] from the list |
| s.append(x) | same as s[len(s):len(s)] = [x] |
| s.extend(x) | same as s[len(s):len(s)] = x |
| s.count(x) | return number of i‘s for which s[i] == x |
| s.index(x[, i[, j]]) | return smallest k such that s[k] == x and i <= k < j |
| s.insert(i, x) | same as s[i:i] = [x] |
| s.pop([i]) | same as x = s[i]; del s[i]; return x |
| s.remove(x) | same as del s[s.index(x)] |
| s.reverse() | reverses the items of s in place >>> x = [1, 2, 3]>>> list(reversed(x))[3, 2, 1]
|
| s.sort([cmp[, key[, reverse]]]) | sort the items of s in place x.sort(cmp) x.sort(key=len),根據長度排序 x.sort(reverse=True),反向排序 |
以上是序列的常用操作
開始點包括在返回結果中,結束點不在分片內。
1 >>> number = [1,2,3];2 >>> number3 [1, 2, 3]4 >>> number.append([4,5])5 >>> number6 [1, 2, 3, [4, 5]]7 >>> number.extend([6,7])8 >>> number9 [1, 2, 3, [4, 5], 6, 7]
定義一個包含一個值得元組,x = (42,)。因此會有
>>> 3*(40+2) 126>>> 3*(40+2,)(42, 42, 42)
>>> x = 1, 2, 3
>>> x
(1, 2 ,3)
>>> print "Pi is: %.3f" % 3.141592653589793238462643383279Pi is: 3.142
| Flag | Meaning |
|---|---|
| '#' | The value conversion will use the “alternate form” (where defined below). >>> print "Pi is: %#20.3f" % 3.141592653589793238462643383279Pi is: 3.142 |
| '0' | The conversion will be zero padded for numeric values. 用0填充 >>> print "Pi is: %020.3f" % 3.141592653589793238462643383279Pi is: 0000000000000003.142 |
| '-' | The converted value is left adjusted (overrides the '0' conversion if both are given). 左對齊 >>> print "Pi is: %-20.3f" % 3.141592653589793238462643383279Pi is: 3.142 |
| ' ' | (a space) A blank should be left before a positive number (or empty string) produced by a signed conversion. 整數左對齊 >>> print "Pi is: % 20.3f" % -3.141592653589793238462643383279Pi is: -3.142>>> print "Pi is: % 20.3f" % 3.141592653589793238462643383279Pi is: 3.142>>> print "Pi is: % 20.3f" % +3.141592653589793238462643383279Pi is: 3.142>>> print "Pi is: %+20.3f" % 3.141592653589793238462643383279Pi is: +3.142>>> print "Pi is: %+20.3f" % -3.141592653589793238462643383279Pi is: -3.142 |
| '+' | A sign character ('+' or '-') will precede the conversion (overrides a “space” flag). >>> print "Pi is: %+20.3f" % 3.141592653589793238462643383279Pi is: +3.142 |
| Conversion | Meaning |
|---|---|
| 'd' | Signed integer decimal. |
| 'i' | Signed integer decimal. |
| 'o' | Signed octal value. |
| 'u' | Obsolete type – it is identical to 'd'. |
| 'x' | Signed hexadecimal (lowercase). |
| 'X' | Signed hexadecimal (uppercase). |
| 'e' | Floating point exponential format (lowercase). |
| 'E' | Floating point exponential format (uppercase). |
| 'f' | Floating point decimal format. |
| 'F' | Floating point decimal format. |
| 'g' | Floating point format. Uses lowercase exponential format if exponent is less than -4 or not less than precision, decimal format otherwise. |
| 'G' | Floating point format. Uses uppercase exponential format if exponent is less than -4 or not less than precision, decimal format otherwise. |
| 'c' | Single character (accepts integer or single character string). |
| 'r' | String (converts any Python object using repr()). |
| 's' | String (converts any Python object using str()). |
| '%' | No argument is converted, results in a '%' character in the result. |
先寫到這里,看看書再繼續寫字典,條件語句,類,異常。。。
新聞熱點
疑難解答