Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e1843a5

Browse filesBrowse files
committed
Restructuring
1 parent 9f5e9e9 commit e1843a5
Copy full SHA for e1843a5

File tree

Expand file treeCollapse file tree

2 files changed

+130
-136
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+130
-136
lines changed
Open diff view settings
Collapse file

‎lectures/functions.md‎

Copy file name to clipboardExpand all lines: lectures/functions.md
+130Lines changed: 130 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,135 @@ print(new_abs_function(3))
171171
print(new_abs_function(-3))
172172
```
173173

174+
### Keyword Arguments
175+
176+
```{index} single: Python; keyword arguments
177+
```
178+
179+
In a {ref}`previous lecture <python_by_example>`, you came across the statement
180+
181+
```{code-block} python3
182+
:class: no-execute
183+
184+
plt.plot(x, 'b-', label="white noise")
185+
```
186+
187+
In this call to Matplotlib's `plot` function, notice that the last argument is passed in `name=argument` syntax.
188+
189+
This is called a *keyword argument*, with `label` being the keyword.
190+
191+
Non-keyword arguments are called *positional arguments*, since their meaning
192+
is determined by order
193+
194+
* `plot(x, 'b-', label="white noise")` is different from `plot('b-', x, label="white noise")`
195+
196+
Keyword arguments are particularly useful when a function has a lot of arguments, in which case it's hard to remember the right order.
197+
198+
You can adopt keyword arguments in user-defined functions with no difficulty.
199+
200+
The next example illustrates the syntax
201+
202+
```{code-cell} python3
203+
def f(x, a=1, b=1):
204+
return a + b * x
205+
```
206+
207+
The keyword argument values we supplied in the definition of `f` become the default values
208+
209+
```{code-cell} python3
210+
f(2)
211+
```
212+
213+
They can be modified as follows
214+
215+
```{code-cell} python3
216+
f(2, a=4, b=5)
217+
```
218+
219+
### The Flexibility of Python Functions
220+
221+
As we discussed in the {ref}`previous lecture <python_by_example>`, Python functions are very flexible.
222+
223+
In particular
224+
225+
* Any number of functions can be defined in a given file.
226+
* Functions can be (and often are) defined inside other functions.
227+
* Any object can be passed to a function as an argument, including other functions.
228+
* A function can return any kind of object, including functions.
229+
230+
We already {ref}`gave an example <test_program_6>` of how straightforward it is to pass a function to
231+
a function.
232+
233+
Note that a function can have arbitrarily many `return` statements (including zero).
234+
235+
Execution of the function terminates when the first return is hit, allowing
236+
code like the following example
237+
238+
```{code-cell} python3
239+
def f(x):
240+
if x < 0:
241+
return 'negative'
242+
return 'nonnegative'
243+
```
244+
245+
Functions without a return statement automatically return the special Python object `None`.
246+
247+
248+
### Docstrings
249+
250+
```{index} single: Python; Docstrings
251+
```
252+
253+
Python has a system for adding comments to functions, modules, etc. called *docstrings*.
254+
255+
The nice thing about docstrings is that they are available at run-time.
256+
257+
Try running this
258+
259+
```{code-cell} python3
260+
def f(x):
261+
"""
262+
This function squares its argument
263+
"""
264+
return x**2
265+
```
266+
267+
After running this code, the docstring is available
268+
269+
```{code-cell} ipython
270+
f?
271+
```
272+
273+
```{code-block} ipython
274+
:class: no-execute
275+
276+
Type: function
277+
String Form:<function f at 0x2223320>
278+
File: /home/john/temp/temp.py
279+
Definition: f(x)
280+
Docstring: This function squares its argument
281+
```
282+
283+
```{code-cell} ipython
284+
f??
285+
```
286+
287+
```{code-block} ipython
288+
:class: no-execute
289+
290+
Type: function
291+
String Form:<function f at 0x2223320>
292+
File: /home/john/temp/temp.py
293+
Definition: f(x)
294+
Source:
295+
def f(x):
296+
"""
297+
This function squares its argument
298+
"""
299+
return x**2
300+
```
301+
302+
With one question mark we bring up the docstring, and with two we get the source code as well.
174303

175304
### One-Line Functions: `lambda`
176305

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

483+
354484
## Exercises
355485

356486
```{exercise}
Collapse file

‎lectures/python_essentials.md‎

Copy file name to clipboardExpand all lines: lectures/python_essentials.md
-136Lines changed: 0 additions & 136 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -600,142 +600,6 @@ 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
604-
605-
```{index} single: Python; Functions
606-
```
607-
608-
Let's talk a bit more about functions, which are all important for good programming style.
609-
610-
### The Flexibility of Python Functions
611-
612-
As we discussed in the {ref}`previous lecture <python_by_example>`, Python functions are very flexible.
613-
614-
In particular
615-
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.
620-
621-
We already {ref}`gave an example <test_program_6>` of how straightforward it is to pass a function to
622-
a function.
623-
624-
Note that a function can have arbitrarily many `return` statements (including zero).
625-
626-
Execution of the function terminates when the first return is hit, allowing
627-
code like the following example
628-
629-
```{code-cell} python3
630-
def f(x):
631-
if x < 0:
632-
return 'negative'
633-
return 'nonnegative'
634-
```
635-
636-
Functions without a return statement automatically return the special Python object `None`.
637-
638-
### Docstrings
639-
640-
```{index} single: Python; Docstrings
641-
```
642-
643-
Python has a system for adding comments to functions, modules, etc. called *docstrings*.
644-
645-
The nice thing about docstrings is that they are available at run-time.
646-
647-
Try running this
648-
649-
```{code-cell} python3
650-
def f(x):
651-
"""
652-
This function squares its argument
653-
"""
654-
return x**2
655-
```
656-
657-
After running this code, the docstring is available
658-
659-
```{code-cell} ipython
660-
f?
661-
```
662-
663-
```{code-block} ipython
664-
:class: no-execute
665-
666-
Type: function
667-
String Form:<function f at 0x2223320>
668-
File: /home/john/temp/temp.py
669-
Definition: f(x)
670-
Docstring: This function squares its argument
671-
```
672-
673-
```{code-cell} ipython
674-
f??
675-
```
676-
677-
```{code-block} ipython
678-
:class: no-execute
679-
680-
Type: function
681-
String Form:<function f at 0x2223320>
682-
File: /home/john/temp/temp.py
683-
Definition: f(x)
684-
Source:
685-
def f(x):
686-
"""
687-
This function squares its argument
688-
"""
689-
return x**2
690-
```
691-
692-
With one question mark we bring up the docstring, and with two we get the source code as well.
693-
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-
739603
## Coding Style and PEP8
740604

741605
```{index} single: Python; PEP8

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.