Head First Python 讀書筆記

來源:互聯網
上載者:User

本書封皮如下:

摘抄如下:

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還是應該進一步閱讀其他書籍並且動手實踐。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.