国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Python基礎教程總結(一)

2019-11-14 17:36:15
字體:
來源:轉載
供稿:網友

引言:

  一直都聽說Python很強大,以前只是瀏覽了一些博客,發現有點像數學建模時使用的Matlab,就沒有深入去了解了。如今Python使用的地方越來越多,最近又在學習機器學習方面的知識,因此想系統的學一學,讓心里踏實一點。以下是本人在看《Python基礎教程第二版》時,使用自己的思維方式進行的總結,自言自語,把知識串起來,想再使用Python時就少翻點資料。

 

1. 心中有數

  學習一門語言之前我都想知道它到底能干什么,如果它干的事情和我現在已經會的語言相通,可能我就比較著學習,瀏覽一下語法。先看看百度百科對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到底能給我帶來怎樣的便利。

 

2. 第一章——基礎知識

2.1. 下載安裝

  閱讀的這本書是使用的是Python2.5,到Python官網http://www.python.org的Downloads下的All Releases下找一找下一個。

  

2.2. 數字和表達式

  • 對于x = 1/2,在C語言里面會得到0,要想得到0.5,需要有浮點數參與運算,即x = 1.0/2。然后Python也是如此,但是如果希望Python沒有浮點數參與就能執行普通除法,需要引入庫。
>>> from __future__ import division

 

  但此時又想做整除,怎么辦?就要用的//(雙斜線) x = 1//2。即使有浮點數參與運算,即x = 1.0 // 2.0,得到的也是0.0;

  • 冪運算: x = 2**3,結果是8。冪運算比取反優先級高,所以x = -3**2,結果是-9,x = (-3)**2結果才是9。或者x = pow(-3,2);
  • 十六進制:0xAF,和C語言一樣,前面是0x;
  • 八進制:010,和C語言一樣,前面是0;
  • 四舍五入最接近的整數:round();
  • 向下取整:math.floor(32.9),結果是32.0;
  • 復數運算:cmath.sqrt(-1),結果1j;
OperationResult
x | ybitwise or of x and y
x ^ ybitwise exclusive or of x and y
x & ybitwise and of x and y
x << nx shifted left by n bits
x >> nx shifted right by n bits
~xthe bits of x inverted

  該運算和C語言類似。

 

2.3. 字符串

  

3. 第二章——列表和元組

3.1. 概覽

  書上說Python包含6種內建序列,常用的是兩種類型:列表和元組。但到底是哪6種呢?搜遍了網上的博客,都是前面提到的那句話。最后到官網Document找到了。

 Sequence Types — str(字符串), unicode, list(列表), tuple(元組), buffer, xrange

  序列,是指有序集合排列。所有序列都具有索引(-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/ 

類型分類類型名稱描述
NoneType(None)null對象None
數字類型int整數
long長整數,任意精度(python2)
float浮點數
complex復數
bool布爾值(True或False)
序列類型str字符串
unicodeUnicode字符串(python2)
list列表
tuple元組
xrangexrang()創建的整數范圍
映射類型dic字典
集合類型set可變集合
frozenset不可變集合

 

 以上都是包括序列在內的常用類型。

 

3.2. 基本運算

OperationResult
x in sTrue if an item of s is equal to x, else False
x not in sFalse if an item of s is equal to x, else True
s + tthe concatenation of s and t
s * n, n * sn 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

  以上是序列的基本通用運算。

 

OperationResult
s[i] = xitem i of s is replaced by x
s[i:j] = tslice 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] = tthe 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),反向排序

  以上是序列的常用操作

3.3. 分片

  開始點包括在返回結果中,結束點不在分片內。

  • number[-3:]  ——  從倒數第三個數開始訪問到結尾;
  • number[:]   ——  返回所有,可用于復制序列;
  • number[0:10:1]——  步長為1;
  • number[::4]  ——  每4個元素的第一個;
  • number[8:3:-1] —— 步長為1,從右至左地提取數據;

 

3.4. 乘法

  • 'python'*5,結果是'pythonpythonpythonpythonpython'。相當于復制。
  • None是Python的內建值,表示啥都沒有。但卻站要站位置。嗯。。。就是站著茅坑不XX。

  

3.5. 列表

  • name = list('hello'),結果是['H','e','l','l','o'];
  • ' '.join(name),結果是"Hello";
  • number[1] = 2,name[2:] = list('ar'),修改值;
  • number[1:1] = [2,3,4],在number[1]前插入2,3,4;
  • number[1:4] = [],代換的方式刪除元素,結果和del number[1:4]一樣。
  • number.append(object),append是將參數作為對象增加,如
  • 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]

  

3.6. 元組

  定義一個包含一個值得元組,x = (42,)。因此會有

>>> 3*(40+2) 126>>> 3*(40+2,)(42, 42, 42)
>>> x = 1, 2, 3
>>> x
(1, 2 ,3)

 

4. 第三章——使用字符串

4.1. 格式化輸出

>>> print "Pi is: %.3f" % 3.141592653589793238462643383279Pi is: 3.142

4.2. 字符串格式化轉換類型

FlagMeaning
'#'

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

 

ConversionMeaning
'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.

 

先寫到這里,看看書再繼續寫字典,條件語句,類,異常。。。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黄冈市| 象州县| 灵璧县| 斗六市| 昌吉市| 伊宁县| 贡觉县| 调兵山市| 陈巴尔虎旗| 江口县| 西乡县| 合川市| 瑞金市| 黄骅市| 确山县| 宜良县| 托克逊县| 景泰县| 大足县| 同江市| 芷江| 同仁县| 商水县| 衡南县| 鹿邑县| 桦甸市| 通山县| 巴林右旗| 西盟| 台南市| 邻水| 寿光市| 岳池县| 平果县| 民县| 望谟县| 斗六市| 陈巴尔虎旗| 平邑县| 沂水县| 舞阳县|