Skip to content

Commit 8b6b7f5

Browse files
author
nbgitpuller
committed
started 005
1 parent b091bb5 commit 8b6b7f5

File tree

1 file changed

+100
-9
lines changed

1 file changed

+100
-9
lines changed

Diff for: 005_Write_Your_Own_Functions.ipynb

+100-9
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,89 @@
2121
"# Write Your Own Functions"
2222
]
2323
},
24+
{
25+
"cell_type": "markdown",
26+
"metadata": {},
27+
"source": [
28+
"In the past notebooks, we have met (and used) several of the many functions that are shipped with Python."
29+
]
30+
},
31+
{
32+
"cell_type": "markdown",
33+
"metadata": {},
34+
"source": [
35+
"It is now time to empower you with the capability to create your own function. But what is a function?"
36+
]
37+
},
2438
{
2539
"cell_type": "markdown",
2640
"metadata": {},
2741
"source": [
2842
"<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/key.png\">\n",
2943
"\n",
30-
"A key concept."
44+
"A **function** is a named sequence of statements."
45+
]
46+
},
47+
{
48+
"cell_type": "markdown",
49+
"metadata": {},
50+
"source": [
51+
"## Function declaration"
52+
]
53+
},
54+
{
55+
"cell_type": "markdown",
56+
"metadata": {},
57+
"source": [
58+
"If you want to create your own function:\n",
59+
"\n",
60+
"1. You first need to think at a meaningful name to give to it. \n",
61+
"2. You put the selected name after the `def` keyword, followed by curved parentheses and ending with a `:`.\n",
62+
"3. Since the following line, you indent the body of the function (that is, the code that has to be executed)."
63+
]
64+
},
65+
{
66+
"cell_type": "code",
67+
"execution_count": 1,
68+
"metadata": {},
69+
"outputs": [],
70+
"source": [
71+
"def print_temp_list():\n",
72+
" temp_list = [23.0, 19.2, 18.5, 21.3, 20.4]\n",
73+
" print(temp_list)"
74+
]
75+
},
76+
{
77+
"cell_type": "markdown",
78+
"metadata": {},
79+
"source": [
80+
"When you execute the above **Code** cell, nothing is printed. No worries, this is correct! We have **only** declared the function. We can now call the declared function:"
81+
]
82+
},
83+
{
84+
"cell_type": "code",
85+
"execution_count": 2,
86+
"metadata": {},
87+
"outputs": [
88+
{
89+
"name": "stdout",
90+
"output_type": "stream",
91+
"text": [
92+
"[23.0, 19.2, 18.5, 21.3, 20.4]\n"
93+
]
94+
}
95+
],
96+
"source": [
97+
"print_temp_list()"
98+
]
99+
},
100+
{
101+
"cell_type": "markdown",
102+
"metadata": {},
103+
"source": [
104+
"<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/key.png\">\n",
105+
"\n",
106+
"A function has to be first declared using the `def` keyword, than it can be called (i.e., executed) multiple times."
31107
]
32108
},
33109
{
@@ -40,32 +116,47 @@
40116
{
41117
"cell_type": "markdown",
42118
"metadata": {
43-
"solution2": "hidden",
119+
"solution2": "shown",
44120
"solution2_first": true
45121
},
46122
"source": [
47123
"<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/test.png\">\n",
48124
"\n",
49-
"Write code that ..."
125+
"Modify the code to call a function that prints the number of elements in the `sal_list`."
50126
]
51127
},
52128
{
53129
"cell_type": "code",
54-
"execution_count": 2,
130+
"execution_count": 4,
55131
"metadata": {
56-
"solution2": "hidden"
132+
"solution2": "shown"
57133
},
58-
"outputs": [],
134+
"outputs": [
135+
{
136+
"name": "stdout",
137+
"output_type": "stream",
138+
"text": [
139+
"Nr. of elements: 7\n"
140+
]
141+
}
142+
],
59143
"source": [
60-
"x = 0"
144+
"def print_len_sal_list():\n",
145+
" sal_list = [32.6, 33.3, 34.8, 32.4, 34.3, 33.7, 32.3] # water salinity in PSU\n",
146+
" len_sal_list = len(sal_list)\n",
147+
" print(\"Nr. of elements: \" + str(len_sal_list))\n",
148+
" \n",
149+
"print_len_sal_list()"
61150
]
62151
},
63152
{
64153
"cell_type": "code",
65-
"execution_count": null,
154+
"execution_count": 5,
66155
"metadata": {},
67156
"outputs": [],
68-
"source": []
157+
"source": [
158+
"sal_list = [32.6, 33.3, 34.8, 32.4, 34.3, 33.7, 32.3]"
159+
]
69160
},
70161
{
71162
"cell_type": "markdown",

0 commit comments

Comments
 (0)