Streamline previous content
This commit is contained in:
parent
974362288a
commit
8fd14a614d
6 changed files with 393 additions and 384 deletions
|
|
@ -19,7 +19,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"In [Chapter 1](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/01_elements.ipynb), we typed the **business logic** of our little program to calculate the mean of a subset of a list of numbers right into the code cells. Then, we executed them one after another. We had no way of **re-using** the code except for either re-executing the cells or copying and pasting their contents into other cells. And whenever we find ourselves doing repetitive manual work, we can be sure that there must be a way of automating what we are doing.\n",
|
||||
"In [Chapter 1](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/01_elements.ipynb#Example:-Averaging-Even-Numbers), we typed the code to calculate the average of the even numbers in a list into several code cells. Then, we executed them one after another. We had no way of **re-using** the code except for either re-executing the cells or copying and pasting their contents into other cells. And, whenever we find ourselves doing repetitive manual work, we can be sure that there must be a way of automating what we are doing.\n",
|
||||
"\n",
|
||||
"At the same time, we executed built-in functions (e.g., [print()](https://docs.python.org/3/library/functions.html#print), [sum()](https://docs.python.org/3/library/functions.html#sum), [len()](https://docs.python.org/3/library/functions.html#len), [id()](https://docs.python.org/3/library/functions.html#id), or [type()](https://docs.python.org/3/library/functions.html#type)) that obviously must be re-using the same parts inside core Python every time we use them.\n",
|
||||
"\n",
|
||||
|
|
@ -55,7 +55,7 @@
|
|||
"\n",
|
||||
"Together, the name and the list of parameters are also referred to as the function's **[signature](https://en.wikipedia.org/wiki/Type_signature)** (i.e., `average_evens(numbers)` below).\n",
|
||||
"\n",
|
||||
"A function may come with an *explicit* **[return value](https://docs.python.org/3/reference/simple_stmts.html#the-return-statement)** (i.e., \"result\" or \"output\") specified with the `return` statement: Functions that have one are considered **fruitful**; otherwise, they are **void**. Functions of the latter kind are still useful because of their **side effects** (e.g., the [print()](https://docs.python.org/3/library/functions.html#print) built-in). Strictly speaking, they also have an *implicit* return value of `None` that is different from the `False` we saw in [Chapter 1](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/01_elements.ipynb#Identity-/-\"Memory-Location\").\n",
|
||||
"A function may come with an *explicit* **return value** (i.e., \"result\" or \"output\") specified with the `return` statement (cf., [reference](https://docs.python.org/3/reference/simple_stmts.html#the-return-statement)): Functions that have one are considered **fruitful**; otherwise, they are **void**. Functions of the latter kind are still useful because of their **side effects** as, for example, the built-in [print()](https://docs.python.org/3/library/functions.html#print) function. Strictly speaking, they also have an *implicit* return value of `None`.\n",
|
||||
"\n",
|
||||
"A function should define a **docstring** that describes what it does in a short subject line, what parameters it expects (i.e., their types), and what it returns (if anything). A docstring is a syntactically valid multi-line string (i.e., type `str`) defined within **triple-double quotes** `\"\"\"`. Strings are covered in depth in [Chapter 6](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/06_text.ipynb#The-str-Type). Widely adopted standards as to how to format a docstring are [PEP 257](https://www.python.org/dev/peps/pep-0257/) and section 3.8 in [Google's Python Style Guide](https://github.com/google/styleguide/blob/gh-pages/pyguide.md)."
|
||||
]
|
||||
|
|
@ -142,7 +142,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"139829511291360"
|
||||
"139646582981088"
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
|
|
@ -302,7 +302,7 @@
|
|||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
|
||||
"nums = [7, 11, 8, 5, 3, 12, 2, 6, 9, 10, 1, 4]"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -317,7 +317,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"6.0"
|
||||
"7.0"
|
||||
]
|
||||
},
|
||||
"execution_count": 9,
|
||||
|
|
@ -337,7 +337,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"The return value is commonly assigned to a new variable for later reference. Otherwise, we would lose access to it in memory right away."
|
||||
"The return value is commonly assigned to a variable for later reference. Otherwise, we would lose access to it in memory right away."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -365,7 +365,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"6.0"
|
||||
"7.0"
|
||||
]
|
||||
},
|
||||
"execution_count": 11,
|
||||
|
|
@ -493,7 +493,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"[PythonTutor](http://pythontutor.com/visualize.html#code=nums%20%3D%20%5B1,%202,%203,%204,%205,%206,%207,%208,%209,%2010%5D%0A%0Adef%20average_evens%28numbers%29%3A%0A%20%20%20%20evens%20%3D%20%5Bn%20for%20n%20in%20numbers%20if%20n%20%25%202%20%3D%3D%200%5D%0A%20%20%20%20average%20%3D%20sum%28evens%29%20/%20len%28evens%29%0A%20%20%20%20return%20average%0A%0Arv%20%3D%20average_evens%28nums%29&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false) visualizes what happens in memory: To be precise, in the exact moment when the function call is initiated and `nums` passed in as the `numbers` argument, there are *two* pointers to the *same* `list` object (cf., steps 4-5 in the visualization). We also see how Python creates a *new* **frame** that holds the function's local scope (i.e., \"internal names\") in addition to the **global** frame. Frames are nothing but [namespaces](https://en.wikipedia.org/wiki/Namespace) to *isolate* the names of different **scopes** from each other. The list comprehension `[n for n in numbers if n % 2 == 0]` constitutes yet another frame that is in scope as the `list` object assigned to `evens` is *being* created (cf., steps 6-18). When the function returns, only the global frame is left (cf., steps 21-22)."
|
||||
"[PythonTutor](http://pythontutor.com/visualize.html#code=nums%20%3D%20%5B1,%202,%203,%204,%205,%206,%207,%208,%209,%2010,%2011,%2012%5D%0A%0Adef%20average_evens%28numbers%29%3A%0A%20%20%20%20evens%20%3D%20%5Bn%20for%20n%20in%20numbers%20if%20n%20%25%202%20%3D%3D%200%5D%0A%20%20%20%20average%20%3D%20sum%28evens%29%20/%20len%28evens%29%0A%20%20%20%20return%20average%0A%0Arv%20%3D%20average_evens%28nums%29&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false) visualizes what happens in memory: To be precise, in the exact moment when the function call is initiated and `nums` passed in as the `numbers` argument, there are *two* pointers to the *same* `list` object (cf., steps 4-5 in the visualization). We also see how Python creates a *new* **frame** that holds the function's local scope (i.e., \"internal names\") in addition to the **global** frame. Frames are nothing but [namespaces](https://en.wikipedia.org/wiki/Namespace) to *isolate* the names of different **scopes** from each other. The list comprehension `[n for n in numbers if n % 2 == 0]` constitutes yet another frame that is in scope as the `list` object assigned to `evens` is *being* created (cf., steps 6-20). When the function returns, only the global frame is left (cf., last step)."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -565,7 +565,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
|
||||
"[7, 11, 8, 5, 3, 12, 2, 6, 9, 10, 1, 4]"
|
||||
]
|
||||
},
|
||||
"execution_count": 16,
|
||||
|
|
@ -600,7 +600,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"6.0"
|
||||
"7.0"
|
||||
]
|
||||
},
|
||||
"execution_count": 17,
|
||||
|
|
@ -635,7 +635,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"6.0"
|
||||
"7.0"
|
||||
]
|
||||
},
|
||||
"execution_count": 18,
|
||||
|
|
@ -655,7 +655,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"[PythonTutor](http://pythontutor.com/visualize.html#code=nums%20%3D%20%5B1,%202,%203,%204,%205,%206,%207,%208,%209,%2010%5D%0A%0Adef%20average_wrong%28numbers%29%3A%0A%20%20%20%20evens%20%3D%20%5Bn%20for%20n%20in%20nums%20if%20n%20%25%202%20%3D%3D%200%5D%0A%20%20%20%20average%20%3D%20sum%28evens%29%20/%20len%28evens%29%0A%20%20%20%20return%20average%0A%0Arv%20%3D%20average_wrong%28%5B123,%20456,%20789%5D%29&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false) is again helpful at visualizing the error interactively: Creating the `list` object `evens` eventually points to takes *12* computational steps, namely one for setting up an empty `list` object, *ten* for filling it with elements derived from `nums` in the global scope, and one to make `evens` point at it (cf., steps 6-18).\n",
|
||||
"[PythonTutor](http://pythontutor.com/visualize.html#code=nums%20%3D%20%5B1,%202,%203,%204,%205,%206,%207,%208,%209,%2010,%2011,%2012%5D%0A%0Adef%20average_wrong%28numbers%29%3A%0A%20%20%20%20evens%20%3D%20%5Bn%20for%20n%20in%20nums%20if%20n%20%25%202%20%3D%3D%200%5D%0A%20%20%20%20average%20%3D%20sum%28evens%29%20/%20len%28evens%29%0A%20%20%20%20return%20average%0A%0Arv%20%3D%20average_wrong%28%5B123,%20456,%20789%5D%29&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false) is again helpful at visualizing the error interactively: Creating the `list` object `evens` eventually points to takes *16* computational steps, namely two for managing the list comprehension, one for setting up an empty `list` object, *twelve* for filling it with elements derived from `nums` in the global scope (i.e., that is the error), and one to make `evens` point at it (cf., steps 6-21).\n",
|
||||
"\n",
|
||||
"The frames logic shown by PythonTutor is the mechanism by which Python not only manages the names inside *one* function call but also for *many* potentially *simultaneous* calls, as revealed in [Chapter 4](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/04_iteration.ipynb#Trivial-Example:-Countdown). It is the reason why we may re-use the same names for the parameters and variables inside both `average_evens()` and `average_wrong()` without Python mixing them up. So, as we already read in the [Zen of Python](https://www.python.org/dev/peps/pep-0020/), \"namespaces are one honking great idea\" (cf., `import this`), and a frame is just a special kind of namespace."
|
||||
]
|
||||
|
|
@ -732,7 +732,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
|
||||
"[7, 11, 8, 5, 3, 12, 2, 6, 9, 10, 1, 4]"
|
||||
]
|
||||
},
|
||||
"execution_count": 20,
|
||||
|
|
@ -802,7 +802,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"5.0"
|
||||
"6.0"
|
||||
]
|
||||
},
|
||||
"execution_count": 22,
|
||||
|
|
@ -837,7 +837,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
|
||||
"[7, 11, 8, 5, 3, 12, 2, 6, 9, 10, 1, 4]"
|
||||
]
|
||||
},
|
||||
"execution_count": 23,
|
||||
|
|
@ -859,7 +859,7 @@
|
|||
"source": [
|
||||
"The reason why everything works is that *every time* we (re-)assign an object to a variable *inside* a function with the `=` statement, this is done in the *local* scope by default. There are ways to change variables existing in an outer scope from within a function, but this is a rather advanced topic on its own.\n",
|
||||
"\n",
|
||||
"[PythonTutor](http://pythontutor.com/visualize.html#code=nums%20%3D%20%5B1,%202,%203,%204,%205,%206,%207,%208,%209,%2010%5D%0A%0Adef%20average_odds%28numbers%29%3A%0A%20%20%20%20nums%20%3D%20%5Bint%28n%29%20for%20n%20in%20numbers%5D%0A%20%20%20%20odds%20%3D%20%5Bn%20for%20n%20in%20nums%20if%20n%20%25%202%20!%3D%200%5D%0A%20%20%20%20average%20%3D%20sum%28odds%29%20/%20len%28odds%29%0A%20%20%20%20return%20average%0A%0Arv%20%3D%20average_odds%28%5B1.0,%2010.0,%203.0,%2010.0,%205.0%5D%29&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false) shows how *two* `nums` variables exist in *different* scopes pointing to *different* objects (cf., steps 14-25) when we execute `average_odds([1.0, 10.0, 3.0, 10.0, 5.0])`.\n",
|
||||
"[PythonTutor](http://pythontutor.com/visualize.html#code=nums%20%3D%20%5B1,%202,%203,%204,%205,%206,%207,%208,%209,%2010,%2011,%2012%5D%0A%0Adef%20average_odds%28numbers%29%3A%0A%20%20%20%20nums%20%3D%20%5Bint%28n%29%20for%20n%20in%20numbers%5D%0A%20%20%20%20odds%20%3D%20%5Bn%20for%20n%20in%20nums%20if%20n%20%25%202%20!%3D%200%5D%0A%20%20%20%20average%20%3D%20sum%28odds%29%20/%20len%28odds%29%0A%20%20%20%20return%20average%0A%0Arv%20%3D%20average_odds%28%5B1.0,%2010.0,%203.0,%2010.0,%205.0%5D%29&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false) shows how *two* `nums` variables exist in *different* scopes pointing to *different* objects (cf., steps 14-25) when we execute `average_odds([1.0, 10.0, 3.0, 10.0, 5.0])`.\n",
|
||||
"\n",
|
||||
"Variables whose names collide with the ones of variables in enclosing scopes - and the global scope is just the most enclosing scope - are said to **shadow** them.\n",
|
||||
"\n",
|
||||
|
|
@ -902,7 +902,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"10"
|
||||
"12"
|
||||
]
|
||||
},
|
||||
"execution_count": 24,
|
||||
|
|
@ -926,7 +926,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"55"
|
||||
"78"
|
||||
]
|
||||
},
|
||||
"execution_count": 25,
|
||||
|
|
@ -946,7 +946,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"We may cast objects as a different type. For example, to \"convert\" a float or a text into an integer, we use the [int()](https://docs.python.org/3/library/functions.html#int) built-in. It creates a *new* object of type `int` from the provided `avg` or `\"6\"` objects that continue to exist in memory unchanged."
|
||||
"We may cast objects as a different type. For example, to \"convert\" a float or a text into an integer, we use the [int()](https://docs.python.org/3/library/functions.html#int) built-in. It creates a *new* object of type `int` from the provided `avg` or `\"7\"` objects that continue to exist in memory unchanged."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -961,7 +961,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"6.0"
|
||||
"7.0"
|
||||
]
|
||||
},
|
||||
"execution_count": 26,
|
||||
|
|
@ -985,7 +985,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"6"
|
||||
"7"
|
||||
]
|
||||
},
|
||||
"execution_count": 27,
|
||||
|
|
@ -1009,7 +1009,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"6"
|
||||
"7"
|
||||
]
|
||||
},
|
||||
"execution_count": 28,
|
||||
|
|
@ -1018,7 +1018,7 @@
|
|||
}
|
||||
],
|
||||
"source": [
|
||||
"int(\"6\")"
|
||||
"int(\"7\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -1029,7 +1029,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"Observe that casting as an integer is different from rounding with the [round()](https://docs.python.org/3/library/functions.html#round) built-in function."
|
||||
"Casting as an `int` object is different from rounding with the built-in [round()](https://docs.python.org/3/library/functions.html#round) function!"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -1102,18 +1102,18 @@
|
|||
"outputs": [
|
||||
{
|
||||
"ename": "ValueError",
|
||||
"evalue": "invalid literal for int() with base 10: 'six'",
|
||||
"evalue": "invalid literal for int() with base 10: 'seven'",
|
||||
"output_type": "error",
|
||||
"traceback": [
|
||||
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
||||
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
|
||||
"\u001b[0;32m<ipython-input-31-28b6aa9c0167>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"six\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
|
||||
"\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: 'six'"
|
||||
"\u001b[0;32m<ipython-input-31-af421b358f21>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"seven\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
|
||||
"\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: 'seven'"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"int(\"six\")"
|
||||
"int(\"seven\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -1281,7 +1281,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"12.0"
|
||||
"14.0"
|
||||
]
|
||||
},
|
||||
"execution_count": 36,
|
||||
|
|
@ -1318,7 +1318,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"12.0"
|
||||
"14.0"
|
||||
]
|
||||
},
|
||||
"execution_count": 37,
|
||||
|
|
@ -1342,7 +1342,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"12.0"
|
||||
"14.0"
|
||||
]
|
||||
},
|
||||
"execution_count": 38,
|
||||
|
|
@ -1366,7 +1366,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"12.0"
|
||||
"14.0"
|
||||
]
|
||||
},
|
||||
"execution_count": 39,
|
||||
|
|
@ -1483,7 +1483,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"The outcome of `average_evens(nums)` is, of course, still `6.0`."
|
||||
"The outcome of `average_evens(nums)` is, of course, still `7.0`."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -1498,7 +1498,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"6.0"
|
||||
"7.0"
|
||||
]
|
||||
},
|
||||
"execution_count": 43,
|
||||
|
|
@ -1555,7 +1555,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"Now we call the function either with or without the `scalar` argument.\n",
|
||||
"Now, we call the function either with or without the `scalar` argument.\n",
|
||||
"\n",
|
||||
"If `scalar` is to be passed in, this may be done as either a positional or a keyword argument. Which of the two versions where `scalar` is `2` is more \"natural\" and faster to comprehend in a larger program?"
|
||||
]
|
||||
|
|
@ -1572,7 +1572,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"6.0"
|
||||
"7.0"
|
||||
]
|
||||
},
|
||||
"execution_count": 45,
|
||||
|
|
@ -1596,7 +1596,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"12.0"
|
||||
"14.0"
|
||||
]
|
||||
},
|
||||
"execution_count": 46,
|
||||
|
|
@ -1620,7 +1620,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"12.0"
|
||||
"14.0"
|
||||
]
|
||||
},
|
||||
"execution_count": 47,
|
||||
|
|
@ -1705,7 +1705,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"6.0"
|
||||
"7.0"
|
||||
]
|
||||
},
|
||||
"execution_count": 49,
|
||||
|
|
@ -1729,7 +1729,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"12.0"
|
||||
"14.0"
|
||||
]
|
||||
},
|
||||
"execution_count": 50,
|
||||
|
|
@ -1749,7 +1749,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"If we pass in `scalar` as a positional argument instead, we obtain a `TypeError`."
|
||||
"If we pass in `scalar` as a positional argument instead, we get a `TypeError`."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -1841,11 +1841,11 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"If you think this is a rather pointless thing to do, you are absolutely correct!\n",
|
||||
"If you think this is rather pointless to do, you are absolutely correct!\n",
|
||||
"\n",
|
||||
"We created a `function` object, dit *not* call it, and Python immediately forgot about it. So what's the point?\n",
|
||||
"\n",
|
||||
"To prove that a `lambda` expression creates a callable `function` object, we use the simple `=` statement to assign it to the variable `add_three`, which is really `add_three()` as per our convention from above."
|
||||
"To show that a `lambda` expression creates a callable `function` object, we use the simple `=` statement to assign it to the variable `add_three`, which is really `add_three()` as per our convention from above."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -1928,7 +1928,7 @@
|
|||
}
|
||||
],
|
||||
"source": [
|
||||
"(lambda x: x + 3)(39) # this looks weird but will become very useful"
|
||||
"(lambda x: x + 3)(39)"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -1939,7 +1939,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"The main point of having functions without a name is to use them in a situation where we know ahead of time that we use the function *once* only.\n",
|
||||
"The main point of having functions without a name is to use them in a situation where we know ahead of time that we use the function only *once*.\n",
|
||||
"\n",
|
||||
"Popular applications of lambda expressions occur in combination with the **map-filter-reduce** paradigm or when we do \"number crunching\" with **arrays** and **data frames**. We look at both in detail in [Chapter 7](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/07_sequences.ipynb)."
|
||||
]
|
||||
|
|
@ -2021,7 +2021,7 @@
|
|||
"source": [
|
||||
"The [math](https://docs.python.org/3/library/math.html) module provides non-trivial mathematical functions like $sin(x)$ and constants like $\\pi$ or $\\text{e}$.\n",
|
||||
"\n",
|
||||
"To make functions and variables defined \"somewhere else\" available in our current program, we must first **[import](https://docs.python.org/3/reference/simple_stmts.html#import)** them with the `import` statement. "
|
||||
"To make functions and variables defined \"somewhere else\" available in our current program, we must first **import** them with the `import` statement (cf., [reference](https://docs.python.org/3/reference/simple_stmts.html#import)). "
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -2084,7 +2084,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"139829616976344"
|
||||
"139646755983752"
|
||||
]
|
||||
},
|
||||
"execution_count": 58,
|
||||
|
|
@ -2489,7 +2489,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"Often, we need a random variable, for example, when we want to build a simulation program. The [random](https://docs.python.org/3/library/random.html) module in the [standard library](https://docs.python.org/3/library/index.html) often suffices for that."
|
||||
"Often, we need a random variable, for example, when we want to build a simulation. The [random](https://docs.python.org/3/library/random.html) module in the [standard library](https://docs.python.org/3/library/index.html) often suffices for that."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -2697,7 +2697,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"0.4165845384207939"
|
||||
"0.21932911318720072"
|
||||
]
|
||||
},
|
||||
"execution_count": 75,
|
||||
|
|
@ -2732,7 +2732,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"<bound method Random.choice of <random.Random object at 0x56279067aaf8>>"
|
||||
"<bound method Random.choice of <random.Random object at 0x55a6c6c03af8>>"
|
||||
]
|
||||
},
|
||||
"execution_count": 76,
|
||||
|
|
@ -2781,7 +2781,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"2"
|
||||
"7"
|
||||
]
|
||||
},
|
||||
"execution_count": 78,
|
||||
|
|
@ -2801,7 +2801,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"To reproduce the same random numbers in a simulation each time we run it, we set the **[random seed](https://en.wikipedia.org/wiki/Random_seed)**. It is good practice to do this at the beginning of a program or notebook. Then every time we re-start the program, we get the *same* random numbers again. This becomes very important, for example, when we employ machine learning algorithms that rely on randomization, like the infamous [Random Forest](https://en.wikipedia.org/wiki/Random_forest), and want to obtain **reproducable** results.\n",
|
||||
"To reproduce the *same* random numbers in a simulation each time we run it, we set the **[random seed](https://en.wikipedia.org/wiki/Random_seed)**. It is good practice to do that at the beginning of a program or notebook. It becomes essential when we employ randomized machine learning algorithms, like the [Random Forest](https://en.wikipedia.org/wiki/Random_forest), and want to obtain **reproducible** results for publication in academic journals.\n",
|
||||
"\n",
|
||||
"The [random](https://docs.python.org/3/library/random.html) module provides the [random.seed()](https://docs.python.org/3/library/random.html#random.seed) function to do that."
|
||||
]
|
||||
|
|
@ -3050,7 +3050,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])"
|
||||
"array([ 7, 11, 8, 5, 3, 12, 2, 6, 9, 10, 1, 4])"
|
||||
]
|
||||
},
|
||||
"execution_count": 87,
|
||||
|
|
@ -3111,7 +3111,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20])"
|
||||
"array([14, 22, 16, 10, 6, 24, 4, 12, 18, 20, 2, 8])"
|
||||
]
|
||||
},
|
||||
"execution_count": 89,
|
||||
|
|
@ -3146,7 +3146,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
|
||||
"[7, 11, 8, 5, 3, 12, 2, 6, 9, 10, 1, 4, 7, 11, 8, 5, 3, 12, 2, 6, 9, 10, 1, 4]"
|
||||
]
|
||||
},
|
||||
"execution_count": 90,
|
||||
|
|
@ -3181,7 +3181,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"55"
|
||||
"78"
|
||||
]
|
||||
},
|
||||
"execution_count": 91,
|
||||
|
|
@ -3205,7 +3205,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"1"
|
||||
"7"
|
||||
]
|
||||
},
|
||||
"execution_count": 92,
|
||||
|
|
@ -3389,7 +3389,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"6.0"
|
||||
"7.0"
|
||||
]
|
||||
},
|
||||
"execution_count": 97,
|
||||
|
|
@ -3413,7 +3413,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"12.0"
|
||||
"14.0"
|
||||
]
|
||||
},
|
||||
"execution_count": 98,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue