{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python Introduction: Functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Functions help extract out common code blocks.\n", "\n", "Let's define a function `print_greeting()`." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def print_greeting():\n", " print(\"Hi there, how are you?\")\n", " print(\"Long time no see.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And call it:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hi there, how are you?\n", "Long time no see.\n" ] } ], "source": [ "print_greeting()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "That's a bit impersonal." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def print_greeting(name):\n", "\n", " print(\"Hi there, {0}, how are you?\".format(name))\n", "\n", " print(\"Long time no see.\")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hi there, Andreas, how are you?\n", "Long time no see.\n" ] } ], "source": [ "print_greeting(\"Andreas\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "But we might not know their name.\n", "\n", "(And we just changed the interface of `print_greeting`!)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def print_greeting(name=\"my friend\"):\n", "\n", " print(\"Hi there, {0}, how are you?\".format(name))\n", "\n", " print(\"Long time no see.\")" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hi there, Andreas, how are you?\n", "Long time no see.\n", "Hi there, my friend, how are you?\n", "Long time no see.\n" ] } ], "source": [ "print_greeting(\"Andreas\")\n", "print_greeting()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Function parameters work like variables.\n", "\n", "So what does this do?" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3, 5]\n" ] } ], "source": [ "def my_func(my_list):\n", " my_list.append(5)\n", " \n", "l = [1,2,3]\n", "my_func(l)\n", "print(l)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Can be very surprising!\n", "\n", "Define a better function `my_func_2`:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def my_func_2(my_list):\n", "\n", " return my_list + [5]" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3]\n", "[1, 2, 3, 5]\n" ] } ], "source": [ "l = [1,2,3]\n", "l2 = my_func_2(l)\n", "print(l)\n", "print(l2)" ] } ], "metadata": {}, "nbformat": 4, "nbformat_minor": 0 }