1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 ‘‘‘ 4 def init(data): 5 data[‘first‘] = {} 6 data[‘middle‘] = {} 7 data[‘last‘] = {} 8 9 10 def lookup(data, label, name): 11 return data[label].get(name) 12 13 14 def store(data, full_name): 15 names = full_name.split() 16 if len(names) == 2: names.insert(1, ‘‘) 17 labels = ‘first‘, ‘middle‘, ‘last‘ 18 for label, name zip(labels, names): 19 people = lookup(data, label, name) 20 if people: 21 people.append(full_name) 22 else: 23 data[label][name] = [full_name] 24 25 ‘‘‘ 26 ‘‘‘ 27 def story(**kwds): 28 return ‘Once upon a time, there was a ‘29 ‘%(job)s called %(name)s.‘ %kwds 30 31 32 print(story(job=‘king‘, name=‘Gumgy‘)) 33 params = {‘job‘: ‘language‘, ‘name‘: ‘Python‘} 34 print(story(**params)) 35 36 del params[‘job‘] 37 print story(job=‘stroke of genius‘, **params) 38 39 ‘‘‘
40 ‘‘‘ 41 def interval(start, stop=None, step=1): 42 ‘imitates range() for step >0‘ 43 if stop is None: 44 start, stop = 0, start 45 46 result = [] 47 i = start 48 while i<stop: 49 result.append(i) 50 i += step 51 print(‘i:‘,i) 52 print(result) 53 interval(10) 54 55 PS C:\Users\tt\OneDrive - 我的云盘\allstack\socket_practise> python.exe .\basic_python_practise.py (‘i:‘, 1) 56 [0] 57 (‘i:‘, 2) 58 [0, 1] 59 (‘i:‘, 3) 60 [0, 1, 2] 61 (‘i:‘, 4) 62 [0, 1, 2, 3] 63 (‘i:‘, 5) 64 [0, 1, 2, 3, 4] 65 (‘i:‘, 6) 66 [0, 1, 2, 3, 4, 5] 67 (‘i:‘, 7) 68 [0, 1, 2, 3, 4, 5, 6] 69 (‘i:‘, 8) 70 [0, 1, 2, 3, 4, 5, 6, 7] 71 (‘i:‘, 9) 72 [0, 1, 2, 3, 4, 5, 6, 7, 8] 73 (‘i:‘, 10) 74 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 75 76 ‘‘‘
‘imitates range() for step >0‘这句是起到说明的作用么
原文:https://www.cnblogs.com/jarrel/p/elementary_python_practise.html