|
334 | 334 | "cell_type": "markdown", |
335 | 335 | "metadata": {}, |
336 | 336 | "source": [ |
337 | | - "Modules are used to make a programmer's life easier. A module is used to group functions, variables, and other things together in an understandable bundle." |
338 | | - ] |
339 | | - }, |
340 | | - { |
341 | | - "cell_type": "markdown", |
342 | | - "metadata": {}, |
343 | | - "source": [ |
344 | | - "Modules can be used to do useful things - games, photo manipulation, graphics, financial calculations, and much more" |
| 337 | + "Modules are used to make a programmer's life easier. A module is used to group functions, variables, and other things together in an understandable bundle.\n", |
| 338 | + "\n", |
| 339 | + "Python comes with lots of modules built-in. For example there's a random module for generating randomnes. Let's import it so we can use some of its functions:" |
345 | 340 | ] |
346 | 341 | }, |
347 | 342 | { |
|
354 | 349 | "metadata": {}, |
355 | 350 | "outputs": [] |
356 | 351 | }, |
| 352 | + { |
| 353 | + "cell_type": "markdown", |
| 354 | + "metadata": {}, |
| 355 | + "source": [ |
| 356 | + "The random module has a randint function which takes two parameters. randint will return a random integer in between (or including) the two parameters. For example we can generate a random number between 1 and 6." |
| 357 | + ] |
| 358 | + }, |
357 | 359 | { |
358 | 360 | "cell_type": "code", |
359 | 361 | "collapsed": false, |
360 | 362 | "input": [ |
361 | | - "random.randint(1,6)\n" |
| 363 | + "random.randint(1, 6)\n" |
362 | 364 | ], |
363 | 365 | "language": "python", |
364 | 366 | "metadata": {}, |
|
375 | 377 | "cell_type": "code", |
376 | 378 | "collapsed": false, |
377 | 379 | "input": [ |
378 | | - "vegas_die_1 = random.randint(1,6)\n", |
379 | | - "vegas_die_2 = random.randint(1,6)\n", |
| 380 | + "vegas_die_1 = random.randint(1, 6)\n", |
| 381 | + "vegas_die_2 = random.randint(1, 6)\n", |
380 | 382 | "print (\"First die: \" + str(vegas_die_1))\n", |
381 | 383 | "print (\"Second die: \" + str(vegas_die_2))\n", |
382 | 384 | "print (\"You rolled a \" + str(vegas_die_1 + vegas_die_2))" |
|
385 | 387 | "metadata": {}, |
386 | 388 | "outputs": [] |
387 | 389 | }, |
| 390 | + { |
| 391 | + "cell_type": "markdown", |
| 392 | + "metadata": {}, |
| 393 | + "source": [ |
| 394 | + "choice is a function in the random module that returns a random item from a given list or string" |
| 395 | + ] |
| 396 | + }, |
388 | 397 | { |
389 | 398 | "cell_type": "code", |
390 | 399 | "collapsed": false, |
|
399 | 408 | "cell_type": "markdown", |
400 | 409 | "metadata": {}, |
401 | 410 | "source": [ |
402 | | - "choice is a function in the random module that returns an item" |
| 411 | + "We could use random.choice to wish happy birthday to a random friend." |
403 | 412 | ] |
404 | 413 | }, |
405 | 414 | { |
406 | 415 | "cell_type": "markdown", |
407 | 416 | "metadata": {}, |
408 | 417 | "source": [ |
409 | | - "Modules and functions can make things really simple to do. For example we could in a few lines of code get and display a YouTube video" |
| 418 | + "Let's make a function random_happy_birthday that takes a list of names and wishes a random person happy birthday" |
410 | 419 | ] |
411 | 420 | }, |
412 | 421 | { |
413 | 422 | "cell_type": "code", |
414 | 423 | "collapsed": false, |
415 | 424 | "input": [ |
416 | | - "from IPython.display import YouTubeVideo\n", |
417 | | - "# a tutorial about Python at PyCon 2014 in Montreal, Canada by Jessica McKellar\n", |
418 | | - "YouTubeVideo('MirG-vJOg04')" |
| 425 | + "def random_happy_birthday(names):\n", |
| 426 | + " name = random.choice(names)\n", |
| 427 | + " happy_birthday(name)" |
419 | 428 | ], |
420 | 429 | "language": "python", |
421 | 430 | "metadata": {}, |
422 | 431 | "outputs": [] |
423 | 432 | }, |
| 433 | + { |
| 434 | + "cell_type": "markdown", |
| 435 | + "metadata": {}, |
| 436 | + "source": [ |
| 437 | + "Now let's try it out with a list of names:" |
| 438 | + ] |
| 439 | + }, |
424 | 440 | { |
425 | 441 | "cell_type": "code", |
426 | 442 | "collapsed": false, |
427 | | - "input": [], |
| 443 | + "input": [ |
| 444 | + "random_happy_birthday([\"Alain\", \"Carol\", \"Rise\", \"Trey\"])" |
| 445 | + ], |
| 446 | + "language": "python", |
| 447 | + "metadata": {}, |
| 448 | + "outputs": [] |
| 449 | + }, |
| 450 | + { |
| 451 | + "cell_type": "markdown", |
| 452 | + "metadata": {}, |
| 453 | + "source": [ |
| 454 | + "Modules and functions can make things really simple to do. For example we could in a few lines of code get and display a YouTube video" |
| 455 | + ] |
| 456 | + }, |
| 457 | + { |
| 458 | + "cell_type": "code", |
| 459 | + "collapsed": false, |
| 460 | + "input": [ |
| 461 | + "from IPython.display import YouTubeVideo\n", |
| 462 | + "# a tutorial about Python at PyCon 2014 in Montreal, Canada by Jessica McKellar\n", |
| 463 | + "YouTubeVideo('MirG-vJOg04')" |
| 464 | + ], |
428 | 465 | "language": "python", |
429 | 466 | "metadata": {}, |
430 | 467 | "outputs": [] |
|
0 commit comments