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

首頁 > 編程 > Python > 正文

head first python 第三章

2019-11-08 03:01:27
字體:
來源:轉載
供稿:網友

Help on method_descriptor:

一 - find(…)

S.find(sub[, start[, end]]) -> intReturn the lowest index in S where substring sub is found,such that sub is contained within S[start:end]. Optionalarguments start and end are interPReted as in slice notation. Return -1 on failure.

str.find()。find是str類的一個函數

找不到返回的是-1

另外python的string 類型是關鍵字是“str

-

二 python里的判斷語句if

not關鍵字

>>> a=10>>> b=9>>>> if not a<b: print("Hello World")Hello World

not相當于其他語言里面的!

三 三元運算符

>>> x,y=4,3#2.5以前的版本>>> smaller=(x<y and [x] or [y])[0]>>> print(smaller)3#在2.5版本更新后,可以寫如下簡單的條件表達式>>> smaller=x if x<y else y>>> smaller3

三 for 循環 一般for循環語法

for iter_var in iterable: suite_to_repeat

3.1用于序列類型 如下代碼返回的字符串的每個字符。對iterable執行迭代

>>> for eachLetter in 'Names': print ('curren letter:',eachLetter)curren letter: Ncurren letter: acurren letter: mcurren letter: ecurren letter: s

3.1.1通過序列項迭代

>>> nameList=['walter','nicole','steve','henry']>>> for eachName in nameList: print(eachName)walternicolestevehenry

3.1.2用序列索引迭代 C++,java屬性的方式

>>> nameList=['walter','nicole','steve','henry']>>> for index in range(len(nameList)): print(nameList[index])walternicolestevehenry

3.1.3使用項和索引迭代

class enumerate(object) | enumerate(iterable[, start]) -> iterator for index, value of iterable | | Return an enumerate object. iterable must be another object that supports | iteration. The enumerate object yields pairs containing a count (from | start, which defaults to zero) and a value yielded by the iterable argument. | enumerate is useful for obtaining an indexed list: | (0, seq[0]), (1, seq[1]), (2, seq[2]), ...

使用enumerate()函數

>>> for index,each_name in enumerate(nameList): print ("%d %s Lee"%(index+1,each_name))1 walter Lee2 nicole Lee3 steve Lee4 henry Lee

print在3.0 才加的外擴號。 在3.0里面 xrange被去掉。并且range返回的不再是一個list而是一個range object

四 pass語句

由于python沒有大括號,有些需要用大括號來結束的地方。如果沒寫語言解釋器會報語法錯誤,于是pass 就來填充這種情況。

五 while else語句

在循環中使用else語句時,else子句只在循環完成后執行,也就是說break語句會跳過else塊。

對比代碼~ python 求最大約數。先對num進行折半,得到count。然后取num與count的余數,當count>1并且取余為0是,此時的count即為最大公約數。

這里的代碼,else在每次執行完循環;也就當count<=1時,執行。

def showMaxFactor(num): count=int(num/2) while count>1: if num % count == 0: print('the lagest factor of %d is %d'%(num,count)) break count-=1 else: print(num,"is prime")for eachNum in range(10,21): showMaxFactor(eachNum)#這里有個點 關于python的/運算符>>> 10/25.0>>> 11/25.5>>> 10%50>>> 11%5.50.0#可以看到返回的時候一個浮點類型,而取余運算根據參與運算的類型來判斷返回類型 #這里要做轉換>>> int(11/2)5

再來看沒有while else的C++代碼

void showMaxFactor(int num){ int count = num/2; while(count>1){ if(num%count==0){ printf("the lagest factor of %d is %d /n",num,count); break; } else{ count--; } }//python簡化了代碼,在結構上減少了一次判斷,并使程序的邏輯變的清晰。 if(count<=1){ printf("%d is prime/n",num); }}int main(int argc, char** argv) { for(int i=10;i<21;i++){ showMaxFactor(i); } showMaxFactor(2); return 0;}

以上代碼輸出結果均為: 這里寫圖片描述

第三章代碼:

import ostry:#getcwd返回當前運行路徑 os.getcwd()#chdir(url)變更運行路徑至url os.chdir('D:/python/chapter3')#打開文件,如果文件不存在會跑出IOError異常 data = open('sketch.txt')#readline()讀出一行文件,返回一個str這里將end=''原因是每讀出的一行后面有一個'/n',用strip('/n')可以刪掉頭尾的換行。 print(data.readline(),end='') print(data.readline(),end='')#seek(0)返回文件的初始位置 data.seek(0) for each_line in data: try:#split返回的是一個list。這里定義的應該是一個元祖? (role,line_spoken) = each_line.split(':',1) print(role,end='') print('said:',end='') print(line_spoken,end='') except ValueError: pass data.close()except IOError: print('The data file is missing')

try……expect在語法細節上與2.X也有些變化。暫且不表 這段代碼要補充的就是文件讀寫關操作。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 昔阳县| 讷河市| 通州区| 津市市| 屯留县| 乌鲁木齐市| 仙居县| 三都| 凤山市| 禄丰县| 含山县| 洛宁县| 邹城市| 镇平县| 兴山县| 肇源县| 南京市| 天津市| 高雄市| 安宁市| 安化县| 陆河县| 德阳市| 庆阳市| 文水县| 隆尧县| 措勤县| 烟台市| 竹溪县| 长子县| 乌鲁木齐县| 武川县| 宣汉县| 丰镇市| 偃师市| 乡城县| 衡南县| 班玛县| 克什克腾旗| 三门峡市| 汉沽区|