|
207 | 207 | "source": [
|
208 | 208 | "## Basic Function Syntax\n",
|
209 | 209 | "\n",
|
210 |
| - "Writing a function in Python is pretty easy! You need to know a few things:\n", |
| 210 | + "Writing a function in Python is pretty easy! Let's take a look at a simple function that converts feet into meters:\n", |
211 | 211 | "\n",
|
212 |
| - "* Functions begin with the keyword `def`.\n", |
213 |
| - "* This keyword is followed by the function *name*.\n", |
214 |
| - " * The name must obey the same rules as variable names.\n", |
215 |
| - "* The **arguments** or **parameters** are defined in parentheses as variable names.\n", |
216 |
| - " * Use empty parentheses if the function doesn't take any inputs.\n", |
217 |
| - "* A colon indicates the end of the function *signature* (the first line).\n", |
218 |
| - "* An indented block of code denotes the start of the *body*.\n", |
219 |
| - "* The final line should be a `return` statement with the value(s) to be returned from the function.\n", |
| 212 | + "<img src=\"../images/functions.png\" alt=\"Aspects of a Python Function\" width=\"700\"/>\n", |
220 | 213 | "\n",
|
221 |
| - "Let's take a look at a simple function that converts feet into meters:" |
| 214 | + "Here's the same function written out:" |
222 | 215 | ]
|
223 | 216 | },
|
224 | 217 | {
|
|
313 | 306 | "metadata": {},
|
314 | 307 | "source": [
|
315 | 308 | "Here's what's happening to our DataFrame when we perform that operation:\n",
|
316 |
| - "<img src=\"../images/vectorized.png\" alt=\"Vectorization in Pandas with scalar\" width=\"500\"/>" |
| 309 | + "<img src=\"../images/vectorized.png\" alt=\"Vectorization in Pandas with scalar\" width=\"600\"/>" |
317 | 310 | ]
|
318 | 311 | },
|
319 | 312 | {
|
|
322 | 315 | "source": [
|
323 | 316 | "## How to `apply()` a Function\n",
|
324 | 317 | "\n",
|
325 |
| - "Similarly, the Pandas `.apply()` method allows you to apply a function over an entire column of a dataframe. Here's an example: " |
| 318 | + "We can also use a more general technique: `apply()`. `apply()` takes any function and **applies** it over every entry of the column. Let's see how we can do the same operation as above using `apply()`." |
326 | 319 | ]
|
327 | 320 | },
|
328 | 321 | {
|
|
341 | 334 | "cell_type": "markdown",
|
342 | 335 | "metadata": {},
|
343 | 336 | "source": [
|
344 |
| - "In the code above, we create a function that adds 10 to whatever comes into it.\n", |
| 337 | + "In the code above, we create a function that adds 10 to whatever is passed into it as an argument.\n", |
345 | 338 | "\n",
|
346 | 339 | "🔔 <span style=\"color:purple\"> **Question**: What happens when we `apply()` our function to the `pop` column?</span>"
|
347 | 340 | ]
|
|
0 commit comments