|
10 | 10 | "name": "python2" |
11 | 11 | }, |
12 | 12 | "name": "", |
13 | | - "signature": "sha256:71c98cee8fe96271cc8983aae5b8d91c4d35188a06dd6e0fce00a96d7bd13e92" |
| 13 | + "signature": "sha256:ee94c8f65b24aca6551d49f1d80ebf2b4fae52fbbda61e090166f59ccddb81b8" |
14 | 14 | }, |
15 | 15 | "nbformat": 3, |
16 | 16 | "nbformat_minor": 0, |
|
647 | 647 | "level": 2, |
648 | 648 | "metadata": {}, |
649 | 649 | "source": [ |
650 | | - "Example: Cupcake Count" |
| 650 | + "Example: Cupcake Tally" |
651 | 651 | ] |
652 | 652 | }, |
653 | 653 | { |
654 | 654 | "cell_type": "markdown", |
655 | 655 | "metadata": {}, |
656 | 656 | "source": [ |
657 | | - "Okay, we've explored functions that:\n", |
| 657 | + "So far we've explored functions that:\n", |
658 | 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" |
| 659 | + "* Print strings" |
664 | 660 | ] |
665 | 661 | }, |
666 | 662 | { |
667 | 663 | "cell_type": "markdown", |
668 | 664 | "metadata": {}, |
669 | 665 | "source": [ |
670 | | - "So let's create a new function, `cupcake_count`. It will return a value to where the function was initially called.\n", |
| 666 | + "Let's create a new function, `cupcake_tally()`. This function will be different from the others that I covered. Rather than just printing stuff, it'll return a value when called.\n", |
671 | 667 | "\n", |
672 | | - "We'll multiply the number of guests by two and return the result because each guest wants to eat two cupcakes." |
| 668 | + "A good birthday party should allocate:\n", |
| 669 | + "* 2 cupcakes per guest\n", |
| 670 | + "* 13 cupcakes for the birthday person, because if it's your birthday, you should be allowed to eat a baker's dozen of cupcakes." |
673 | 671 | ] |
674 | 672 | }, |
675 | 673 | { |
676 | 674 | "cell_type": "code", |
677 | 675 | "collapsed": false, |
678 | 676 | "input": [ |
679 | | - "def cupcake_count(guests):\n", |
| 677 | + "def cupcake_tally(guests):\n", |
680 | 678 | " \"\"\" Given number of party guests, returns how many cupcakes are needed. \"\"\"\n", |
681 | | - " return 2 * guests" |
| 679 | + " return 2 * guests + 13" |
682 | 680 | ], |
683 | 681 | "language": "python", |
684 | 682 | "metadata": {}, |
685 | 683 | "outputs": [], |
686 | | - "prompt_number": 58 |
| 684 | + "prompt_number": 64 |
687 | 685 | }, |
688 | 686 | { |
689 | | - "cell_type": "heading", |
690 | | - "level": 1, |
| 687 | + "cell_type": "markdown", |
691 | 688 | "metadata": {}, |
692 | 689 | "source": [ |
693 | | - "print(\"We need to make \" + str(cupcake_count(10)) + \" cupcakes.\")" |
| 690 | + "How many cupcakes do we need for a party with 30 guests?" |
694 | 691 | ] |
695 | 692 | }, |
| 693 | + { |
| 694 | + "cell_type": "code", |
| 695 | + "collapsed": false, |
| 696 | + "input": [ |
| 697 | + "cupcake_tally(30)" |
| 698 | + ], |
| 699 | + "language": "python", |
| 700 | + "metadata": {}, |
| 701 | + "outputs": [ |
| 702 | + { |
| 703 | + "metadata": {}, |
| 704 | + "output_type": "pyout", |
| 705 | + "prompt_number": 65, |
| 706 | + "text": [ |
| 707 | + "73" |
| 708 | + ] |
| 709 | + } |
| 710 | + ], |
| 711 | + "prompt_number": 65 |
| 712 | + }, |
696 | 713 | { |
697 | 714 | "cell_type": "markdown", |
698 | 715 | "metadata": {}, |
699 | 716 | "source": [ |
700 | | - "Ok, so we call our function, get_cupcake_count, and pass in the integer 10 as the value of the parameter, 'guests'. \n", |
701 | | - "\n", |
702 | | - "Then we multiply the number of guests by 2 and return the result back to where the function was initially called. \n", |
703 | | - "\n", |
704 | | - "Lastly, it prints out the returned integer value of 20." |
| 717 | + "What about if I'm celebrating my birthday at home with just my husband, Daniel?" |
705 | 718 | ] |
706 | 719 | }, |
| 720 | + { |
| 721 | + "cell_type": "code", |
| 722 | + "collapsed": false, |
| 723 | + "input": [ |
| 724 | + "cupcake_tally(guests=1)" |
| 725 | + ], |
| 726 | + "language": "python", |
| 727 | + "metadata": {}, |
| 728 | + "outputs": [ |
| 729 | + { |
| 730 | + "metadata": {}, |
| 731 | + "output_type": "pyout", |
| 732 | + "prompt_number": 69, |
| 733 | + "text": [ |
| 734 | + "15" |
| 735 | + ] |
| 736 | + } |
| 737 | + ], |
| 738 | + "prompt_number": 69 |
| 739 | + }, |
707 | 740 | { |
708 | 741 | "cell_type": "markdown", |
709 | 742 | "metadata": {}, |
710 | 743 | "source": [ |
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", |
| 744 | + "Ooh, 15 cupcakes sounds about right for our little party, yeah? ;)\n", |
712 | 745 | "\n", |
713 | | - "We need to convert the integer with the string function to convert the integer into a string object" |
| 746 | + "Notice how we explicitly named the `guests` parameter in that example." |
| 747 | + ] |
| 748 | + }, |
| 749 | + { |
| 750 | + "cell_type": "heading", |
| 751 | + "level": 3, |
| 752 | + "metadata": {}, |
| 753 | + "source": [ |
| 754 | + "Printing the return value" |
714 | 755 | ] |
715 | 756 | }, |
716 | 757 | { |
717 | 758 | "cell_type": "code", |
718 | 759 | "collapsed": false, |
719 | 760 | "input": [ |
720 | | - "print(\"We need to make \" + str(get_cupcake_count(10)) + \" cupcakes.\")" |
| 761 | + "cupcakes = cupcake_tally(10)\n", |
| 762 | + "print(\"We need to make {0} cupcakes.\".format(cupcakes))" |
721 | 763 | ], |
722 | 764 | "language": "python", |
723 | 765 | "metadata": {}, |
724 | | - "outputs": [] |
| 766 | + "outputs": [ |
| 767 | + { |
| 768 | + "output_type": "stream", |
| 769 | + "stream": "stdout", |
| 770 | + "text": [ |
| 771 | + "We need to make 33 cupcakes.\n" |
| 772 | + ] |
| 773 | + } |
| 774 | + ], |
| 775 | + "prompt_number": 70 |
| 776 | + }, |
| 777 | + { |
| 778 | + "cell_type": "markdown", |
| 779 | + "metadata": {}, |
| 780 | + "source": [ |
| 781 | + "We call our function, `cupcake_tally()`, and pass in 10 as the value of `guests`. \n", |
| 782 | + "\n", |
| 783 | + "`cupcake_tally(10)` returns 33, which gets stored in the `cupcakes` variable.\n", |
| 784 | + "\n", |
| 785 | + "Lastly, we print \"We need to make 33 cupcakes.\" using Python string formatting." |
| 786 | + ] |
| 787 | + }, |
| 788 | + { |
| 789 | + "cell_type": "heading", |
| 790 | + "level": 3, |
| 791 | + "metadata": {}, |
| 792 | + "source": [ |
| 793 | + "Multiple function arguments" |
| 794 | + ] |
725 | 795 | }, |
726 | 796 | { |
727 | 797 | "cell_type": "markdown", |
|
736 | 806 | "cell_type": "code", |
737 | 807 | "collapsed": false, |
738 | 808 | "input": [ |
739 | | - "def get_cupcake_count(cupcakes, guests): \n", |
740 | | - " return cupcakes * guests" |
| 809 | + "def cupcake_tally(cupcakes, guests): \n", |
| 810 | + " return cupcakes * guests + 13" |
741 | 811 | ], |
742 | 812 | "language": "python", |
743 | 813 | "metadata": {}, |
744 | | - "outputs": [] |
| 814 | + "outputs": [], |
| 815 | + "prompt_number": 71 |
745 | 816 | }, |
746 | 817 | { |
747 | 818 | "cell_type": "code", |
748 | 819 | "collapsed": false, |
749 | 820 | "input": [ |
750 | | - "print(get_cupcake_count(4, 15))" |
| 821 | + "cupcake_tally(4, 15)" |
751 | 822 | ], |
752 | 823 | "language": "python", |
753 | 824 | "metadata": {}, |
754 | | - "outputs": [] |
| 825 | + "outputs": [ |
| 826 | + { |
| 827 | + "metadata": {}, |
| 828 | + "output_type": "pyout", |
| 829 | + "prompt_number": 73, |
| 830 | + "text": [ |
| 831 | + "73" |
| 832 | + ] |
| 833 | + } |
| 834 | + ], |
| 835 | + "prompt_number": 73 |
755 | 836 | }, |
756 | 837 | { |
757 | 838 | "cell_type": "markdown", |
758 | 839 | "metadata": {}, |
759 | 840 | "source": [ |
760 | | - "Note that we have a 'cupcakes' parameter as well as a 'guests' parameter. Our function is called and passes in two integer values of 4 and 15. It then prints out the returned result, 60." |
| 841 | + "Note that we have 2 parameters now: `cupcakes` and `guests`\n", |
| 842 | + "\n", |
| 843 | + "Our function is called and passes in two values of 4 and 15. It returns 73." |
761 | 844 | ] |
762 | 845 | }, |
763 | 846 | { |
|
0 commit comments