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 7a181f1

Browse filesBrowse files
committed
Split out docstring section
1 parent e1843a5 commit 7a181f1
Copy full SHA for 7a181f1

File tree

Expand file treeCollapse file tree

2 files changed

+81
-76
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+81
-76
lines changed
Open diff view settings
Collapse file

‎lectures/functions.md‎

Copy file name to clipboardExpand all lines: lectures/functions.md
+17-74Lines changed: 17 additions & 74 deletions
  • Display the source diff
  • Display the rich diff
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,20 @@ 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+
174188
### Keyword Arguments
175189

176190
```{index} single: Python; keyword arguments
@@ -227,79 +241,8 @@ In particular
227241
* Any object can be passed to a function as an argument, including other functions.
228242
* A function can return any kind of object, including functions.
229243

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.
244+
We will give examples of how straightforward it is to pass a function to
245+
a function in the following sections.
303246

304247
### One-Line Functions: `lambda`
305248

Collapse file

‎lectures/python_essentials.md‎

Copy file name to clipboardExpand all lines: lectures/python_essentials.md
+64-2Lines changed: 64 additions & 2 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -600,12 +600,18 @@ 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-
## Coding Style and PEP8
603+
604+
## Coding Style and Documentation
605+
606+
A consistent coding style and the use of
607+
documentations can make the code easier to understand and maintain.
608+
609+
### Python Style Guidelines: PEP8
604610

605611
```{index} single: Python; PEP8
606612
```
607613

608-
To learn more about the Python programming philosophy type `import this` at the prompt.
614+
You can find Python programming philosophy by typing `import this` at the prompt.
609615

610616
Among other things, Python strongly favors consistency in programming style.
611617

@@ -621,6 +627,62 @@ In Python, the standard style is set out in [PEP8](https://www.python.org/dev/pe
621627

622628
(Occasionally we'll deviate from PEP8 in these lectures to better match mathematical notation)
623629

630+
### Docstrings
631+
632+
```{index} single: Python; Docstrings
633+
```
634+
635+
Python has a system for adding comments to modules, class, functions, etc. called *docstrings*.
636+
637+
The nice thing about docstrings is that they are available at run-time.
638+
639+
Try running this
640+
641+
```{code-cell} python3
642+
def f(x):
643+
"""
644+
This function squares its argument
645+
"""
646+
return x**2
647+
```
648+
649+
After running this code, the docstring is available
650+
651+
```{code-cell} ipython
652+
f?
653+
```
654+
655+
```{code-block} ipython
656+
:class: no-execute
657+
658+
Type: function
659+
String Form:<function f at 0x2223320>
660+
File: /home/john/temp/temp.py
661+
Definition: f(x)
662+
Docstring: This function squares its argument
663+
```
664+
665+
```{code-cell} ipython
666+
f??
667+
```
668+
669+
```{code-block} ipython
670+
:class: no-execute
671+
672+
Type: function
673+
String Form:<function f at 0x2223320>
674+
File: /home/john/temp/temp.py
675+
Definition: f(x)
676+
Source:
677+
def f(x):
678+
"""
679+
This function squares its argument
680+
"""
681+
return x**2
682+
```
683+
684+
With one question mark we bring up the docstring, and with two we get the source code as well.
685+
624686
## Exercises
625687

626688
Solve the following exercises.

0 commit comments

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