Skip to content

Commit 885791a

Browse files
committed
Relocate Lambda Expression Section to Chapter 4
1 parent e2994ac commit 885791a

2 files changed

Lines changed: 39 additions & 38 deletions

File tree

lectures/functions.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,45 @@ In the context of our program, the ability to bind new names to functions
311311
means that there is no problem *passing a function as an argument to another
312312
function*---as we did above.
313313

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+
314353
## Exercises
315354

316355
```{exercise}

lectures/python_essentials.md

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -653,44 +653,6 @@ def f(x):
653653

654654
With one question mark we bring up the docstring, and with two we get the source code as well.
655655

656-
### One-Line Functions: `lambda`
657-
658-
```{index} single: Python; lambda functions
659-
```
660-
661-
The `lambda` keyword is used to create simple functions on one line.
662-
663-
For example, the definitions
664-
665-
```{code-cell} python3
666-
def f(x):
667-
return x**3
668-
```
669-
670-
and
671-
672-
```{code-cell} python3
673-
f = lambda x: x**3
674-
```
675-
676-
are entirely equivalent.
677-
678-
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).
679-
680-
The SciPy library has a function called `quad` that will do this calculation for us.
681-
682-
The syntax of the `quad` function is `quad(f, a, b)` where `f` is a function and `a` and `b` are numbers.
683-
684-
To create the function $f(x) = x^3$ we can use `lambda` as follows
685-
686-
```{code-cell} python3
687-
from scipy.integrate import quad
688-
689-
quad(lambda x: x**3, 0, 2)
690-
```
691-
692-
Here the function created by `lambda` is said to be *anonymous* because it was never given a name.
693-
694656
### Keyword Arguments
695657

696658
```{index} single: Python; keyword arguments

0 commit comments

Comments
 (0)