|
10 | 10 | "name": "python2" |
11 | 11 | }, |
12 | 12 | "name": "", |
13 | | - "signature": "sha256:98933314083fad8f511ddedbb2e2864dc55638392d8ce23f6cb46b78f049a2a3" |
| 13 | + "signature": "sha256:21fa40ef7368043bdaf558035d71a9c7d07f9c147ef8083eadd7fe8a891b7ca2" |
14 | 14 | }, |
15 | 15 | "nbformat": 3, |
16 | 16 | "nbformat_minor": 0, |
|
67 | 67 | "cell_type": "markdown", |
68 | 68 | "metadata": {}, |
69 | 69 | "source": [ |
70 | | - "Say we have a simple program that prints a happy birthday greeting:" |
| 70 | + "Say we have a simple program that prints a happy birthday greeting for Carol.\n", |
| 71 | + "\n", |
| 72 | + "It's not Carol's birthday, but let's run this program anyway, just to confuse her in good fun ;)" |
71 | 73 | ] |
72 | 74 | }, |
73 | 75 | { |
|
78 | 80 | ], |
79 | 81 | "language": "python", |
80 | 82 | "metadata": {}, |
81 | | - "outputs": [] |
| 83 | + "outputs": [ |
| 84 | + { |
| 85 | + "output_type": "stream", |
| 86 | + "stream": "stdout", |
| 87 | + "text": [ |
| 88 | + "Happy Birthday, dear Carol!\n" |
| 89 | + ] |
| 90 | + } |
| 91 | + ], |
| 92 | + "prompt_number": 1 |
82 | 93 | }, |
83 | 94 | { |
84 | 95 | "cell_type": "markdown", |
|
96 | 107 | ], |
97 | 108 | "language": "python", |
98 | 109 | "metadata": {}, |
99 | | - "outputs": [] |
| 110 | + "outputs": [], |
| 111 | + "prompt_number": 3 |
100 | 112 | }, |
101 | 113 | { |
102 | 114 | "cell_type": "heading", |
|
150 | 162 | "cell_type": "markdown", |
151 | 163 | "metadata": {}, |
152 | 164 | "source": [ |
153 | | - "Don't forget those parenthesis. Without the parenthesis Python wouldn't know we were trying to call our function, and will print out a statement " |
| 165 | + "Don't forget those parentheses. \n", |
| 166 | + "\n", |
| 167 | + "Without the parentheses, Python wouldn't know that we were trying to call our function, and will print out the value of the thing called `happy_birthday_carol`." |
154 | 168 | ] |
155 | 169 | }, |
156 | 170 | { |
|
161 | 175 | ], |
162 | 176 | "language": "python", |
163 | 177 | "metadata": {}, |
164 | | - "outputs": [] |
| 178 | + "outputs": [ |
| 179 | + { |
| 180 | + "metadata": {}, |
| 181 | + "output_type": "pyout", |
| 182 | + "prompt_number": 4, |
| 183 | + "text": [ |
| 184 | + "<function __main__.happy_birthday_carol>" |
| 185 | + ] |
| 186 | + } |
| 187 | + ], |
| 188 | + "prompt_number": 4 |
| 189 | + }, |
| 190 | + { |
| 191 | + "cell_type": "markdown", |
| 192 | + "metadata": {}, |
| 193 | + "source": [ |
| 194 | + "Here, the Python interpreter is basically saying, \"The value of happy_birthday_carol is a function.\"" |
| 195 | + ] |
| 196 | + }, |
| 197 | + { |
| 198 | + "cell_type": "heading", |
| 199 | + "level": 3, |
| 200 | + "metadata": {}, |
| 201 | + "source": [ |
| 202 | + "Let's define and call another function" |
| 203 | + ] |
165 | 204 | }, |
166 | 205 | { |
167 | 206 | "cell_type": "markdown", |
168 | 207 | "metadata": {}, |
169 | 208 | "source": [ |
170 | | - "We can create a slew of functions named in a similar fashion:" |
| 209 | + "That function printed a nice greeting for Carol. But Rise's now feeling left out. Let's define a function for her too." |
171 | 210 | ] |
172 | 211 | }, |
173 | 212 | { |
|
179 | 218 | ], |
180 | 219 | "language": "python", |
181 | 220 | "metadata": {}, |
182 | | - "outputs": [] |
| 221 | + "outputs": [], |
| 222 | + "prompt_number": 5 |
| 223 | + }, |
| 224 | + { |
| 225 | + "cell_type": "markdown", |
| 226 | + "metadata": {}, |
| 227 | + "source": [ |
| 228 | + "Now, let's call the function:" |
| 229 | + ] |
183 | 230 | }, |
184 | 231 | { |
185 | 232 | "cell_type": "code", |
|
189 | 236 | ], |
190 | 237 | "language": "python", |
191 | 238 | "metadata": {}, |
192 | | - "outputs": [] |
| 239 | + "outputs": [ |
| 240 | + { |
| 241 | + "output_type": "stream", |
| 242 | + "stream": "stdout", |
| 243 | + "text": [ |
| 244 | + "Happy Birthday, dear Rise!\n" |
| 245 | + ] |
| 246 | + } |
| 247 | + ], |
| 248 | + "prompt_number": 6 |
| 249 | + }, |
| 250 | + { |
| 251 | + "cell_type": "heading", |
| 252 | + "level": 3, |
| 253 | + "metadata": {}, |
| 254 | + "source": [ |
| 255 | + "Refactor time!" |
| 256 | + ] |
193 | 257 | }, |
194 | 258 | { |
195 | 259 | "cell_type": "markdown", |
|
201 | 265 | "\n", |
202 | 266 | "Python allows us to do just that with function parameters. We can add a parameter by typing a variable name in between the parethesis after our function name. We can then use the parameter variable in our function.\n", |
203 | 267 | "\n", |
204 | | - "We'll create a new function called 'happy_birthday', and add a parameter, 'name'." |
| 268 | + "We'll create a new function called `happy_birthday`, and add a parameter, `name`." |
205 | 269 | ] |
206 | 270 | }, |
207 | 271 | { |
|
213 | 277 | ], |
214 | 278 | "language": "python", |
215 | 279 | "metadata": {}, |
216 | | - "outputs": [] |
| 280 | + "outputs": [], |
| 281 | + "prompt_number": 18 |
| 282 | + }, |
| 283 | + { |
| 284 | + "cell_type": "markdown", |
| 285 | + "metadata": {}, |
| 286 | + "source": [ |
| 287 | + "Here, we are concatenating 3 strings:\n", |
| 288 | + "* \"Happy Birthday, dear \"\n", |
| 289 | + "* whatever the value of `name` is (Note: for the purposes of this example, let's assume that we're always getting a string)\n", |
| 290 | + "* \"!\"" |
| 291 | + ] |
217 | 292 | }, |
218 | 293 | { |
219 | 294 | "cell_type": "markdown", |
220 | 295 | "metadata": {}, |
221 | 296 | "source": [ |
222 | | - "To execute or run our new function, we simply call it by its name, 'happy_birthday' and pass a value in between the parenthesis, such as the string, 'Trey'." |
| 297 | + "To execute or run our new function, we simply call it by its name, `happy_birthday`, and pass a value in between the parenthesis, such as the string `Trey`." |
223 | 298 | ] |
224 | 299 | }, |
225 | 300 | { |
|
230 | 305 | ], |
231 | 306 | "language": "python", |
232 | 307 | "metadata": {}, |
233 | | - "outputs": [] |
| 308 | + "outputs": [ |
| 309 | + { |
| 310 | + "output_type": "stream", |
| 311 | + "stream": "stdout", |
| 312 | + "text": [ |
| 313 | + "Happy Birthday, dear Trey!\n" |
| 314 | + ] |
| 315 | + } |
| 316 | + ], |
| 317 | + "prompt_number": 19 |
| 318 | + }, |
| 319 | + { |
| 320 | + "cell_type": "heading", |
| 321 | + "level": 3, |
| 322 | + "metadata": {}, |
| 323 | + "source": [ |
| 324 | + "Calling it over and over in a loop" |
| 325 | + ] |
234 | 326 | }, |
235 | 327 | { |
236 | 328 | "cell_type": "markdown", |
237 | 329 | "metadata": {}, |
238 | 330 | "source": [ |
239 | | - "Let's get a little bolder and make a list of names that we can loop over and then pass each one into our 'happy_birthday' function. What we get is the same results as the other earlier lines of code!" |
| 331 | + "Let's get a little bolder and make a list of names that we can loop over and then pass each one into our `happy_birthday` function. What we get is the same results as the other earlier lines of code!" |
240 | 332 | ] |
241 | 333 | }, |
242 | 334 | { |
|
247 | 339 | ], |
248 | 340 | "language": "python", |
249 | 341 | "metadata": {}, |
250 | | - "outputs": [] |
| 342 | + "outputs": [], |
| 343 | + "prompt_number": 23 |
251 | 344 | }, |
252 | 345 | { |
253 | 346 | "cell_type": "code", |
|
258 | 351 | ], |
259 | 352 | "language": "python", |
260 | 353 | "metadata": {}, |
261 | | - "outputs": [] |
| 354 | + "outputs": [ |
| 355 | + { |
| 356 | + "output_type": "stream", |
| 357 | + "stream": "stdout", |
| 358 | + "text": [ |
| 359 | + "Happy Birthday, dear Carol!\n", |
| 360 | + "Happy Birthday, dear Rise!\n", |
| 361 | + "Happy Birthday, dear Trey!\n", |
| 362 | + "Happy Birthday, dear Alain!\n" |
| 363 | + ] |
| 364 | + } |
| 365 | + ], |
| 366 | + "prompt_number": 24 |
| 367 | + }, |
| 368 | + { |
| 369 | + "cell_type": "heading", |
| 370 | + "level": 2, |
| 371 | + "metadata": {}, |
| 372 | + "source": [ |
| 373 | + "Example: Cupcake Calculation Function" |
| 374 | + ] |
262 | 375 | }, |
263 | 376 | { |
264 | 377 | "cell_type": "markdown", |
265 | 378 | "metadata": {}, |
266 | 379 | "source": [ |
267 | 380 | "Now, let's see what we need to do to make a function return a value to where the function was initially called.\n", |
268 | 381 | "\n", |
269 | | - "So let's create a new function, 'get_cupcake_count'.\n", |
| 382 | + "So let's create a new function, `get_cupcake_count`.\n", |
270 | 383 | "\n", |
271 | | - "We'll multiple the number of guests by two and return the result because each guest wants to eat two cupcakes." |
| 384 | + "We'll multiply the number of guests by two and return the result because each guest wants to eat two cupcakes." |
272 | 385 | ] |
273 | 386 | }, |
274 | 387 | { |
|
0 commit comments