@@ -171,6 +171,46 @@ print(new_abs_function(3))
171171print(new_abs_function(-3))
172172```
173173
174+
175+ ### One-Line Functions: ` lambda `
176+
177+ ``` {index} single: Python; lambda functions
178+ ```
179+
180+ The ` lambda ` keyword is used to create simple functions on one line.
181+
182+ For example, the definitions
183+
184+ ``` {code-cell} python3
185+ def f(x):
186+ return x**3
187+ ```
188+
189+ and
190+
191+ ``` {code-cell} python3
192+ f = lambda x: x**3
193+ ```
194+
195+ are entirely equivalent.
196+
197+ To see why ` lambda ` is useful, suppose that we want to calculate $\int_0^2 x^3 dx$ (and have forgotten our high-school calculus).
198+
199+ The SciPy library has a function called ` quad ` that will do this calculation for us.
200+
201+ The syntax of the ` quad ` function is ` quad(f, a, b) ` where ` f ` is a function and ` a ` and ` b ` are numbers.
202+
203+ To create the function $f(x) = x^3$ we can use ` lambda ` as follows
204+
205+ ``` {code-cell} python3
206+ from scipy.integrate import quad
207+
208+ quad(lambda x: x**3, 0, 2)
209+ ```
210+
211+ Here the function created by ` lambda ` is said to be * anonymous* because it was never given a name.
212+
213+
174214### Why Write Functions?
175215
176216User-defined functions are important for improving the clarity of your code by
@@ -311,45 +351,6 @@ In the context of our program, the ability to bind new names to functions
311351means that there is no problem * passing a function as an argument to another
312352function* ---as we did above.
313353
314- ### One-Line Functions: ` lambda `
315-
316- ``` {index} single: Python; lambda functions
317- ```
318-
319- The ` lambda ` keyword is used to create simple functions on one line.
320-
321- For example, the definitions
322-
323- ``` {code-cell} python3
324- def f(x):
325- return x**3
326- ```
327-
328- and
329-
330- ``` {code-cell} python3
331- f = lambda x: x**3
332- ```
333-
334- are entirely equivalent.
335-
336- To see why ` lambda ` is useful, suppose that we want to calculate $\int_0^2 x^3 dx$ (and have forgotten our high-school calculus).
337-
338- The SciPy library has a function called ` quad ` that will do this calculation for us.
339-
340- The syntax of the ` quad ` function is ` quad(f, a, b) ` where ` f ` is a function and ` a ` and ` b ` are numbers.
341-
342- To create the function $f(x) = x^3$ we can use ` lambda ` as follows
343-
344- ``` {code-cell} python3
345- from scipy.integrate import quad
346-
347- quad(lambda x: x**3, 0, 2)
348- ```
349-
350- Here the function created by ` lambda ` is said to be * anonymous* because it was never given a name.
351-
352-
353354## Exercises
354355
355356``` {exercise}
0 commit comments