Skip to content

Commit df9944f

Browse files
committed
Merge branch 'modules'
2 parents 078682c + 2d3c54d commit df9944f

File tree

1 file changed

+54
-17
lines changed

1 file changed

+54
-17
lines changed

Intro to Python (Part 3).ipynb

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -334,14 +334,9 @@
334334
"cell_type": "markdown",
335335
"metadata": {},
336336
"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:"
345340
]
346341
},
347342
{
@@ -354,11 +349,18 @@
354349
"metadata": {},
355350
"outputs": []
356351
},
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+
},
357359
{
358360
"cell_type": "code",
359361
"collapsed": false,
360362
"input": [
361-
"random.randint(1,6)\n"
363+
"random.randint(1, 6)\n"
362364
],
363365
"language": "python",
364366
"metadata": {},
@@ -375,8 +377,8 @@
375377
"cell_type": "code",
376378
"collapsed": false,
377379
"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",
380382
"print (\"First die: \" + str(vegas_die_1))\n",
381383
"print (\"Second die: \" + str(vegas_die_2))\n",
382384
"print (\"You rolled a \" + str(vegas_die_1 + vegas_die_2))"
@@ -385,6 +387,13 @@
385387
"metadata": {},
386388
"outputs": []
387389
},
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+
},
388397
{
389398
"cell_type": "code",
390399
"collapsed": false,
@@ -399,32 +408,60 @@
399408
"cell_type": "markdown",
400409
"metadata": {},
401410
"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."
403412
]
404413
},
405414
{
406415
"cell_type": "markdown",
407416
"metadata": {},
408417
"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"
410419
]
411420
},
412421
{
413422
"cell_type": "code",
414423
"collapsed": false,
415424
"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)"
419428
],
420429
"language": "python",
421430
"metadata": {},
422431
"outputs": []
423432
},
433+
{
434+
"cell_type": "markdown",
435+
"metadata": {},
436+
"source": [
437+
"Now let's try it out with a list of names:"
438+
]
439+
},
424440
{
425441
"cell_type": "code",
426442
"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+
],
428465
"language": "python",
429466
"metadata": {},
430467
"outputs": []

0 commit comments

Comments
 (0)