本書封皮如下:
摘抄如下:
p30:遞迴列印列表
def print_lol(the_list):for each_item in the_list:if isinstance(each_item,list):print_lol(each_item)else:print(each_item)
p68:含預設值的列印
def printf_lol(the_list,indent=False,leven=0):for each_item in the_list:if isinstance(each_item,list):print_lol(each_item,indent,level+1)else:ifindent:for tab_stop in range(level):print("\t",end='')print(each_item)
p101:處理異常
try:data=open('sketch.txt')for each_line in data:try:(role,line_spoken)=each_line.split(':',1)print(role,end='')print('said:',end='')print(line_spoken,end='')except:passdata.close()except:print('The data file is missing!')
p102:特定指定異常
try:data=open('sketch.txt')for each_line in data:try:(role,line_spoken)=each_line.split(':',1)print(role,end='')print('said:',end='')print(line_spoken,end='')except ValueError:passdata.close()except IOError:print('The data file is missing!')
p133:資料醃制
import pickle...with open('mydata,pickle','wb') as mysavedata:pickle.dump([1,2,'three'],mysavedata)...with open('mydata.pickle','rb') as myrestoredata:a_list=pickle.load(myrestoredata)print(a_list)
p168:找出時間列表中最小的三個不重複時間
def sanitize(time_string):if '-' in time_string:splitter='-'elif ':' in time_string:splitter=':'else:return(time_string)(mins,secs)=time_string.split(splitter)return (mins+'.'+secs)with open('james.txt') as jaf:data=jaf.readline()james=data.strip().split(',')print(sorted(set([sanitize(t) for t in james]))[0:3])
p200:定義類
class Athlete:def __init__(self,a_name,a_dob=None,a_times=[]):self.name=a_nameself.dob=a_dobself.times=a_timesdef top3(self):return(sorted(set([sanitize(t) for t in self.times]))[0:3])def add_time(self,time_value):self.times.append(time_value)def add_times(self,list_of_times):self.times.extend(list_of_times)
p208:繼承類
class Athlete(list):def __init__(self,a_name,a_dob=None,a_times=[]):list.__init__([])self.name=a_nameself.dob=a_dobself.extend(a_times)def top3(self):return(sorted(set([sanitize(t) for t in self]))[0:3])vera=AthleteList('vera Vi')vera.append('1.31')print(vera.top3())vera.extend(['2.22','1-21','2:22'])print(vera.top3())
p407:二維字典
row_data={}with open('PaceData.csv') as paces:column_headings=paces.readline().strip().split(',')column_headings.pop(0)for each_line in paces:row=each_line.strip().split(',')row_label=row.pop(0)inner_dict={}for i in range(len(column_headings)):inner_dict[row[i]]=column_headings[i]row_data[row_label]=inner_dictcolumn_heading=row_data['15k']['43:24']prediction=[k for k in row_data['20k'].keys() if row_data['20k'][k]==column_heading]
說明:這本書花了幾乎一半篇幅介紹python在web和行動裝置上的應用,由於本人對此不感興趣就跳過了這部分內容。總體來說,這本書還是延續了《head first》系列的風格,圖文並茂生動形象,不過介紹的知識並不算很多,要想真正學python還是應該進一步閱讀其他書籍並且動手實踐。