第10章 python語法簡介
再看python程序結(jié)構(gòu)
- 程序由模塊構(gòu)成
- 模塊包含語句
- 語句包含表達(dá)式
- 表達(dá)式建立并處理對象
一些注意事項(xiàng)
- python代碼中可能有;,它的作用是一行中有兩個語句。
- 被括號包裹的語句可以跨行
- 測試內(nèi)容數(shù)字。isdigit()
習(xí)題
第11章 賦值、表達(dá)式和打印
賦值語句
- 賦值語句建立對象引用值
- 變量名在首次賦值時被創(chuàng)建
- 變量名在引用前必須先賦值
- 關(guān)注隱式賦值:導(dǎo)入模塊,函數(shù)和類的定義,for循環(huán)等。
In [1]: a='a'
In [2]: a
Out[2]: 'a'
In [3]: b,c='b','c'
In [4]: b
Out[4]: 'b'
In [5]: c
Out[5]: 'c'
In [6]: [d,f]='d','f'
In [7]: d
Out[7]: 'd'
In [8]: f
Out[8]: 'f'
In [9]: [g,h]=['g','h']
In [10]: g
Out[10]: 'g'
In [11]: h
Out[11]: 'h'
In [12]: i,j,k='ijk'
In [13]: i
Out[13]: 'i'
In [14]: j
Out[14]: 'j'
In [15]: k
Out[15]: 'k'
In [17]: l,*n='lnm'
In [18]: l
Out[18]: 'l'
# 注意 這個地方獲得一個數(shù)組,并不是一個字符串
# 可以作為一種字符串轉(zhuǎn)數(shù)組的方法
In [19]: n
Out[19]: ['n', 'm']
In [20]: l,*n=[1,2,3,4,5]
In [21]: l
Out[21]: 1
In [22]: n
Out[22]: [2, 3, 4, 5]
In [23]: o=p='p'
In [24]: o
Out[24]: 'p'
In [25]: p
Out[25]: 'p'
# 星號變量的使用
In [37]: *a,b,c=[1,2,3,4,5,6,7,]
In [38]: a
Out[38]: [1, 2, 3, 4, 5]
In [39]: b
Out[39]: 6
In [40]: c
Out[40]: 7
In [41]: a,*b,c=[0,1,2,3,4,5,6,7,8,9]
In [42]: a
Out[42]: 0
In [43]: b
Out[43]: [1, 2, 3, 4, 5, 6, 7, 8]
In [44]: c
Out[44]: 9
In [45]: a,b,*c=[0,1,2,3,4,5,6,7,8,9]
In [46]: a
Out[46]: 0
In [47]: b
Out[47]: 1
In [48]: c
Out[48]: [2, 3, 4, 5, 6, 7, 8, 9]
# *賦值超出邊界會獲得一個空列表
In [49]: a,b,*c=[1,2]
In [50]: a
Out[50]: 1
In [51]: b
Out[51]: 2
In [52]: c
Out[52]: []
命名慣例
- 以劃線開頭的變量(_X)不會被from module import *導(dǎo)入。
- 前后都是雙下劃線的變量是系統(tǒng)定義的變量,對解釋器有特殊意義。
- 雙下劃線開頭是類的本地變量
- 通過交互模式運(yùn)行時,只有單個下劃線的變量名,會保存最后表達(dá)式的結(jié)果。
變量名沒有類型,但是對象有
print函數(shù)
print 帶三個參數(shù)sep,end和file
默認(rèn)發(fā)送到標(biāo)準(zhǔn)輸出流,當(dāng)然也可以重定義。
第12章 if測試和語法規(guī)則
真值測試:
- 任何非零數(shù)字或非空對象都為真
- 數(shù)字零、空對象以及特殊對象None都倍認(rèn)作是假
- 比較和相等測試會遞歸地應(yīng)用在數(shù)據(jù)結(jié)構(gòu)中
- 比較和相等測試會返回True和False
- 布爾and和or運(yùn)算會返回真或假的操作對象(注意:這里返回的是對象,不是True或False)
In [56]: 1 and 2
Out[56]: 2
In [57]: 1 and 0
Out[57]: 0
In [58]: 0 or 1
Out[58]: 1
In [59]: 0 or 100
Out[59]: 100
In [60]: 100 or 10
Out[60]: 100
In [61]: not 100
Out[61]: False
In [62]: [] or {}
Out[62]: {}
第13章 while和for循環(huán)
while循環(huán)
In [66]: x='abcdefg'
In [67]: while x:
...: print(x,end=' ')
...: x=x[1:]
...:
abcdefg bcdefg cdefg defg efg fg g
- break 直接跳出所在循環(huán)
- continue 跳到最近所在循環(huán)的開頭處
- pass 什么也不做
- else 只有當(dāng)循環(huán)正常離開的時候才會被執(zhí)行
# 判定質(zhì)數(shù)
In [69]: y=111
In [70]: x=y//2
In [72]: while x>1:
...: if y%x==0:
...: print(y,'has factor',x)
...: break
...: x -= 1
...: else:
...: print(y,'is prime')
...:
111 has factor 37
編寫循環(huán)的技巧
- for 循環(huán)執(zhí)行比 while 快
- 注意使用range()
- 注意使用zip() zip數(shù)量不一樣的,就取較少的數(shù)量。
- 盡量不要在for 中 調(diào)用 while
# zip 快速產(chǎn)生字典
In [79]: keys=(1,2,3,4)
In [80]: values=(5,6,7,8)
In [81]: d=dict(zip(keys,values))
In [82]: d
Out[82]: {1: 5, 2: 6, 3: 7, 4: 8}
- enumerate 來做偏移,返回一個生成器對象。
In [83]: s='abcde'
In [84]: for (offset,item) in enumerate(s):
...: print(offset,item,sep='-')
...:
0-a
1-b
2-c
3-d
4-e
第14章 迭代器和解析,第一部分
迭代器在python中使用c語言實(shí)現(xiàn),while語句使用python虛擬機(jī)運(yùn)行的字節(jié)碼,所以for比while快的多。
迭代器中,next()函數(shù)調(diào)用的就是< next >方法
對于列表這樣內(nèi)置對象,不是自身的迭代器,因?yàn)樗麄冎С侄啻未蜷_迭代器,這樣的對象,可以用iter來啟動迭代。
In [85]: l=[1,2,3]
In [86]: next(l)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-86-cdc8a39da60d> in <module>()
----> 1 next(l)
TypeError: 'list' object is not an iterator
In [87]: ll=iter(l)
In [88]: next(ll)
Out[88]: 1
In [89]: next(ll)
Out[89]: 2
In [90]: next(ll)
Out[90]: 3
In [91]: next(ll)
---------------------------------------------------------------------------
StopIteration Traceback (most recent call last)
<ipython-input-91-f4a50bb27fab> in <module>()
----> 1 next(ll)
StopIteration:
列表解析
列表解析的速度更快
In [95]: [x+y for x in 'abc' for y in 'lmn']
Out[95]: ['al', 'am', 'an', 'bl', 'bm', 'bn', 'cl', 'cm', 'cn']
# 下列方法均是在內(nèi)部調(diào)用迭代器
In [96]: sum([1,2,3])
Out[96]: 6
In [97]: any([0,1,2])
Out[97]: True
In [98]: all([0,1,2])
Out[98]: False
In [99]: max([0,1,2])
Out[99]: 2
In [100]: min([0,1,2])
Out[100]: 0
In [102]: range(5)
Out[102]: range(0, 5)
In [103]: range(5)[:2]
Out[103]: range(0, 2)
In [104]: range(5)[1:2]
Out[104]: range(1, 2)
支持多個迭代器
# 支持多個迭代器
In [106]: r=range(3)
In [107]: next(r)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-107-8ebe59a56b1d> in <module>()
----> 1 next(r)
TypeError: 'range' object is not an iterator
In [108]: r1=iter(r)
In [109]: next(r1)
Out[109]: 0
In [110]: next(r1)
Out[110]: 1
In [111]: r2=iter(r)
In [112]: next(r2)
Out[112]: 0
In [113]: next(r2)
Out[113]: 1
# 像zip map filter 只支持單個活躍的迭代器
In [114]: z=zip((1,2,3),(4,5,6))
In [115]: z
Out[115]: <zip at 0x10ffed988>
In [116]: z1=iter(z)
In [117]: z2=iter(z)
In [118]: next(z1)
Out[118]: (1, 4)
In [119]: next(z1)
Out[119]: (2, 5)
In [120]: next(z2)
Out[120]: (3, 6)
In [121]: next(z2)
---------------------------------------------------------------------------
StopIteration Traceback (most recent call last)
<ipython-input-121-ec134147f7d2> in <module>()
----> 1 next(z2)
StopIteration:
習(xí)題
- for循環(huán)和迭代器有什么關(guān)系?
for循環(huán)使用迭代協(xié)議來遍歷迭代對象的每一項(xiàng)。在每次迭代中調(diào)用的都是<next>方法,而且捕捉stopIteration異常,從而決定合適停止循環(huán)。
- for循環(huán)和列表解析的關(guān)系?
兩者都是迭代工具。
列表解析是更高效的方法。
- python中的迭代環(huán)境
for 循環(huán) ,列表解析,map內(nèi)置函數(shù),in成員測試,sum函數(shù)
- 讀文件的最好方法是什么
在迭代環(huán)境中開發(fā)文件,如for或列表解析。
第15章 文檔
- dir 展示了所有可用的屬性
- <doc> 為文檔字符串
- help函數(shù)