{ "metadata": { "name": "", "signature": "sha256:ad77b227c6bb954c9e376d7bc5f489061650e722cbbb278269a43cc8c246720d" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Function Basics\n", "\n", "- **Author:** [Chris Albon](http://www.chrisalbon.com/), [@ChrisAlbon](https://twitter.com/chrisalbon)\n", "- **Date:** -\n", "- **Repo:** [Python 3 code snippets for data science](https://github.com/chrisalbon/code_py)\n", "- **Note:**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Based on Byte of Python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a function called printMax with the paramaters x and y." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def printMax(x, y):\n", " # if a is larger than b\n", " if x > y:\n", " # then print this\n", " print(x, 'is maximum')\n", " # if a is equal to b\n", " elif x == y:\n", " # print this\n", " print(x, 'is equal to', y)\n", " # otherwise\n", " else:\n", " # print this\n", " print(y, 'is maximum')" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "4 is maximum\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Run the function with two arguments" ] }, { "cell_type": "code", "collapsed": false, "input": [ "printMax(3,4)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "4 is maximum\n" ] } ], "prompt_number": 37 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note: By default, variables created within functions are local to the function. But you can create a global function that IS defined outside the function." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a variable called x" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x = 50" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a function called func()" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def func():\n", " # Create a global variable called x\n", " global x\n", "\n", " # Print this\n", " print('x is', x)\n", " # Set x to 2.\n", " x = 2\n", " # Print this\n", " print('Changed global x to', x)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 29 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Run the func() function" ] }, { "cell_type": "code", "collapsed": false, "input": [ "func()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "x is 2\n", "Changed global x to 2\n" ] } ], "prompt_number": 34 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Print x" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 35, "text": [ "2" ] } ], "prompt_number": 35 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Default Argument Values" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a function called say() that displays x with the default value of 1" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def say(x, times = 1, times2 = 3):\n", " print(x * times, x * times2)\n", "\n", "# Run the function say() with the default values\n", "say('!')\n", "\n", "# Run the function say() with the non-default values of 5 and 10\n", "say('!', 5, 10)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "! !!!\n", "!!!!! !!!!!!!!!!\n" ] } ], "prompt_number": 19 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## VarArgs Parameters (i.e. unlimited number of parameters)\n", "- \\* denotes that all positonal arguments from that point to next arg are used\n", "- \\** dnotes that all keyword arguments from that point to the next arg are used" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a function called total() with three parameters" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def total(initial=5, *numbers, **keywords):\n", " # Create a variable called count that takes it's value from initial\n", " count = initial\n", " # for each item in numbers\n", " for number in numbers:\n", " # add count to that number\n", " count += number\n", " # for each item in keywords\n", " for key in keywords:\n", " # add count to keyword's value\n", " count += keywords[key]\n", " # return counts\n", " return count" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 25 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Run total()\n", "- 10 is for initial.\n", "- 1,2,3 are for *numbers.\n", "- vegetables and fruit is for **keywords." ] }, { "cell_type": "code", "collapsed": false, "input": [ "total(10, 1, 2, 3, vegetables=50, fruits=100)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 28, "text": [ "166" ] } ], "prompt_number": 28 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## DocStrings (outputs documentation about a function)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a function called printMax with the paramaters x and y" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def printMax(x, y):\n", " # Create the docstring\n", " '''Prints out the maximum of two values'''\n", " # if a is larger than b\n", " if x > y:\n", " # then print this\n", " print(x, 'is maximum')\n", " # if a is equal to b\n", " elif x == y:\n", " # print this\n", " print(x, 'is equal to', y)\n", " # otherwise\n", " else:\n", " # print this\n", " print(y, 'is maximum')" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 21 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Run the function with two arguments" ] }, { "cell_type": "code", "collapsed": false, "input": [ "printMax(3,4)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "4 is maximum\n" ] } ], "prompt_number": 22 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### View the docstring" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print(printMax.__doc__)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Prints out the maximum of two values\n" ] } ], "prompt_number": 23 } ], "metadata": {} } ] }