Skip to content

Commit 1742a3e

Browse files
authored
Merge pull request #205 from QuantEcon/lec-review-more-functions
Lecture 4 and 5: Integrating "More Functions" Section into Chapter 4
2 parents 9f5e9e9 + 71b9183 commit 1742a3e

File tree

2 files changed

+91
-90
lines changed

2 files changed

+91
-90
lines changed

lectures/functions.md

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ This will become clearer as you see more examples.
118118

119119
Let's start by discussing how it's done.
120120

121-
### Syntax
121+
### Basic Syntax
122122

123123
Here's a very simple Python function, that implements the mathematical function
124124
$f(x) = 2 x + 1$
@@ -171,6 +171,78 @@ print(new_abs_function(3))
171171
print(new_abs_function(-3))
172172
```
173173

174+
Note that a function can have arbitrarily many `return` statements (including zero).
175+
176+
Execution of the function terminates when the first return is hit, allowing
177+
code like the following example
178+
179+
```{code-cell} python3
180+
def f(x):
181+
if x < 0:
182+
return 'negative'
183+
return 'nonnegative'
184+
```
185+
186+
Functions without a return statement automatically return the special Python object `None`.
187+
188+
### Keyword Arguments
189+
190+
```{index} single: Python; keyword arguments
191+
```
192+
193+
In a {ref}`previous lecture <python_by_example>`, you came across the statement
194+
195+
```{code-block} python3
196+
:class: no-execute
197+
198+
plt.plot(x, 'b-', label="white noise")
199+
```
200+
201+
In this call to Matplotlib's `plot` function, notice that the last argument is passed in `name=argument` syntax.
202+
203+
This is called a *keyword argument*, with `label` being the keyword.
204+
205+
Non-keyword arguments are called *positional arguments*, since their meaning
206+
is determined by order
207+
208+
* `plot(x, 'b-', label="white noise")` is different from `plot('b-', x, label="white noise")`
209+
210+
Keyword arguments are particularly useful when a function has a lot of arguments, in which case it's hard to remember the right order.
211+
212+
You can adopt keyword arguments in user-defined functions with no difficulty.
213+
214+
The next example illustrates the syntax
215+
216+
```{code-cell} python3
217+
def f(x, a=1, b=1):
218+
return a + b * x
219+
```
220+
221+
The keyword argument values we supplied in the definition of `f` become the default values
222+
223+
```{code-cell} python3
224+
f(2)
225+
```
226+
227+
They can be modified as follows
228+
229+
```{code-cell} python3
230+
f(2, a=4, b=5)
231+
```
232+
233+
### The Flexibility of Python Functions
234+
235+
As we discussed in the {ref}`previous lecture <python_by_example>`, Python functions are very flexible.
236+
237+
In particular
238+
239+
* Any number of functions can be defined in a given file.
240+
* Functions can be (and often are) defined inside other functions.
241+
* Any object can be passed to a function as an argument, including other functions.
242+
* A function can return any kind of object, including functions.
243+
244+
We will give examples of how straightforward it is to pass a function to
245+
a function in the following sections.
174246

175247
### One-Line Functions: `lambda`
176248

@@ -351,6 +423,7 @@ In the context of our program, the ability to bind new names to functions
351423
means that there is no problem *passing a function as an argument to another
352424
function*---as we did above.
353425

426+
354427
## Exercises
355428

356429
```{exercise}

lectures/python_essentials.md

Lines changed: 17 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -600,47 +600,39 @@ Note:
600600
* `all()` returns `True` when *all* boolean values/expressions in the sequence are `True`
601601
* `any()` returns `True` when *any* boolean values/expressions in the sequence are `True`
602602

603-
## More Functions
604603

605-
```{index} single: Python; Functions
606-
```
604+
## Coding Style and Documentation
607605

608-
Let's talk a bit more about functions, which are all important for good programming style.
606+
A consistent coding style and the use of
607+
documentation can make the code easier to understand and maintain.
609608

610-
### The Flexibility of Python Functions
609+
### Python Style Guidelines: PEP8
611610

612-
As we discussed in the {ref}`previous lecture <python_by_example>`, Python functions are very flexible.
611+
```{index} single: Python; PEP8
612+
```
613613

614-
In particular
614+
You can find Python programming philosophy by typing `import this` at the prompt.
615615

616-
* Any number of functions can be defined in a given file.
617-
* Functions can be (and often are) defined inside other functions.
618-
* Any object can be passed to a function as an argument, including other functions.
619-
* A function can return any kind of object, including functions.
616+
Among other things, Python strongly favors consistency in programming style.
620617

621-
We already {ref}`gave an example <test_program_6>` of how straightforward it is to pass a function to
622-
a function.
618+
We've all heard the saying about consistency and little minds.
623619

624-
Note that a function can have arbitrarily many `return` statements (including zero).
620+
In programming, as in mathematics, the opposite is true
625621

626-
Execution of the function terminates when the first return is hit, allowing
627-
code like the following example
622+
* A mathematical paper where the symbols $\cup$ and $\cap$ were
623+
reversed would be very hard to read, even if the author told you so on the
624+
first page.
628625

629-
```{code-cell} python3
630-
def f(x):
631-
if x < 0:
632-
return 'negative'
633-
return 'nonnegative'
634-
```
626+
In Python, the standard style is set out in [PEP8](https://www.python.org/dev/peps/pep-0008/).
635627

636-
Functions without a return statement automatically return the special Python object `None`.
628+
(Occasionally we'll deviate from PEP8 in these lectures to better match mathematical notation)
637629

638630
### Docstrings
639631

640632
```{index} single: Python; Docstrings
641633
```
642634

643-
Python has a system for adding comments to functions, modules, etc. called *docstrings*.
635+
Python has a system for adding comments to modules, classes, functions, etc. called *docstrings*.
644636

645637
The nice thing about docstrings is that they are available at run-time.
646638

@@ -691,71 +683,7 @@ def f(x):
691683

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

694-
### Keyword Arguments
695-
696-
```{index} single: Python; keyword arguments
697-
```
698-
699-
In a {ref}`previous lecture <python_by_example>`, you came across the statement
700-
701-
```{code-block} python3
702-
:class: no-execute
703-
704-
plt.plot(x, 'b-', label="white noise")
705-
```
706-
707-
In this call to Matplotlib's `plot` function, notice that the last argument is passed in `name=argument` syntax.
708-
709-
This is called a *keyword argument*, with `label` being the keyword.
710-
711-
Non-keyword arguments are called *positional arguments*, since their meaning
712-
is determined by order
713-
714-
* `plot(x, 'b-', label="white noise")` is different from `plot('b-', x, label="white noise")`
715-
716-
Keyword arguments are particularly useful when a function has a lot of arguments, in which case it's hard to remember the right order.
717-
718-
You can adopt keyword arguments in user-defined functions with no difficulty.
719-
720-
The next example illustrates the syntax
721-
722-
```{code-cell} python3
723-
def f(x, a=1, b=1):
724-
return a + b * x
725-
```
726-
727-
The keyword argument values we supplied in the definition of `f` become the default values
728-
729-
```{code-cell} python3
730-
f(2)
731-
```
732-
733-
They can be modified as follows
734-
735-
```{code-cell} python3
736-
f(2, a=4, b=5)
737-
```
738-
739-
## Coding Style and PEP8
740-
741-
```{index} single: Python; PEP8
742-
```
743-
744-
To learn more about the Python programming philosophy type `import this` at the prompt.
745-
746-
Among other things, Python strongly favors consistency in programming style.
747-
748-
We've all heard the saying about consistency and little minds.
749-
750-
In programming, as in mathematics, the opposite is true
751-
752-
* A mathematical paper where the symbols $\cup$ and $\cap$ were
753-
reversed would be very hard to read, even if the author told you so on the
754-
first page.
755-
756-
In Python, the standard style is set out in [PEP8](https://www.python.org/dev/peps/pep-0008/).
757-
758-
(Occasionally we'll deviate from PEP8 in these lectures to better match mathematical notation)
686+
You can find conventions for docstrings in [PEP257](https://peps.python.org/pep-0257/).
759687

760688
## Exercises
761689

0 commit comments

Comments
 (0)