2026-xx-xx_this-is-python.html
001|
002|
003|
004|
005|
006|
007|
008|
009|
010|
011|
012|
013|
014|
015|
016|
017|
018|
019|
020|
021|
022|
023|
024|
025|
026|
027|
028|
029|
030| ListSize = 16
031|
032| Reds = [3, 7, 13]
033|
034| Item = lambda n: 'red' if n in Reds else 'black'
035|
036|
037|
038| colors = []
039|
040|
041| for ix in range(ListSize):
042|
043|
044| color = Item(ix)
045|
046|
047| colors.append(color)
048|
049|
050|
051| for ix,color in enumerate(colors):
052| print(f'{ix:2d}: {color}')
053|
054| print()
055|
056|
057|
058|
059|
060| multiply_by = lambda factor: lambda n: factor*n
061|
062|
063| multiply_by_5 = multiply_by(5)
064| multiply_by_8 = multiply_by(8)
065|
066| print(f'')
067| print()
068|
069|
070|
071|
072|
073|
074|
075|
076|
077|
078| import time
079| import random
080|
081| def timer (function):
082| ts = []
083|
084| def inner (*args):
085| t0 = time.time()
086| rv = function(*args)
087| tt = time.time() - t0
088| ts.append(tt)
089| return rv
090|
091| inner.times = ts
092|
093| return inner
094|
095|
096| @timer
097| def my_function (a, b, c=0):
098| ...
099| ...
100| time.sleep(1.5 + (random.random() * 5))
101| ...
102| ...
103| return 42
104|
105| @timer
106| def my_other_function ():
107| ...
108| return
109|
110|
111| if __name__ == '__main__':
112| print()
113|
114| for ix in range(8):
115| print(ix, '...', sep='', end=' ')
116| my_function("foobar", 42)
117| print()
118|
119| my_other_function()
120| print()
121|
122| print(f'Total Time: {sum(my_function.times)}')
123| print(f'Max Time: {max(my_function.times)}')
124| print(f'Min Time: {min(my_function.times)}')
125| print()
126|
127| for ix,t in enumerate(my_function.times):
128| print(f'[{ix:2d}] {t}')
129| print()
130|
131| print(f'Other Times: {my_other_function.times}')
132| print()
133|
134|
135|
136|
137|
138|
139|
140|
141| from random import choice, shuffle
142|
143|
144| people = ['Alex', "Blair", 'Chris', "Drew", 'Ed', 'Fred']
145|
146|
147| booths = ['101', '201', '301a','301b', '401']
148|
149|
150| shuffle(booths)
151|
152|
153| tickets = []
154| for n in range(len(people)):
155| a = choice("ABCD")
156| b = choice("LMNOP")
157| ticket = f'LX{a}{b}{n:03d}'
158| tickets.append(ticket)
159|
160|
161| shuffle(tickets)
162|
163|
164| group = []
165| for member in zip(people, booths, tickets):
166| group.append(member)
167| print(member)
168| print()
169|
170|
171| from random import choice, shuffle
172|
173|
174| people = ['Alex', "Blair", 'Chris', "Drew", 'Ed', 'Fred']
175|
176|
177| booths = ['101', '201', '301a','301b', '401']
178|
179|
180| while len(booths) < len(people):
181| booths.append('--')
182|
183|
184| shuffle(booths)
185|
186|
187| tickets = []
188| for n in range(len(people)):
189| ticket = f'LX{choice("ABCD")}{choice("LMNOP")}{n:03d}'
190| tickets.append(ticket)
191|
192|
193| shuffle(tickets)
194|
195|
196| group = []
197| for member in zip(people, booths, tickets):
198| group.append(member)
199|
200|
201| for name,booth,ticket in group:
202| lotto = ' Winner!' if ticket.endswith('000') else ''
203| print(f'{name:7s} Booth: {booth:5s}{lotto}')
204| print()
205|
206|
207| from random import choice, shuffle
208|
209|
210| people = ['Alex', "Blair", 'Chris', "Drew", 'Ed', 'Fred']
211|
212|
213| booths = ['101', '201', '301a','301b', '401']
214|
215|
216| booth_gap = len(people) - len(booths)
217| if 0 < booth_gap:
218| blanks = ['--'] * booth_gap
219| booths.extend(blanks)
220|
221|
222| shuffle(booths)
223|
224|
225| X1 = "ABCDEFG"
226| X2 = "LMNOPRS"
227| ticket = lambda n: f'LX{choice(X1)}{choice(X2)}{n:03d}'
228|
229| tickets = [ticket(n) for n in range(len(people))]
230|
231|
232| shuffle(tickets)
233|
234|
235| group = list(zip(people, booths, tickets))
236|
237|
238| fmt = '{0:7s} Booth: {1:5s} Ticket: {2}{3}'
239|
240| for ix,member in enumerate(group):
241| name, booth, ticket = member
242| lotto = ' Winner!' if ticket.endswith('000') else ''
243| print(fmt.format(name,booth,ticket,lotto))
244| print()
245|
246|
247|
248|
249|
250|
251|
252|
253| def utf8_errors (s_in, codepage='cp1250', encoding='utf-8'):
254| '''For working with UTF-8 issues.'''
255| bs = s_in.encode(encoding)
256| s_out = bs.decode(codepage)
257| print()
258| print(s_in)
259| print()
260| print(' '.join(['%02x'%b for b in bs]))
261| print()
262| print(s_out)
263| print()
264| return (s_out, bs, s_in)
265|
266| def demo_utf8_errors ():
267|
268| s1 = r'I didn’t imagine that! Or think to ask, “When was that?”'
269| utf8_errors(s1)
270|
271|
272|
273|
274|
275|
276| if __name__ == '__main__':
277| print()
278| print()
279|
280|
281|