Skip to content

Commit 654be86

Browse files
authored
Merge pull request #198 from QuantEcon/lec-lambda-relocate
Chapter 4 & 5: Relocate Lambda Expression Section to Chapter 4
2 parents e2994ac + 5937389 commit 654be86

File tree

2 files changed

+40
-38
lines changed

2 files changed

+40
-38
lines changed

lectures/functions.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,46 @@ print(new_abs_function(3))
171171
print(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

176216
User-defined functions are important for improving the clarity of your code by

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)