The book covers the following:
The excerpt is as follows:
P30: recursive print list
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: Print with default values
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: handling exceptions
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: specified exception
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!')
Pinterest: Data pickling
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: Find the minimum three non-repetition times in the time list
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: Definition class
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: inheritance class
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: Two-Dimensional dictionary
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]
Note: This book covers almost half of Python's applications on Web and mobile devices. I skipped this part because I am not interested in it. In general, this book continues the style of the "head first" series, which is illustrated in images and texts. However, there is not much knowledge to introduce, to learn python, read other books and practice them first.