fix of invalid notebook

pull/10/head
Kenneth C. Kleissl 2020-01-10 11:37:52 +01:00
parent c20a2649dd
commit 42ed0e5518
3 changed files with 15 additions and 358 deletions

View File

@ -1701,7 +1701,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
"version": "3.7.5"
},
"latex_envs": {
"LaTeX_envs_menu_present": true,

View File

@ -1,337 +1,5 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"cell_style": "center"
},
"source": [
"\n",
"# 3. Functions\n",
"A **function** is a block of code that is first defined, and thereafter can be called to run as many times as needed. A function might have arguments, some of which can be optional if a default value is specified.\n",
"\n",
"A function is called by parentheses: `function_name()`. Arguments are placed inside the parentehes and comma separated if there are more than one.\n",
"Similar to `f(x, y)` from mathematics.\n",
"\n",
"A function can return one or more values to the caller. The values to return are put in the `return` statement. When the code hits a `return` statement the function terminates. If no `return` statement is given, the function will return `None`.\n",
"\n",
"The general syntax of a function is:\n",
"\n",
"~~~python\n",
"def function_name(arg1, arg2, default_arg1=0, default_arg2=None):\n",
" '''This is the docstring \n",
" \n",
" The docstring explains what the function does, so it is like a multiline comment. It does not have to be here, \n",
" but it is good practice to use them to document the code. They are especially useful for more complicated \n",
" functions, although functions should in general be kept as simple as possible.\n",
" Arguments could be explained together with their types (e.g. strings, lists, dicts etc.).\n",
" '''\n",
" \n",
" # Function code goes here\n",
" \n",
" # Possible 'return' statement terminating the function. If 'return' is not specified, function returns None.\n",
" return return_val1, return_val2\n",
"~~~\n",
"\n",
"If multiple values are to be returned, they can be separated by commas as shown. The returned entity will by default be a `tuple`.\n",
"\n",
"Note that when using default arguments, it is good practice to only use immutable types. An example further below will demonstrate why this is recommended. \n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Basic functions\n",
"A simple function with one argument is defined below."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<style>\n",
"/* div#notebook {\n",
" font-size: 13pt;\n",
" line-height: 120%;\n",
" color: #303030;\n",
" -webkit-font-smoothing: antialiased !important;\n",
" padding-top: 25px !important;\n",
"} */\n",
"\n",
"\n",
"body,\n",
"div.body {\n",
" font-family: Roboto;\n",
" /* font-size: 16pt; */\n",
" color: #303030;\n",
" /* background-color: #d1b5b5; */\n",
" /* background: #8f4f4f; */\n",
" margin-right: 10px;\n",
" /* -webkit-font-smoothing: antialiased !important; */\n",
"}\n",
"\n",
"/* Code inside HTML/Markdown */\n",
"div.rendered_html code {\n",
" border-radius: 5px;\n",
"}\n",
"\n",
"/* Output area from code cells */\n",
"div.output_area pre {\n",
" /* font-size: 11pt !important; */\n",
" color: #303030;\n",
"}\n",
"\n",
"\n",
"\n",
"/* HEADING 1 styles */\n",
"h1 {\n",
" font-family: 'Roboto', 'Signika', sans-serif;\n",
" font-weight: ligher;\n",
" font-style: normal;\n",
" /* font-size: 20pt; */\n",
" width: 100%;\n",
" text-align: left;\n",
" color: #EE7631;\n",
" border-bottom: 1px solid rgba(238, 118, 49, 0.575);\n",
" padding-bottom: 6px;\n",
" /* font-variant: small-caps; */\n",
" }\n",
" \n",
" /* table {\n",
" margin: 5px;\n",
" width: 290px;\n",
" }\n",
" \n",
" th {\n",
" padding: 3px;\n",
" }\n",
" \n",
" td {\n",
" padding-left: 8px;\n",
" padding-right: 8px;\n",
" border: 1px solid #990000;\n",
" background-color: #ffffcc;\n",
" }\n",
"\n",
" #trHeader {\n",
" text-decoration: underline;\n",
" color: #990000;\n",
" }\n",
" \n",
" .centerCell {\n",
" text-align: center;\n",
" } */\n",
"\n",
"/* HEADING 2 styles */\n",
"h2 {\n",
" font-family: \"Roboto\";\n",
" text-align: left;\n",
" /* font-size: 12pt; */\n",
" color: #EE7631;\n",
" margin-bottom: 5px;\n",
" /* border-bottom: 1px solid lightgray; */\n",
" border-bottom: 0.8px solid rgba(238, 118, 49, 0.575);\n",
" padding-bottom: 6px\n",
" }\n",
" \n",
" /* table {\n",
" margin: 5px;\n",
" width: 290px;\n",
" }\n",
" \n",
" th {\n",
" padding: 3px;\n",
" }\n",
" \n",
" td {\n",
" padding-left: 8px;\n",
" padding-right: 8px;\n",
" border: 1px solid #990000;\n",
" background-color: #ffffcc;\n",
" }\n",
"\n",
" #trHeader {\n",
" text-decoration: underline;\n",
" color: #990000;\n",
" }\n",
" \n",
" .centerCell {\n",
" text-align: center;\n",
" } */\n",
"\n",
"h3 {\n",
" font-family: 'Roboto';\n",
" /* text-align: left; */\n",
" /* font-size: 12pt; */\n",
" /* color: #EE7631; */\n",
" /* margin-bottom: 5px; */\n",
" border-bottom: 0.5px solid #ededed;\n",
" padding-bottom: 6px\n",
" }\n",
"\n",
"\n",
"\n",
"p {\n",
" font-family: Roboto;\n",
" font-size: 16px;\n",
"}\n",
"\n",
"/* Lists with dots */\n",
"ul {\n",
" font-size: 16px;\n",
" line-height: 150%;\n",
"}\n",
"\n",
"/* Lists with numbers */\n",
"ol {\n",
" font-size: 16px;\n",
" line-height: 150%;\n",
"}\n",
"\n",
"/* Horizontal rules */\n",
"hr { \n",
" margin-top: 3px; \n",
" margin-bottom: 3px \n",
"}\n",
"\n",
"/* Links */\n",
"a {\n",
" color: #EE7631;\n",
"}\n",
"\n",
"\n",
"/* Change global font size (for code) */\n",
".CodeMirror {\n",
"font-size: 16px !important;\n",
"font-family: monospace;\n",
"}\n",
"\n",
"\n",
"/* Code cells in the notebook - NOTE: color is font-color */\n",
".cm-s-ipython.CodeMirror {\n",
" font-family: monospace, monospace;\n",
" /* font-size: 11pt; */\n",
" background: #ededed;\n",
" color: #303030; \n",
" border-radius: 2px;\n",
" /* margin-right: 10px; */\n",
" font-style: normal;\n",
" font-weight: normal;\n",
"}\n",
"\n",
"\n",
"/* Background of code cells */\n",
".cm-s-ipython.CodeMirror {\n",
" font-family: monospace, monospace;\n",
" /* font-size: 11pt; */\n",
" background: rgba(211, 211, 211, 0.123);\n",
" color: #303030;\n",
" border-radius: 2px;\n",
" font-style: normal;\n",
" font-weight: normal;\n",
"}\n",
"\n",
"\n",
"/* .CodeMirror-gutters {\n",
" border: none;\n",
" border-right: 1px solid #e0e1e3 !important;\n",
" background-color: #e0e1e3 !important;\n",
" background: #e0e1e3 !important;\n",
" border-radius: 0px;\n",
" white-space: nowrap;\n",
"} */\n",
"\n",
"\n",
"\n",
"/* Code syntax highlithig theme */\n",
".cm-s-ipython .CodeMirror-cursor {\n",
" border-left: 2px solid #ff711a !important;\n",
"}\n",
".cm-s-ipython span.cm-comment {\n",
" color: #8d8d8d;\n",
" font-style: italic; \n",
"}\n",
".cm-s-ipython span.cm-atom {\n",
" color: #055be0;\n",
"}\n",
".cm-s-ipython span.cm-number {\n",
" color: #ff8132;\n",
"}\n",
".cm-s-ipython span.cm-property {\n",
" color: #303030;\n",
"}\n",
".cm-s-ipython span.cm-attribute {\n",
" color: #303030;\n",
"}\n",
".cm-s-ipython span.cm-keyword {\n",
" color: #a045ddf3;\n",
" font-weight: normal;\n",
"}\n",
".cm-s-ipython span.cm-string {\n",
" color: #009e07;\n",
"}\n",
".cm-s-ipython span.cm-meta {\n",
" color: #aa22ff;\n",
"}\n",
".cm-s-ipython span.cm-operator {\n",
" color: #055be0;\n",
"}\n",
".cm-s-ipython span.cm-builtin {\n",
" color: #3f2ce7;\n",
"}\n",
".cm-s-ipython span.cm-variable {\n",
" color: #303030;\n",
"}\n",
".cm-s-ipython span.cm-variable-2 {\n",
" color: #de143d;\n",
"}\n",
".cm-s-ipython span.cm-variable-3 {\n",
" color: #aa22ff;\n",
"}\n",
".cm-s-ipython span.cm-def {\n",
" color: #e22978;\n",
" font-weight: normal;\n",
"}\n",
".cm-s-ipython span.cm-error {\n",
" background: rgba(191,11,55,.70);\n",
"}\n",
".cm-s-ipython span.cm-tag {\n",
" color: #e22978;\n",
"}\n",
".cm-s-ipython span.cm-link {\n",
" color: #ef5c00;\n",
"}\n",
".cm-s-ipython span.cm-storage {\n",
" color: #055be0;\n",
"}\n",
".cm-s-ipython span.cm-entity {\n",
" color: #e22978;\n",
"}\n",
".cm-s-ipython span.cm-quote {\n",
" color: #009e07;\n",
"}\n",
"</style>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from IPython.display import HTML\n",
"HTML('<style>{}</style>'.format(open('../css/cowi.css').read()))"
]
},
{
"cell_type": "markdown",
"metadata": {
@ -400,7 +68,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 3,
"metadata": {},
"outputs": [
{
@ -409,7 +77,7 @@
"36.25"
]
},
"execution_count": 2,
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
@ -427,7 +95,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 4,
"metadata": {},
"outputs": [
{
@ -459,7 +127,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 5,
"metadata": {},
"outputs": [
{
@ -494,18 +162,7 @@
},
{
"cell_type": "markdown",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "invalid syntax (<ipython-input-1-febc0acd8c48>, line 3)",
"output_type": "error",
"traceback": [
"\u001b[1;36m File \u001b[1;32m\"<ipython-input-1-febc0acd8c48>\"\u001b[1;36m, line \u001b[1;32m3\u001b[0m\n\u001b[1;33m The built-in `enumerate` is useful when you want to loop over an iterable together with the index of each of its elements:\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
]
}
],
"source": [
"## Examples of built-in functions\n",
"### Using `enumerate` for looping in index/value pairs\n",
@ -514,7 +171,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 6,
"metadata": {},
"outputs": [
{
@ -540,7 +197,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 7,
"metadata": {},
"outputs": [
{
@ -572,7 +229,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 8,
"metadata": {},
"outputs": [
{
@ -610,7 +267,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 9,
"metadata": {},
"outputs": [
{
@ -646,7 +303,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 10,
"metadata": {},
"outputs": [
{
@ -676,7 +333,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 11,
"metadata": {},
"outputs": [
{
@ -916,7 +573,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 12,
"metadata": {},
"outputs": [
{
@ -1189,7 +846,7 @@
"<IPython.core.display.HTML object>"
]
},
"execution_count": 11,
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
@ -1217,7 +874,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
"version": "3.7.5"
},
"latex_envs": {
"LaTeX_envs_menu_present": true,

View File

@ -1106,7 +1106,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
"version": "3.7.5"
},
"latex_envs": {
"LaTeX_envs_menu_present": true,