-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_functions.py
More file actions
436 lines (331 loc) · 12.1 KB
/
Copy pathlambda_functions.py
File metadata and controls
436 lines (331 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
"""
Module: lambda_functions.py
Topic: Lambda Functions
Level: Intermediate
This file teaches you about:
- Lambda function syntax
- When to use lambdas
- map(), filter(), reduce()
- Lambda with sorted()
- Lambda pitfalls
"""
# =============================================================================
# SECTION 1: LAMBDA BASICS
# =============================================================================
print("=" * 60)
print("LAMBDA BASICS")
print("=" * 60)
# Lambda syntax: lambda arguments: expression
# Regular function
def square(x):
return x ** 2
# Equivalent lambda
square_lambda = lambda x: x ** 2
print(f"Regular function: square(5) = {square(5)}")
print(f"Lambda function: square_lambda(5) = {square_lambda(5)}")
# Lambda with multiple arguments
add = lambda a, b: a + b
print(f"\nadd(3, 4) = {add(3, 4)}")
# Lambda with default arguments
greet = lambda name, greeting="Hello": f"{greeting}, {name}!"
print(f"greet('Alice') = {greet('Alice')}")
print(f"greet('Bob', 'Hi') = {greet('Bob', 'Hi')}")
# Lambda with conditional
is_even = lambda x: "even" if x % 2 == 0 else "odd"
print(f"\nis_even(4) = {is_even(4)}")
print(f"is_even(5) = {is_even(5)}")
# =============================================================================
# SECTION 2: WHEN TO USE LAMBDAS
# =============================================================================
print("\n" + "=" * 60)
print("WHEN TO USE LAMBDAS")
print("=" * 60)
# Good use case: short, simple operations
numbers = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x ** 2, numbers))
print(f"Square numbers: {squared}")
# Good use case: callback functions
button_click = lambda: print("Button clicked!")
print("\nSimulating button click:")
button_click()
# Good use case: key functions
students = [('Alice', 85), ('Bob', 92), ('Charlie', 78), ('Diana', 95)]
sorted_by_grade = sorted(students, key=lambda x: x[1], reverse=True)
print(f"\nStudents sorted by grade: {sorted_by_grade}")
# Bad use case: complex logic (use regular functions instead)
# DON'T do this:
# complex_lambda = lambda x: x**2 if x > 0 else -x**2 if x < 0 else 0
# DO this instead:
def complex_function(x):
"""Complex logic belongs in a named function."""
if x > 0:
return x ** 2
elif x < 0:
return -(x ** 2)
return 0
# =============================================================================
# SECTION 3: MAP FUNCTION
# =============================================================================
print("\n" + "=" * 60)
print("MAP FUNCTION")
print("=" * 60)
# map(function, iterable) applies function to each element
numbers = [1, 2, 3, 4, 5]
# Using lambda with map
doubled = list(map(lambda x: x * 2, numbers))
print(f"Doubled: {doubled}")
# Map with multiple iterables
list1 = [1, 2, 3]
list2 = [10, 20, 30]
sums = list(map(lambda x, y: x + y, list1, list2))
print(f"Sum of two lists: {sums}")
# Map vs list comprehension (comprehension often more readable)
numbers = [1, 2, 3, 4, 5]
# Using map
squares_map = list(map(lambda x: x ** 2, numbers))
# Using list comprehension (Pythonic way)
squares_comp = [x ** 2 for x in numbers]
print(f"\nMap result: {squares_map}")
print(f"Comprehension result: {squares_comp}")
# Map with built-in functions
words = ['hello', 'world', 'python']
uppercased = list(map(str.upper, words))
print(f"\nUppercased: {uppercased}")
# Map with None (converts to list of tuples)
keys = ['name', 'age', 'city']
values = ['Alice', 25, 'NYC']
pairs = list(zip(keys, values))
print(f"\nZipped pairs: {pairs}")
# =============================================================================
# SECTION 4: FILTER FUNCTION
# =============================================================================
print("\n" + "=" * 60)
print("FILTER FUNCTION")
print("=" * 60)
# filter(function, iterable) keeps elements where function returns True
numbers = list(range(1, 11))
# Filter even numbers
evens = list(filter(lambda x: x % 2 == 0, numbers))
print(f"Even numbers: {evens}")
# Filter with condition
words = ['apple', 'banana', 'cherry', 'date', 'elderberry']
long_words = list(filter(lambda x: len(x) > 5, words))
print(f"Long words (>5 chars): {long_words}")
# Filter with None (removes falsy values)
mixed = [0, 1, False, True, '', 'hello', None, [], [1, 2]]
truthy = list(filter(None, mixed))
print(f"\nTruthy values: {truthy}")
# Filter vs list comprehension
numbers = list(range(1, 11))
# Using filter
evens_filter = list(filter(lambda x: x % 2 == 0, numbers))
# Using list comprehension (often more readable)
evens_comp = [x for x in numbers if x % 2 == 0]
print(f"\nFilter: {evens_filter}")
print(f"Comprehension: {evens_comp}")
# Combining map and filter
numbers = list(range(1, 11))
result = list(map(lambda x: x ** 2, filter(lambda x: x % 2 == 0, numbers)))
print(f"\nSquares of evens: {result}")
# Same with list comprehension (more readable)
result_comp = [x ** 2 for x in numbers if x % 2 == 0]
print(f"Same with comprehension: {result_comp}")
# =============================================================================
# SECTION 5: REDUCE FUNCTION
# =============================================================================
print("\n" + "=" * 60)
print("REDUCE FUNCTION")
print("=" * 60)
from functools import reduce
# reduce(function, iterable) reduces to single value
numbers = [1, 2, 3, 4, 5]
# Sum using reduce
total = reduce(lambda x, y: x + y, numbers)
print(f"Sum: {total}")
# Product using reduce
product = reduce(lambda x, y: x * y, numbers)
print(f"Product: {product}")
# Find maximum
maximum = reduce(lambda x, y: x if x > y else y, numbers)
print(f"Maximum: {maximum}")
# With initial value
total_with_init = reduce(lambda x, y: x + y, numbers, 100)
print(f"\nSum with initial 100: {total_with_init}")
# Building a result
words = ['Hello', 'World', 'Python']
sentence = reduce(lambda x, y: f"{x} {y}", words)
print(f"Sentence: {sentence}")
# Practical example: flatten nested lists
nested = [[1, 2], [3, 4], [5, 6]]
flattened = reduce(lambda x, y: x + y, nested)
print(f"\nFlattened: {flattened}")
# =============================================================================
# SECTION 6: LAMBDA WITH SORTED
# =============================================================================
print("\n" + "=" * 60)
print("LAMBDA WITH SORTED")
print("=" * 60)
# Sort by custom key
students = [
{'name': 'Alice', 'grade': 85},
{'name': 'Bob', 'grade': 92},
{'name': 'Charlie', 'grade': 78},
{'name': 'Diana', 'grade': 95}
]
# Sort by grade
by_grade = sorted(students, key=lambda x: x['grade'], reverse=True)
print("Sorted by grade (descending):")
for student in by_grade:
print(f" {student['name']}: {student['grade']}")
# Sort by name length
by_name_length = sorted(students, key=lambda x: len(x['name']))
print("\nSorted by name length:")
for student in by_name_length:
print(f" {student['name']}")
# Sort tuple elements
items = [('apple', 3), ('banana', 1), ('cherry', 2)]
sorted_items = sorted(items, key=lambda x: x[1])
print(f"\nSorted by quantity: {sorted_items}")
# Sort by multiple keys
people = [
('Alice', 25, 'Engineer'),
('Bob', 25, 'Designer'),
('Charlie', 30, 'Engineer'),
('Diana', 30, 'Designer')
]
# Sort by age, then by name
sorted_people = sorted(people, key=lambda x: (x[1], x[0]))
print(f"\nSorted by age, then name: {sorted_people}")
# =============================================================================
# SECTION 7: LAMBDA WITH DATA STRUCTURES
# =============================================================================
print("\n" + "=" * 60)
print("LAMBDA WITH DATA STRUCTURES")
print("=" * 60)
# Dictionary of lambdas
operations = {
'add': lambda x, y: x + y,
'subtract': lambda x, y: x - y,
'multiply': lambda x, y: x * y,
'divide': lambda x, y: x / y if y != 0 else None
}
print("Calculator operations:")
print(f" add(10, 5): {operations['add'](10, 5)}")
print(f" multiply(10, 5): {operations['multiply'](10, 5)}")
# List of lambdas
transforms = [
lambda x: x.upper(),
lambda x: x.lower(),
lambda x: x.title(),
lambda x: x[::-1]
]
text = "Hello World"
print(f"\nTransformations of '{text}':")
for i, transform in enumerate(transforms):
print(f" Transform {i + 1}: {transform(text)}")
# =============================================================================
# SECTION 8: PRACTICAL EXAMPLES
# =============================================================================
print("\n" + "=" * 60)
print("PRACTICAL EXAMPLES")
print("=" * 60)
def process_data(data, operations):
"""
Apply a series of operations to data.
operations: list of (function, description) tuples
"""
results = []
current = data
for operation, description in operations:
current = operation(current)
results.append((description, current))
return results
data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
ops = [
(lambda x: list(filter(lambda n: n % 2 == 0, x)), "Filter evens"),
(lambda x: list(map(lambda n: n ** 2, x)), "Square"),
(lambda x: list(filter(lambda n: n < 50, x)), "Less than 50"),
(lambda x: sum(x), "Sum")
]
print("Data processing pipeline:")
results = process_data(data, ops)
for desc, result in results:
print(f" {desc}: {result}")
def create_multiplier(n):
"""Create a multiplier function."""
return lambda x: x * n
print("\nFunction factory:")
double = create_multiplier(2)
triple = create_multiplier(3)
print(f" double(5): {double(5)}")
print(f" triple(5): {triple(5)}")
def group_by(items, key_func):
"""Group items by a key function."""
groups = {}
for item in items:
key = key_func(item)
if key not in groups:
groups[key] = []
groups[key].append(item)
return groups
words = ['apple', 'banana', 'cherry', 'apricot', 'blueberry', 'avocado']
by_first_letter = group_by(words, lambda x: x[0])
print("\nGroup by first letter:")
for letter, word_list in sorted(by_first_letter.items()):
print(f" {letter}: {word_list}")
def analyze_text(text):
"""Analyze text using various metrics."""
words = text.split()
metrics = {
'word_count': len(words),
'char_count': len(text),
'avg_word_length': sum(map(len, words)) / len(words) if words else 0,
'unique_words': len(set(map(str.lower, words))),
'longest_word': max(words, key=len) if words else '',
'shortest_word': min(words, key=len) if words else ''
}
return metrics
sample = "The quick brown fox jumps over the lazy dog"
print(f"\nText analysis of '{sample}':")
metrics = analyze_text(sample)
for key, value in metrics.items():
print(f" {key}: {value}")
# =============================================================================
# SECTION 9: LAMBDA PITFALLS
# =============================================================================
print("\n" + "=" * 60)
print("LAMBDA PITFALLS")
print("=" * 60)
# Pitfall 1: Late binding in loops
print("Pitfall 1: Late binding")
functions = [lambda x: x + i for i in range(5)]
print(f" Wrong: {[f(10) for f in functions]}") # All add 4!
# Correct way with default argument
functions = [lambda x, i=i: x + i for i in range(5)]
print(f" Correct: {[f(10) for f in functions]}")
# Pitfall 2: Overly complex lambdas
# Don't write complex lambdas like this:
# complex_lambda = lambda x: (x**2 if x > 0 else -x**2) if isinstance(x, (int, float)) else None
# Instead, use a proper function:
def proper_function(x):
if not isinstance(x, (int, float)):
return None
return x ** 2 if x > 0 else -(x ** 2)
print("\nPitfall 2: Complexity")
print(f" Use proper functions for complex logic")
# Pitfall 3: Losing debug information
# Lambda functions have generic __name__
add_lambda = lambda x, y: x + y
def add_function(x, y):
return x + y
print(f"\nPitfall 3: Debug info")
print(f" Lambda name: {add_lambda.__name__}")
print(f" Function name: {add_function.__name__}")
# =============================================================================
# MAIN EXECUTION
# =============================================================================
if __name__ == "__main__":
print("\n" + "=" * 60)
print("✅ You've learned about lambda functions!")
print("📚 Next: Learn about decorators in decorators_intro.py")
print("=" * 60)