一、對象相關術語
python程式中儲存的所有資料都是圍繞對象的概念建立起來的
每個對象都有一個身份(兩個變量名是否為同一對象用‘is’比較)、一個類型(對象類型用type()函數檢視)和一個值(對象中的資料是否相同用‘==’比較)。
1
2
3
4
5
6
7
8
<code>In [</code><code>1</code><code>]: a</code><code>=</code><code>3</code>
<code>In [</code><code>3</code><code>]: </code><code>type</code><code>(a) </code><code>#檢視變量類型</code>
<code>Out[</code><code>3</code><code>]: </code><code>int</code>
<code>In [</code><code>4</code><code>]: b</code><code>=</code><code>3</code>
<code>In [</code><code>5</code><code>]: a </code><code>is</code> <code>b </code><code>#比較兩個變量的類型</code>
<code>Out[</code><code>5</code><code>]: </code><code>True</code>
<code>In [</code><code>6</code><code>]: a</code><code>=</code><code>=</code><code>b </code><code>#值比較</code>
<code>Out[</code><code>6</code><code>]: </code><code>True</code>
對象類型也稱為對象的類别,用于描述對象的内部表示及其支援的方法和操作(使用dir()可以檢視變量所支援的操作,help()檢視詳細資訊)
建立特定類型的對象時,有時也将對象成為該類型的執行個體
執行個體建立後,身份和類型就不可改變。如果對象的值是可以改變的成為可變對象;否則為不可變對象
<a href="https://s2.51cto.com/wyfs02/M01/8D/F5/wKioL1iw7y6RkNB4AABDNJ7zTQI198.png-wh_500x0-wm_3-wmp_4-s_2297064584.png" target="_blank"></a>
如果某個對象包含對其他對象的引用,則稱為容器
大多數對象擁有大量特有的資料屬性和方法
屬性:與對象相關的值
方法:被調用時将在對象上執行某些操作的函數
二、類型的轉換
python中提供了大量的内置函數,用來實作不同資料類型之間的轉換,其中常用的有:
str(), repr()或 format(): 将非字元型資料轉換為字元
int(): 轉為整數
float(): 轉換為浮點型
list(s): 将字串 s 轉為清單;
tuple(s): 将字串 s 轉為元組;
set(s): 将字串 s 轉為集合;
frozenset(s): 将字串 s 轉換為不可變集合;
chr(x): 将整數轉為字元
dict(d): 建立字典; 其 d 必須是(key, value)的元組序列;
ord(x): 将字元轉換為整數值
hex(x): 将整數轉換為 16 進制字元
bin(x): 将整數轉換為 2 進制字元
oct(x): 将整數轉換為 8 進制字元
三、資料類型的運算
常見的資料類型有:int、long、float、complex、bool。對于數字類型,其本身是不可變類型,屬于字面量。
常用數字操作
操作
描述
x+y
加法
x**y
乘方
x-y
減法
x%y
取模
x*y
乘法
-x
一進制減法
x/y
除法
+x
一進制加法
x//y
截斷除法(隻留商)
比較運算
x<<y
左移
x|y
按位或
x>>y
右移
x^y
按位異或
x&y
按位與
~x
按位求反
四、序列類型
序列
索引為非負整數的有序對象集合,支援疊代。
字元串: str,unicode
'' 不可變類型
元組: tuple
() 不可變類型
清單: list
[] 可變類型
所有序列都可使用的操作和方法:
s[i]
傳回一個序列的元素i
min(s)
s中的最小值
s[i:j]
傳回一個切片
max(s)
s中的最大值
s[i:j:stride]
傳回一個擴充切片
sum(s [,init])
s中各項的和
len(s)
s中的元素數
all(s)
s中的所有項是否為true
any(s)
s中的任意項是否為true
9
10
11
12
13
14
15
16
17
<code>In [</code><code>16</code><code>]: a</code><code>=</code><code>(</code><code>1</code><code>,</code><code>2</code><code>,</code><code>3</code><code>,</code><code>76</code><code>,</code><code>9</code><code>,</code><code>0</code><code>,</code><code>56</code><code>,</code><code>34</code><code>) </code><code>#定義元組</code>
<code>In [</code><code>17</code><code>]: a[</code><code>2</code><code>] </code><code>#第三個元素(索引從0開始)</code>
<code>Out[</code><code>17</code><code>]: </code><code>3</code>
<code>In [</code><code>19</code><code>]: a[</code><code>2</code><code>:</code><code>6</code><code>] </code><code>#索引大于等于2,小于6的元素</code>
<code>Out[</code><code>19</code><code>]: (</code><code>3</code><code>, </code><code>76</code><code>, </code><code>9</code><code>, </code><code>0</code><code>)</code>
<code>In [</code><code>20</code><code>]: a[::</code><code>2</code><code>] </code><code>#所有奇數元素</code>
<code>Out[</code><code>20</code><code>]: (</code><code>1</code><code>, </code><code>3</code><code>, </code><code>9</code><code>, </code><code>56</code><code>)</code>
<code>In [</code><code>23</code><code>]: </code><code>len</code><code>(a) </code><code>#元組的長度</code>
<code>Out[</code><code>23</code><code>]: </code><code>8</code>
<code>In [</code><code>24</code><code>]: </code><code>any</code><code>(a) </code><code>#任意值是否為真</code>
<code>Out[</code><code>24</code><code>]: </code><code>True</code>
<code>In [</code><code>25</code><code>]: </code><code>min</code><code>(a) </code><code>#最小值</code>
<code>Out[</code><code>25</code><code>]: </code><code>0</code>
<code>In [</code><code>26</code><code>]: </code><code>max</code><code>(a) </code><code>#最大值</code>
<code>Out[</code><code>26</code><code>]: </code><code>76</code>
<code>In [</code><code>27</code><code>]: </code><code>sum</code><code>(a) </code><code>#元組所有元素之和</code>
<code>Out[</code><code>27</code><code>]: </code><code>181</code>
字元串的操作和方法
s.captitalize()
首字元變大寫
s.index(sub [,start [,end]])
找到指定字元串sub首次出現的位置,沒找到則報錯
t.join(s)
使用t作為分隔符連接配接序列s中的字元,成為字元串
s.lower()/s.upper()
轉換為小寫/大寫
s.replace(old,new [,count])
替換一個子字元串
s.split([sep [,maxsplit]])
使用sep作為分隔符對一個字元串進行劃分為清單,maxsplit指定最大次數
s.strip([chars])
删除char開頭和結尾的空白字元
s.find()
傳回子串第一個比對的偏移位置
s.swapcase()
字元串大小寫颠倒(大寫變小寫,小寫變大寫)
<code>In [</code><code>32</code><code>]: s</code><code>=</code><code>'beautiful is better than ugly.'</code>
<code>In [</code><code>33</code><code>]: s.capitalize()</code>
<code>Out[</code><code>33</code><code>]: </code><code>'Beautiful is better than ugly.'</code>
<code>In [</code><code>35</code><code>]: s.index(</code><code>'is'</code><code>)</code>
<code>Out[</code><code>35</code><code>]: </code><code>10</code>
<code>In [</code><code>38</code><code>]: a</code><code>=</code><code>[</code><code>'beautiful'</code><code>,</code><code>'is'</code> <code>,</code><code>'better'</code><code>, </code><code>'than'</code><code>, </code><code>'ugly'</code><code>]</code>
<code>In [</code><code>41</code><code>]: ''.join(s)</code>
<code>Out[</code><code>41</code><code>]: </code><code>'beautiful is better than ugly.'</code>
<code>In [</code><code>42</code><code>]: s</code>
<code>Out[</code><code>42</code><code>]: </code><code>'beautiful is better than ugly.'</code>
<code>In [</code><code>43</code><code>]: s.replace(</code><code>'b'</code><code>,</code><code>'a'</code><code>)</code>
<code>Out[</code><code>43</code><code>]: </code><code>'aeautiful is aetter than ugly.'</code>
<code>In [</code><code>44</code><code>]: s</code>
<code>Out[</code><code>44</code><code>]: </code><code>'beautiful is better than ugly.'</code>
<code>In [</code><code>46</code><code>]: s.split(</code><code>' '</code><code>)</code>
<code>Out[</code><code>46</code><code>]: [</code><code>'beautiful'</code><code>, </code><code>'is'</code><code>, </code><code>'better'</code><code>, </code><code>'than'</code><code>, </code><code>'ugly.'</code><code>]</code>
清單的操作和方法(可變序列)
清單屬于容器類型,其擁有的特點是任意對象的有序集合,可以通過索引通路其中的元素,長度可變,支援異構和任意嵌套。
s[i]=v
項目指派
del s[i]
項目删除
s[i:j]=t
切片指派
del s[i:j]
切片删除
s[i:j:stride]=t
擴充切片指派
del s[i:j:stride]
擴充切片删除
list(s)
将s轉換為一個清單
s.append(x)
将一個新元素x追加到s末尾
s.extend(t)
将一個新清單t追加到s末尾
s.count(x)
計算s中x的出現次數
s.index(x,[,start[,stop]])
查找x出現的最小索引位置,可選參數start,stop用于指定搜尋的起始和結束索引。
s.insert(i,x)
在索引i處插入x
s.pop([i])
傳回元素i并從清單中移除,如果省略i,則傳回清單中最後一個元素
s.remove(x)
搜尋x并從s中移除
s.reverse()
颠倒s中的所有元素的順序
s.sort([key [,reverse]])
對s中的所有元素進行排序,key是一個函數鍵值,reverse是一個标志,表示以倒序對清單進行排序。key和reverse應該始終以關鍵字參數的形式指定。
l1 +l2
合并兩個清單,傳回一個新的清單,不會修改原清單。
l1*N
把l1重複N次,傳回一個新清單。
in
成員關系判斷字元 ,用法 obj in container (obj 是否在 container)
清單的複制方式
l1=[1,2,3,4]
l2=l1 潛複制,同一個對象
l2=l1[:] 深複制,不是同一個對象
l2=copy.deepcopy(l1) 深複制
<a href="https://s1.51cto.com/wyfs02/M02/8D/F8/wKiom1ixBTHDuPy6AABKdEYgfb0632.png-wh_500x0-wm_3-wmp_4-s_3340812959.png" target="_blank"></a>
元組的操作和方法
容器類型,任意對象的有序集合,通過索引通路其中的元素,不可變對象,長度固定,支援異構和嵌套。雖然元組本身是不可變類型,但如果元組内嵌套了可變類型的元素,那麼此元素的修改不會傳回新元組。
()
定義空元組
合并兩個元組,傳回一個新的元組,不會修改原元組。
把l1重複N次,傳回一個新元組。
五、字典
字典屬于可變類型(映射類型),使用dict()函數可以建立。字典在其他程式設計語言中又稱作關聯數組或者散清單,主要通過鍵實作元素的存取;字典是一個無序集合(即不可通過索引對字典進行周遊);是可變類型的容器,其長度可變,同時支援異構與嵌套。
任何不可變對象都可以作為字典的鍵(如:字元串、數字、元組等);包含可變對象的清單、字典和元組不能用作鍵。
定義字典:
{key1:value1,key2:value2} {}:空字典
字典所支援的操作和方法:
len(m)
傳回 m 中的項目數
m[k]
傳回 m 中鍵 k 的項
m[k]=x
将 m[k]的值設為 x
Del m[k]
從 m 中删除 m[k]
K in m
如果 k 是 m 中的鍵, 傳回 true
m.clear()
删除 m 中的所有項目
m.copy()
傳回 m 的一個副本
m.fromkeys(s [,value])
建立一個新字典并将 s 中所有元素作為新字典的鍵, 鍵的值均為 value
m.get(k [,v])
傳回 m[k], 找不到則傳回 v
m.has_key(k)
如果 m 中存在 k 則傳回 true; 否則傳回 false( python3 中廢棄, 使用 in)
m.items()
傳回由(key,value)對組成的一個序列
m.keys()
傳回鍵值組成一個序列
m.pop(k [,default])
如果找到 m[k], 則傳回并從 m 中删除, 否則提供 defult 值; 沒提供則引發異常
m.popitem()
從 m 中删除一個随機的(key,value)對, 并把它傳回為一個元組
m.update(b)
将 b 中的所有對象添加到 m 中
m.values()
傳回 m 中所有值得一個序列
m.setdefault(k [,v])
如果找到 m[k], 則傳回 m[k], 否則傳回 v, 并将 m[k]的值設為 v
六、集合
集合使用函數set()建立,不可變集合使用frozenset建立。和字典一樣,集合也是無序排列、可以哈希的值;支援的方法有:集合關系測試,成員關系測試;可以疊代。集合部支援的操作有:索引、元素擷取、切片等。
集合常用操作和方法:
傳回 s 中項目數
S.copy()
制作 s 的一份副本
S.difference(t)
求差集。 傳回所有在 s 中不在 t 中的項目
s.intersection(t)
求交集。 傳回同時在 s 和 t 中的項目
s.isdisjoint(t)
如果 s 和 t 沒有相同項, 傳回 true
s.issubset(t)
如果 s 是 t 的一個子集, 傳回 true
s.issuperset(t)
如果 s 是 t 的一個超集, 傳回 true
s.symmetric_difference(t)
求對稱差集, 傳回所有在 s 或 t 中, 但不同時在這兩個集合的項
s.union(t)
求并集, 傳回所有在 s 或 t 中的項
s|t
S 和 t 的并集
s&t
S 和 t 的交集
s-t
差集
s^t
對稱差集
集合項目數
最大值
最小值
all()、 any()、 cmp()
可變集合(set)特有的操作和方法
s.add(itme)
将 item 添加到 s 中, 如果 item 已存在, 則無效果
s.clear()
删除 s 中的所有項
s.difference_update(t)
從 s 中删除同時也在 t 中的所有項
s.discard(item)
從 s 中删除 item, 如果 item 不存在則無效果
s.intersection_update(t)
計算 s 與 t 的交集, 結果放入 s
s.pop()
傳回一個任意的幾何元素, 并将其從 s 中删除
s.remove(item)
從 s 中删除 item, 如果 item 不是 s 的成員, 引發異常
s.symmetric_difference_update(t)
計算 s 與 t 的對稱差集, 結果放入 s
s.update(t)
将 t 中的所有項添加到 s, t 可以是集合, 序列或者支援疊代的任意對象。
七、python中的引用計數和垃圾收集器
python中所有對象都有引用計數:當給對象配置設定一個新名稱或者将其放入一個容器中, 其引用計數都會增加。使用 del 語句或者變量名重新指派時, 對象的引用計數會減少。
使用Sys.getrefcount()可以獲得對象目前引用計數。一個對象的引用計數歸零時, 它将被垃圾收集機制回收。
八、python的表達式和語句總結
基本操作: x + y, x - y, x * y, x / y, x // y, x % y
邏輯運算: x or y, x and y, not x
成員關系運算: x in y, x not in y
對象執行個體測試: x is y, x not is y
一進制運算: -x, +x, ~x:
比較運算: x < y, x > y, x <= y, x >= y, x == y, x != y
位運算: x | y, x & y, x ^ y, x << y, x >> y
幂運算: x ** y
索引和分片: x[i], x[i:j], x[i:j:stride]
調用: x(...)
取屬性: x.attribute
匿名函數: lambda args: expression
三元選擇表達式: x if y else z
生成器函數發送協定: yield x
本文轉自 夢想成大牛 51CTO部落格,原文連結:http://blog.51cto.com/yinsuifeng/1901162,如需轉載請自行聯系原作者