|
11 | 11 | "- Exercises: 20 min\n",
|
12 | 12 | "\n",
|
13 | 13 | "**Questions**\n",
|
14 |
| - "- \"How can I use built-in functions?\"\n", |
15 |
| - "- \"How can I find out what they do?\"\n", |
| 14 | + "- How can I use built-in functions?\n", |
| 15 | + "- How can I find out what they do?\n", |
16 | 16 | "\n",
|
17 | 17 | "**Learning Objectives**\n",
|
18 |
| - "- \"Explain the purpose of functions.\"\n", |
19 |
| - "- \"Correctly call built-in Python functions.\"\n", |
20 |
| - "- \"Correctly nest calls to built-in functions.\"\n", |
21 |
| - "- \"Use help to display [documentation](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#documentation) for built-in functions.\"\n", |
| 18 | + "- Explain the purpose of functions.\n", |
| 19 | + "- Correctly call built-in Python functions.\n", |
| 20 | + "- Correctly nest calls to built-in functions.\n", |
| 21 | + "- Use help to display [documentation](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#documentation) for built-in functions.\n", |
22 | 22 | "\n",
|
23 | 23 | "*****"
|
24 | 24 | ]
|
|
27 | 27 | "cell_type": "markdown",
|
28 | 28 | "metadata": {},
|
29 | 29 | "source": [
|
30 |
| - "## A function may take zero or more arguments.\n", |
| 30 | + "## A function may take zero or more arguments\n", |
31 | 31 | "\n",
|
32 |
| - "* We have seen some functions already --- now let's take a closer look.\n", |
| 32 | + "* We have seen some functions already - now let's take a closer look.\n", |
33 | 33 | "* An *argument* is a value passed into a function.\n",
|
34 | 34 | "* `len` takes exactly one.\n",
|
35 | 35 | "* `int`, `str`, and `float` create a new value from an existing one.\n",
|
36 | 36 | "* `print` takes zero or more.\n",
|
37 | 37 | "* `print` with no arguments prints a blank line.\n",
|
38 |
| - " * Must always use parentheses, even if they're empty,\n", |
39 |
| - " so that Python knows a function is being called." |
| 38 | + "* Functions must always be called with parentheses, even if there are no arguments." |
40 | 39 | ]
|
41 | 40 | },
|
42 | 41 | {
|
43 | 42 | "cell_type": "code",
|
44 |
| - "execution_count": null, |
45 |
| - "metadata": {}, |
46 |
| - "outputs": [], |
| 43 | + "execution_count": 1, |
| 44 | + "metadata": {}, |
| 45 | + "outputs": [ |
| 46 | + { |
| 47 | + "name": "stdout", |
| 48 | + "output_type": "stream", |
| 49 | + "text": [ |
| 50 | + "before\n", |
| 51 | + "\n", |
| 52 | + "after\n" |
| 53 | + ] |
| 54 | + } |
| 55 | + ], |
47 | 56 | "source": [
|
48 | 57 | "print('before')\n",
|
49 | 58 | "print()\n",
|
|
54 | 63 | "cell_type": "markdown",
|
55 | 64 | "metadata": {},
|
56 | 65 | "source": [
|
57 |
| - "## Commonly-used built-in functions include `max`, `min`, and `round`.\n", |
| 66 | + "## Commonly-used built-in functions include `max`, `min`, and `round`\n", |
58 | 67 | "\n",
|
| 68 | + "* Built-in functions are those that already exist in Python (in contrast to custom functions that a user can create).\n", |
| 69 | + "* Some common built-in functions include `max`, `min`, and `round`.\n", |
59 | 70 | "* Use `max` to find the largest value of one or more values.\n",
|
60 | 71 | "* Use `min` to find the smallest.\n",
|
61 | 72 | "* Both work on character strings as well as numbers.\n",
|
62 |
| - " * \"Larger\" and \"smaller\" use (0-9, A-Z, a-z) to compare letters." |
| 73 | + "* \"Larger\" and \"smaller\" use (0-9, A-Z, a-z) to compare letters." |
63 | 74 | ]
|
64 | 75 | },
|
65 | 76 | {
|
66 | 77 | "cell_type": "code",
|
67 |
| - "execution_count": null, |
68 |
| - "metadata": {}, |
69 |
| - "outputs": [], |
| 78 | + "execution_count": 2, |
| 79 | + "metadata": {}, |
| 80 | + "outputs": [ |
| 81 | + { |
| 82 | + "name": "stdout", |
| 83 | + "output_type": "stream", |
| 84 | + "text": [ |
| 85 | + "3\n", |
| 86 | + "0\n" |
| 87 | + ] |
| 88 | + } |
| 89 | + ], |
70 | 90 | "source": [
|
71 | 91 | "print(max(1, 2, 3))\n",
|
72 | 92 | "print(min('a', 'A', '0'))"
|
|
76 | 96 | "cell_type": "markdown",
|
77 | 97 | "metadata": {},
|
78 | 98 | "source": [
|
79 |
| - "## Functions may only work for certain (combinations of) arguments.\n", |
| 99 | + "## Functions may operate on certain combinations of arguments\n", |
80 | 100 | "\n",
|
81 | 101 | "* `max` and `min` must be given at least one argument.\n",
|
82 |
| - " * \"Largest of the empty set\" is a meaningless question.\n", |
83 |
| - "* And they must be given things that can meaningfully be compared.\n" |
| 102 | + "* For example, \"Largest of the empty set\" is a meaningless question.\n", |
| 103 | + "* Furthermore, `max` requires types that are comparable (i.e., strings and ints can't be compared to each other)." |
84 | 104 | ]
|
85 | 105 | },
|
86 | 106 | {
|
87 | 107 | "cell_type": "code",
|
88 |
| - "execution_count": null, |
| 108 | + "execution_count": 3, |
89 | 109 | "metadata": {},
|
90 |
| - "outputs": [], |
| 110 | + "outputs": [ |
| 111 | + { |
| 112 | + "ename": "TypeError", |
| 113 | + "evalue": "max expected at least 1 argument, got 0", |
| 114 | + "output_type": "error", |
| 115 | + "traceback": [ |
| 116 | + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", |
| 117 | + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", |
| 118 | + "\u001b[0;32m/var/folders/6h/ms_dpkl536d1w6qqd_wnckrw0000gn/T/ipykernel_40851/3540540054.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmax\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", |
| 119 | + "\u001b[0;31mTypeError\u001b[0m: max expected at least 1 argument, got 0" |
| 120 | + ] |
| 121 | + } |
| 122 | + ], |
| 123 | + "source": [ |
| 124 | + "print(max())" |
| 125 | + ] |
| 126 | + }, |
| 127 | + { |
| 128 | + "cell_type": "code", |
| 129 | + "execution_count": 4, |
| 130 | + "metadata": {}, |
| 131 | + "outputs": [ |
| 132 | + { |
| 133 | + "ename": "TypeError", |
| 134 | + "evalue": "'>' not supported between instances of 'str' and 'int'", |
| 135 | + "output_type": "error", |
| 136 | + "traceback": [ |
| 137 | + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", |
| 138 | + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", |
| 139 | + "\u001b[0;32m/var/folders/6h/ms_dpkl536d1w6qqd_wnckrw0000gn/T/ipykernel_40851/2220240766.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmax\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'a'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", |
| 140 | + "\u001b[0;31mTypeError\u001b[0m: '>' not supported between instances of 'str' and 'int'" |
| 141 | + ] |
| 142 | + } |
| 143 | + ], |
91 | 144 | "source": [
|
92 | 145 | "print(max(1, 'a'))"
|
93 | 146 | ]
|
|
96 | 149 | "cell_type": "markdown",
|
97 | 150 | "metadata": {},
|
98 | 151 | "source": [
|
99 |
| - "## Functions may have default values for some arguments.\n", |
| 152 | + "## Functions may have default values for some arguments\n", |
100 | 153 | "\n",
|
101 |
| - "* `round` will round off a floating-point number.\n", |
102 |
| - "* By default, rounds to zero decimal places." |
| 154 | + "* `round` will round off a floating-point number. It accepts two arguments: the number, and the number of decimal places to round off to.\n", |
| 155 | + "* By default, it rounds to zero decimal places." |
103 | 156 | ]
|
104 | 157 | },
|
105 | 158 | {
|
|
120 | 173 | },
|
121 | 174 | {
|
122 | 175 | "cell_type": "code",
|
123 |
| - "execution_count": null, |
124 |
| - "metadata": {}, |
125 |
| - "outputs": [], |
| 176 | + "execution_count": 5, |
| 177 | + "metadata": {}, |
| 178 | + "outputs": [ |
| 179 | + { |
| 180 | + "data": { |
| 181 | + "text/plain": [ |
| 182 | + "3.7" |
| 183 | + ] |
| 184 | + }, |
| 185 | + "execution_count": 5, |
| 186 | + "metadata": {}, |
| 187 | + "output_type": "execute_result" |
| 188 | + } |
| 189 | + ], |
126 | 190 | "source": [
|
127 | 191 | "round(3.712, 1)"
|
128 | 192 | ]
|
|
138 | 202 | },
|
139 | 203 | {
|
140 | 204 | "cell_type": "code",
|
141 |
| - "execution_count": null, |
142 |
| - "metadata": {}, |
143 |
| - "outputs": [], |
| 205 | + "execution_count": 6, |
| 206 | + "metadata": {}, |
| 207 | + "outputs": [ |
| 208 | + { |
| 209 | + "name": "stdout", |
| 210 | + "output_type": "stream", |
| 211 | + "text": [ |
| 212 | + "Help on built-in function round in module builtins:\n", |
| 213 | + "\n", |
| 214 | + "round(number, ndigits=None)\n", |
| 215 | + " Round a number to a given precision in decimal digits.\n", |
| 216 | + " \n", |
| 217 | + " The return value is an integer if ndigits is omitted or None. Otherwise\n", |
| 218 | + " the return value has the same type as the number. ndigits may be negative.\n", |
| 219 | + "\n" |
| 220 | + ] |
| 221 | + } |
| 222 | + ], |
144 | 223 | "source": [
|
145 | 224 | "help(round)"
|
146 | 225 | ]
|
|
149 | 228 | "cell_type": "markdown",
|
150 | 229 | "metadata": {},
|
151 | 230 | "source": [
|
152 |
| - "## The Jupyter Notebook has two ways to get help.\n", |
| 231 | + "## The Jupyter Notebook has two ways to get help\n", |
153 | 232 | "\n",
|
154 | 233 | "* Place the cursor inside the parenthesis of the function,\n",
|
155 |
| - " hold down `shift`,\n", |
156 |
| - " and press `tab`.\n", |
| 234 | + " hold down `Shift`,\n", |
| 235 | + " and press `Tab`.\n", |
157 | 236 | "* Or type a function name with a question mark after it.\n",
|
158 | 237 | "\n",
|
159 |
| - "## Every function returns something.\n", |
| 238 | + "## Every function returns a value\n", |
160 | 239 | "\n",
|
161 | 240 | "* Every [function call](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#function-call) produces some result.\n",
|
162 | 241 | "* If the function doesn't have a useful result to return,\n",
|
|
165 | 244 | },
|
166 | 245 | {
|
167 | 246 | "cell_type": "code",
|
168 |
| - "execution_count": null, |
169 |
| - "metadata": {}, |
170 |
| - "outputs": [], |
| 247 | + "execution_count": 7, |
| 248 | + "metadata": {}, |
| 249 | + "outputs": [ |
| 250 | + { |
| 251 | + "name": "stdout", |
| 252 | + "output_type": "stream", |
| 253 | + "text": [ |
| 254 | + "example\n", |
| 255 | + "result of print is None\n" |
| 256 | + ] |
| 257 | + } |
| 258 | + ], |
171 | 259 | "source": [
|
172 | 260 | "result = print('example')\n",
|
173 | 261 | "print('result of print is', result)"
|
|
177 | 265 | "cell_type": "markdown",
|
178 | 266 | "metadata": {},
|
179 | 267 | "source": [
|
180 |
| - "## Difference between Function, Method, Object\n", |
| 268 | + "## Functions, objects, and methods\n", |
181 | 269 | " \n",
|
182 |
| - "A **function** is a piece of code that is called by name. It can be passed data to operate on (ie. the parameters) and can optionally return data (the return value).\n", |
| 270 | + "A **function** is a block of code that can be reused. It can be passed data to operate on (ie. the parameters) and can optionally return data (the return value).\n", |
183 | 271 | "\n",
|
184 |
| - "A **method** is a function which is tied to a particular object. Each of an object's methods typically implements one of the things it can do, or one of the questions it can answer. It is called using the dot notation: e.g. `object.method()`\n", |
| 272 | + "An **object** is a collection of conceptually related variables and functions using those variables. Every [object](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#object) is an instance of a `class`, which is like a blueprint for an object. \n", |
185 | 273 | "\n",
|
186 |
| - "An **object** is a collection of conceptually related grouping of variables (called \"members\") and functions using those variables (called \"methods\"). Every [object](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#object) is an instance of a `class`, which is like a blueprint for an object. \n", |
| 274 | + "A **method** is a function which is tied to a particular object. Each of an object's methods typically implements one of the things it can do, or one of the questions it can answer. It is called using the dot notation: e.g. `object.method()`.\n", |
187 | 275 | "\n",
|
188 |
| - " - Everything that exists is an object.\n", |
189 |
| - " - Everything that happens is a function call.\n", |
| 276 | + "- In Python, everything is an object.\n", |
| 277 | + "- In Python, everything that happens is a function call.\n", |
190 | 278 | " \n",
|
191 |
| - "Read more about objects, classes and methods [here](https://docs.python.org/3/tutorial/classes.html)\n", |
| 279 | + "Read more about objects, classes and methods [here](https://docs.python.org/3/tutorial/classes.html).\n", |
192 | 280 | "\n",
|
193 | 281 | "Check out our Python glossary [here](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md)."
|
194 | 282 | ]
|
|
209 | 297 | "cell_type": "code",
|
210 | 298 | "execution_count": null,
|
211 | 299 | "metadata": {
|
212 |
| - "collapsed": true |
| 300 | + "collapsed": true, |
| 301 | + "jupyter": { |
| 302 | + "outputs_hidden": true |
| 303 | + } |
213 | 304 | },
|
214 | 305 | "outputs": [],
|
215 | 306 | "source": [
|
|
221 | 312 | "cell_type": "code",
|
222 | 313 | "execution_count": null,
|
223 | 314 | "metadata": {
|
224 |
| - "collapsed": true |
| 315 | + "collapsed": true, |
| 316 | + "jupyter": { |
| 317 | + "outputs_hidden": true |
| 318 | + } |
225 | 319 | },
|
226 | 320 | "outputs": [],
|
227 | 321 | "source": []
|
|
276 | 370 | "cell_type": "code",
|
277 | 371 | "execution_count": null,
|
278 | 372 | "metadata": {
|
279 |
| - "collapsed": true |
| 373 | + "collapsed": true, |
| 374 | + "jupyter": { |
| 375 | + "outputs_hidden": true |
| 376 | + } |
280 | 377 | },
|
281 | 378 | "outputs": [],
|
282 | 379 | "source": [
|
|
395 | 492 | },
|
396 | 493 | {
|
397 | 494 | "cell_type": "markdown",
|
398 |
| - "metadata": { |
399 |
| - "collapsed": true |
400 |
| - }, |
| 495 | + "metadata": {}, |
401 | 496 | "source": [
|
402 | 497 | "*****\n",
|
403 |
| - "# Keypoints:\n", |
404 |
| - "- \"A function may take zero or more arguments.\"\n", |
405 |
| - "- \"Commonly-used built-in functions include `max`, `min`, and `round`.\"\n", |
406 |
| - "- \"Functions may only work for certain (combinations of) arguments.\"\n", |
407 |
| - "- \"Functions may have default values for some arguments.\"\n", |
408 |
| - "- \"Use the built-in function `help` to get help for a function.\"\n", |
409 |
| - "- \"The Jupyter Notebook has two ways to get help.\"\n", |
| 498 | + "# Key Points\n", |
| 499 | + "\n", |
| 500 | + "- A function may take zero or more arguments.\"\n", |
| 501 | + "- Commonly-used built-in functions include `max`, `min`, and `round`.\"\n", |
| 502 | + "- Functions may only work for certain (combinations of) arguments.\"\n", |
| 503 | + "- Functions may have default values for some arguments.\"\n", |
| 504 | + "- Use the built-in function `help` to get help for a function.\"\n", |
| 505 | + "- The Jupyter Notebook has two ways to get help.\"\n", |
410 | 506 | "- \"Every function returns something.\""
|
411 | 507 | ]
|
412 |
| - }, |
413 |
| - { |
414 |
| - "cell_type": "code", |
415 |
| - "execution_count": null, |
416 |
| - "metadata": { |
417 |
| - "collapsed": true |
418 |
| - }, |
419 |
| - "outputs": [], |
420 |
| - "source": [] |
421 | 508 | }
|
422 | 509 | ],
|
423 | 510 | "metadata": {
|
424 | 511 | "kernelspec": {
|
425 |
| - "display_name": "Python 3", |
| 512 | + "display_name": "Python 3 (ipykernel)", |
426 | 513 | "language": "python",
|
427 | 514 | "name": "python3"
|
428 | 515 | },
|
|
436 | 523 | "name": "python",
|
437 | 524 | "nbconvert_exporter": "python",
|
438 | 525 | "pygments_lexer": "ipython3",
|
439 |
| - "version": "3.6.3" |
| 526 | + "version": "3.9.7" |
440 | 527 | }
|
441 | 528 | },
|
442 | 529 | "nbformat": 4,
|
443 |
| - "nbformat_minor": 1 |
| 530 | + "nbformat_minor": 4 |
444 | 531 | }
|
0 commit comments