|
10 | 10 | "name": "python2" |
11 | 11 | }, |
12 | 12 | "name": "", |
13 | | - "signature": "sha256:b363fd42a98873952aba75206e163d01b139ef9e32ca1075b0d581f04bbf654a" |
| 13 | + "signature": "sha256:71c98cee8fe96271cc8983aae5b8d91c4d35188a06dd6e0fce00a96d7bd13e92" |
14 | 14 | }, |
15 | 15 | "nbformat": 3, |
16 | 16 | "nbformat_minor": 0, |
|
548 | 548 | "level": 2, |
549 | 549 | "metadata": {}, |
550 | 550 | "source": [ |
551 | | - "Example: Cupcake Calculation Function" |
| 551 | + "Example: Getting Fancy With Math" |
552 | 552 | ] |
553 | 553 | }, |
554 | 554 | { |
555 | 555 | "cell_type": "markdown", |
556 | 556 | "metadata": {}, |
557 | 557 | "source": [ |
558 | | - "Now, let's see what we need to do to make a function return a value to where the function was initially called.\n", |
| 558 | + "What about a function that does some math for us?\n", |
559 | 559 | "\n", |
560 | | - "So let's create a new function, `get_cupcake_count`.\n", |
| 560 | + "Every year on my birthday, I like to figure out what numbers are divisible by my new age. That is, the factors of my age:" |
| 561 | + ] |
| 562 | + }, |
| 563 | + { |
| 564 | + "cell_type": "code", |
| 565 | + "collapsed": false, |
| 566 | + "input": [ |
| 567 | + "def print_factors(age):\n", |
| 568 | + " for i in range(1, age + 1):\n", |
| 569 | + " if age % i == 0:\n", |
| 570 | + " print(i)" |
| 571 | + ], |
| 572 | + "language": "python", |
| 573 | + "metadata": {}, |
| 574 | + "outputs": [], |
| 575 | + "prompt_number": 61 |
| 576 | + }, |
| 577 | + { |
| 578 | + "cell_type": "markdown", |
| 579 | + "metadata": {}, |
| 580 | + "source": [ |
| 581 | + "This year, I turned 32." |
| 582 | + ] |
| 583 | + }, |
| 584 | + { |
| 585 | + "cell_type": "code", |
| 586 | + "collapsed": false, |
| 587 | + "input": [ |
| 588 | + "print_factors(32)" |
| 589 | + ], |
| 590 | + "language": "python", |
| 591 | + "metadata": {}, |
| 592 | + "outputs": [ |
| 593 | + { |
| 594 | + "output_type": "stream", |
| 595 | + "stream": "stdout", |
| 596 | + "text": [ |
| 597 | + "1\n", |
| 598 | + "2\n", |
| 599 | + "4\n", |
| 600 | + "8\n", |
| 601 | + "16\n", |
| 602 | + "32\n" |
| 603 | + ] |
| 604 | + } |
| 605 | + ], |
| 606 | + "prompt_number": 62 |
| 607 | + }, |
| 608 | + { |
| 609 | + "cell_type": "markdown", |
| 610 | + "metadata": {}, |
| 611 | + "source": [ |
| 612 | + "From the output of `print_factors()`, I now know that I'm:\n", |
| 613 | + "* Twice as old as a 16-year-old\n", |
| 614 | + "* Four times as old as an 8-year-old\n", |
| 615 | + "* Eight times as old as a 4-year-old\n", |
| 616 | + "* Sixteen times as old as a 2-year-old\n", |
561 | 617 | "\n", |
562 | | - "We'll multiply the number of guests by two and return the result because each guest wants to eat two cupcakes." |
| 618 | + "How is this useful? Well, the next time a 4-year-old bullies me, I'm going to say, \"Watch it, I'm exactly eight times as old as you.\"\n", |
| 619 | + "\n", |
| 620 | + "Next year, I'll be able to brag to 3-year-olds: \"Ha ha ha, I'm exactly eleven times as old as you.\"" |
563 | 621 | ] |
564 | 622 | }, |
565 | 623 | { |
566 | 624 | "cell_type": "code", |
567 | 625 | "collapsed": false, |
568 | 626 | "input": [ |
569 | | - "def get_cupcake_count(guests):\n", |
570 | | - " return 2 * guests" |
| 627 | + "print_factors(33)" |
571 | 628 | ], |
572 | 629 | "language": "python", |
573 | 630 | "metadata": {}, |
574 | | - "outputs": [] |
| 631 | + "outputs": [ |
| 632 | + { |
| 633 | + "output_type": "stream", |
| 634 | + "stream": "stdout", |
| 635 | + "text": [ |
| 636 | + "1\n", |
| 637 | + "3\n", |
| 638 | + "11\n", |
| 639 | + "33\n" |
| 640 | + ] |
| 641 | + } |
| 642 | + ], |
| 643 | + "prompt_number": 63 |
| 644 | + }, |
| 645 | + { |
| 646 | + "cell_type": "heading", |
| 647 | + "level": 2, |
| 648 | + "metadata": {}, |
| 649 | + "source": [ |
| 650 | + "Example: Cupcake Count" |
| 651 | + ] |
| 652 | + }, |
| 653 | + { |
| 654 | + "cell_type": "markdown", |
| 655 | + "metadata": {}, |
| 656 | + "source": [ |
| 657 | + "Okay, we've explored functions that:\n", |
| 658 | + "* Take strings or other values as input\n", |
| 659 | + "* Print strings\n", |
| 660 | + "\n", |
| 661 | + "Now, let's create a function that:\n", |
| 662 | + "* Takes a number as input\n", |
| 663 | + "* Returns a number" |
| 664 | + ] |
| 665 | + }, |
| 666 | + { |
| 667 | + "cell_type": "markdown", |
| 668 | + "metadata": {}, |
| 669 | + "source": [ |
| 670 | + "So let's create a new function, `cupcake_count`. It will return a value to where the function was initially called.\n", |
| 671 | + "\n", |
| 672 | + "We'll multiply the number of guests by two and return the result because each guest wants to eat two cupcakes." |
| 673 | + ] |
575 | 674 | }, |
576 | 675 | { |
577 | 676 | "cell_type": "code", |
578 | 677 | "collapsed": false, |
579 | 678 | "input": [ |
580 | | - "print(\"We need to make \" + str(get_cupcake_count(10)) + \" cupcakes.\")" |
| 679 | + "def cupcake_count(guests):\n", |
| 680 | + " \"\"\" Given number of party guests, returns how many cupcakes are needed. \"\"\"\n", |
| 681 | + " return 2 * guests" |
581 | 682 | ], |
582 | 683 | "language": "python", |
583 | 684 | "metadata": {}, |
584 | | - "outputs": [] |
| 685 | + "outputs": [], |
| 686 | + "prompt_number": 58 |
| 687 | + }, |
| 688 | + { |
| 689 | + "cell_type": "heading", |
| 690 | + "level": 1, |
| 691 | + "metadata": {}, |
| 692 | + "source": [ |
| 693 | + "print(\"We need to make \" + str(cupcake_count(10)) + \" cupcakes.\")" |
| 694 | + ] |
585 | 695 | }, |
586 | 696 | { |
587 | 697 | "cell_type": "markdown", |
|
598 | 708 | "cell_type": "markdown", |
599 | 709 | "metadata": {}, |
600 | 710 | "source": [ |
601 | | - "Note if you are wondering what the str() function is... Python does not allow us to concatenate strings with integers and will tell you so. We need to cast it with the string function to convert the integer into a string object" |
| 711 | + "Note if you are wondering what the str() function is... Python does not allow us to concatenate strings with integers and will tell you so. \n", |
| 712 | + "\n", |
| 713 | + "We need to convert the integer with the string function to convert the integer into a string object" |
602 | 714 | ] |
603 | 715 | }, |
604 | 716 | { |
|
720 | 832 | "cell_type": "code", |
721 | 833 | "collapsed": false, |
722 | 834 | "input": [ |
723 | | - "print(\"\\nOn the day of Sept 20, 2014, were you at Intro to Python?\")\n", |
| 835 | + "print(\"On the day of Sept 20, 2014, were you at Intro to Python?\")\n", |
724 | 836 | "answer = input(\"Answer truthfully, yes or no --> \")\n", |
725 | 837 | "if answer == \"no\":\n", |
726 | 838 | " answer = input(\"Are you sure that you weren't? Tell the truth, now --> \")\n", |
727 | 839 | "\n", |
728 | | - "print(\"\\nWere you thinking of skipping the workshop to go to Sea World?\")\n", |
| 840 | + "print(\"Were you thinking of skipping the workshop to go to Sea World?\")\n", |
729 | 841 | "answer = input(\"Answer truthfully, yes or no --> \")\n", |
730 | 842 | "if answer == \"no\":\n", |
731 | 843 | " answer = input(\"Are you sure that you weren't? Tell the truth, now --> \")" |
|
0 commit comments