From 8e9944fd0e89f0fc79eccefa3c055070b8d126e6 Mon Sep 17 00:00:00 2001 From: Martijn Vermaat <martijn@vermaat.name> Date: Tue, 23 Jul 2013 16:57:52 +0200 Subject: [PATCH] String formatting and modules --- python-intro/fibonacci.py | 10 + python-intro/python-intro.ipynb | 325 +++++++++++++++++++++++++++++--- 2 files changed, 311 insertions(+), 24 deletions(-) create mode 100644 python-intro/fibonacci.py diff --git a/python-intro/fibonacci.py b/python-intro/fibonacci.py new file mode 100644 index 0000000..75da24c --- /dev/null +++ b/python-intro/fibonacci.py @@ -0,0 +1,10 @@ +def fib(n): + if n == 0: + return 0 + if n == 1: + return 1 + return fib(n - 1) + fib(n - 2) + +if __name__ == '__main__': + for n in range(5, 10): + print 'fib({0}) = {1}'.format(n, fib(n)) diff --git a/python-intro/python-intro.ipynb b/python-intro/python-intro.ipynb index 5f2de08..3715a7a 100644 --- a/python-intro/python-intro.ipynb +++ b/python-intro/python-intro.ipynb @@ -198,7 +198,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "* Statement by statement directly in the interpreter" + "* Statement by statement directly in the interpreter." ] }, { @@ -213,7 +213,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "* By editing in a file running the code afterwards" + "* By editing in a file and running the code afterwards." ] }, { @@ -1065,7 +1065,7 @@ } }, "source": [ - "Mutable ordered sequences of values." + "Mutable sequences of values." ] }, { @@ -1144,7 +1144,7 @@ } }, "source": [ - "Immutable ordered sequences of values." + "Immutable sequences of values." ] }, { @@ -1206,7 +1206,7 @@ "level": 2, "metadata": {}, "source": [ - "Strings" + "Strings (1/2)" ] }, { @@ -1217,14 +1217,14 @@ } }, "source": [ - "The special formatting operator `%` replaces conversion specifications by values." + "Immutable sequences of characters." ] }, { "cell_type": "code", "collapsed": false, "input": [ - "'%s times %i equals %.3f' % ('pi', 2, 6.283185307179586)" + "'a string can be written in single quotes'" ], "language": "python", "metadata": {}, @@ -1232,13 +1232,13 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 30, + "prompt_number": 108, "text": [ - "'pi times 2 equals 6.283'" + "'a string can be written in single quotes'" ] } ], - "prompt_number": 30 + "prompt_number": 108 }, { "cell_type": "markdown", @@ -1289,13 +1289,101 @@ ], "prompt_number": 32 }, + { + "cell_type": "heading", + "level": 1, + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "Sequence types" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Strings (2/2)" + ] + }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Strings are immutable." + "A common operation is formatting strings using argument substitutions." ] }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'{} times {} equals {:.2f}'.format('pi', 2, 6.283185307179586)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 117, + "text": [ + "'pi times 2 equals 6.28'" + ] + } + ], + "prompt_number": 117 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Accessing arguments by position or name is more readable." + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'{1} times {0} equals {2:.2f}'.format('pi', 2, 6.283185307179586)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 116, + "text": [ + "'2 times pi equals 6.28'" + ] + } + ], + "prompt_number": 116 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'{number} times {amount} equals {result:.2f}'.format(number='pi', amount=2,\n", + " result=6.283185307179586)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 118, + "text": [ + "'pi times 2 equals 6.28'" + ] + } + ], + "prompt_number": 118 + }, { "cell_type": "heading", "level": 1, @@ -3609,34 +3697,205 @@ } }, "source": [ - "Exceptions" + "Modules" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Non-interactive programming" ] }, { "cell_type": "markdown", + "metadata": {}, + "source": [ + "* Real work needs to be saved to files.\n", + "* Larger programs need modular development.\n", + "* A `.py` file (with Python code) is called a module.\n", + "\n", + "Example:" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "cat fibonacci.py" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "def fib(n):\r\n", + " if n == 0:\r\n", + " return 0\r\n", + " if n == 1:\r\n", + " return 1\r\n", + " return fib(n - 1) + fib(n - 2)\r\n", + "\r\n", + "if __name__ == '__main__':\r\n", + " for n in range(5, 10):\r\n", + " print 'fib({0}) = {1}'.format(n, fib(n))\r\n" + ] + } + ], + "prompt_number": 130 + }, + { + "cell_type": "heading", + "level": 1, "metadata": { "slideshow": { - "slide_type": "-" + "slide_type": "subslide" } }, "source": [ - "Todo.\n", - "\n", - "* Handling exceptions with ``try...except``." + "Modules" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Using code from a module" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You can import a module into your code." ] }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import fibonacci\n", + "fibonacci.fib(7)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 122, + "text": [ + "13" + ] + } + ], + "prompt_number": 122 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Or you can run the module directly." + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "%%sh\n", + "python fibonacci.py" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "fib(5) = 5\n", + "fib(6) = 8\n", + "fib(7) = 13\n", + "fib(8) = 21\n", + "fib(9) = 34\n" + ] + } + ], + "prompt_number": 131 + }, { "cell_type": "heading", "level": 1, "metadata": { "slideshow": { - "slide_type": "slide" + "slide_type": "subslide" } }, "source": [ "Modules" ] }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Working with files in IPython" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You can run a module directly in IPython." + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "%run fibonacci.py" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "fib(5) = 5\n", + "fib(6) = 8\n", + "fib(7) = 13\n", + "fib(8) = 21\n", + "fib(9) = 34\n" + ] + } + ], + "prompt_number": 135 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "fib(12)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 136, + "text": [ + "144" + ] + } + ], + "prompt_number": 136 + }, { "cell_type": "markdown", "metadata": { @@ -3645,33 +3904,50 @@ } }, "source": [ - "Todo.\n", - "\n", - "- Edit code in a file and running it with ``%ed filename.py``.\n", - "- Running it with ``python filename.py``." + "You can edit and run a file with `%edit`." ] }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "%edit fibonacci.py" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 134 + }, { "cell_type": "heading", "level": 1, "metadata": { "slideshow": { - "slide_type": "skip" + "slide_type": "slide" } }, "source": [ - "Extras" + "Todo" ] }, { "cell_type": "markdown", + "metadata": {}, + "source": [ + "* Handling exceptions with `try ... except`.\n", + "* Interpreting a stack trace." + ] + }, + { + "cell_type": "heading", + "level": 1, "metadata": { "slideshow": { "slide_type": "skip" } }, "source": [ - "Some style overrides and a table of contents." + "Extras" ] }, { @@ -3682,6 +3958,7 @@ } }, "source": [ + "Some style overrides.\n", "<style type=\"text/css\">\n", ".reveal {\n", " overflow-y: hidden;\n", -- GitLab