{ "metadata": { "name": "", "signature": "sha256:6890d9534d246de790d359583953ad5522e87432441965b2a6c9edc81e204476" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "A notebook demonstrating some key features of the python programming language.\n", "\n", "Draws particularly closely on ideas from [the Python Tutorial][1] and [the Python Regular Expression Tutorial][2]\n", "\n", "[1]: https://docs.python.org/3/tutorial/index.html\n", "[2]: https://docs.python.org/2/howto/regex.html\n", "\n", "You might also want to familiarize yourself with IPython notebooks, through sites like [this one][3] or [this one][4].\n", "\n", "[3]: https://blog.safaribooksonline.com/2013/12/12/start-ipython-notebook/\n", "[4]: http://www.randalolson.com/2012/05/12/a-short-demo-on-how-to-use-ipython-notebook-as-a-research-notebook/\n", "\n", "Don't forget [the argument sketch][5].\n", "\n", "[5]:https://www.youtube.com/watch?v=kQFKtI6gn9Y\n", "\n", "Matthew Stone; Feb 10, 2015; CS 195." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Numerical computations, specified interactively." ] }, { "cell_type": "code", "collapsed": false, "input": [ "9 / 2" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 1, "text": [ "4" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lists: specified using brackets." ] }, { "cell_type": "code", "collapsed": false, "input": [ "[1, 2, 3]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 2, "text": [ "[1, 2, 3]" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Tuples: fixed sequences, specified using parentheses." ] }, { "cell_type": "code", "collapsed": false, "input": [ "(\"a\", \"b\")" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 3, "text": [ "('a', 'b')" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Various ways of specifying string literals, using single quotes or double quotes, and using the `r` tag to control how special characters are treated." ] }, { "cell_type": "code", "collapsed": false, "input": [ "man1 = \"I'd like to have an argument please.\"\n", "man1" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 4, "text": [ "\"I'd like to have an argument please.\"" ] } ], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "man2 = 'I\\'d like to have an argument please.'\n", "man2" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 5, "text": [ "\"I'd like to have an argument please.\"" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "man3 = r\"I'd like to have an argument please.\"\n", "man3" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 6, "text": [ "\"I'd like to have an argument please.\"" ] } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "man4 = r'I\\'d like to have an argument please.'\n", "man4" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 7, "text": [ "\"I\\\\'d like to have an argument please.\"" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "A variety of operations on strings:\n", "- `len` (function): Computing the length of a string \n", "- `split` (method): Separating a string into tokens based on a simple pattern delimeter\n", "- indexing and slices using `[:]` notation" ] }, { "cell_type": "code", "collapsed": false, "input": [ "len(man1)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 8, "text": [ "36" ] } ], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [ "words = man1.split(' ')\n", "words" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 9, "text": [ "[\"I'd\", 'like', 'to', 'have', 'an', 'argument', 'please.']" ] } ], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [ "man1[2]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 10, "text": [ "'d'" ] } ], "prompt_number": 10 }, { "cell_type": "code", "collapsed": false, "input": [ "man1[-2]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 11, "text": [ "'e'" ] } ], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [ "man1[0:15]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 12, "text": [ "\"I'd like to hav\"" ] } ], "prompt_number": 12 }, { "cell_type": "code", "collapsed": false, "input": [ "man1[:8]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 13, "text": [ "\"I'd like\"" ] } ], "prompt_number": 13 }, { "cell_type": "code", "collapsed": false, "input": [ "man1[-7:]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 14, "text": [ "'please.'" ] } ], "prompt_number": 14 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Key concepts and constructs for flow of control in Python\n", "- Indentation defines blocks to organize code\n", "- Blocks begin with an operator ending in `:`\n", "- `if ... else ...` blocks for conditional execution\n", "- `for ...` blocks for iteration\n", "- generator expressions make iteration a very lightweight coding style.\n", "\n", "You can totally [geek out][1] on programming with generators!\n", "[1]:http://www.dabeaz.com/generators/" ] }, { "cell_type": "code", "collapsed": false, "input": [ "if man1[-1] == '.' :\n", " man1 = man1[:-1]" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 15 }, { "cell_type": "code", "collapsed": false, "input": [ "man1" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 16, "text": [ "\"I'd like to have an argument please\"" ] } ], "prompt_number": 16 }, { "cell_type": "code", "collapsed": false, "input": [ "for w in words :\n", " print w, \"!\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "I'd !\n", "like !\n", "to !\n", "have !\n", "an !\n", "argument !\n", "please. !\n" ] } ], "prompt_number": 17 }, { "cell_type": "code", "collapsed": false, "input": [ "'-- '.join(words)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 18, "text": [ "\"I'd-- like-- to-- have-- an-- argument-- please.\"" ] } ], "prompt_number": 18 }, { "cell_type": "code", "collapsed": false, "input": [ "'-- '.join(w.upper() + \" really \" for w in words if len(w) < 5 )" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 19, "text": [ "\"I'D really -- LIKE really -- TO really -- HAVE really -- AN really \"" ] } ], "prompt_number": 19 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Regular expressions, the mainstay of string programming in Python.\n", "\n", "Interface:\n", "- `compile` a regular expression (string) into a pattern\n", "- `search` or `match` a pattern against another string, returning `Match` objects\n", "- `group` method gives the matched string (or matched substrings for complex patterns)\n", "- `span` shows where in the original string the match was found" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import re" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 20 }, { "cell_type": "code", "collapsed": false, "input": [ "pat1 = r\"argument\"" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 21 }, { "cell_type": "code", "collapsed": false, "input": [ "p1 = re.compile(pat1)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 22 }, { "cell_type": "code", "collapsed": false, "input": [ "m1 = p1.search(man1)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 23 }, { "cell_type": "code", "collapsed": false, "input": [ "m1" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 24, "text": [ "<_sre.SRE_Match at 0x105a6b308>" ] } ], "prompt_number": 24 }, { "cell_type": "code", "collapsed": false, "input": [ "m1.group()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 25, "text": [ "'argument'" ] } ], "prompt_number": 25 }, { "cell_type": "code", "collapsed": false, "input": [ "m1.span()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 26, "text": [ "(20, 28)" ] } ], "prompt_number": 26 }, { "cell_type": "code", "collapsed": false, "input": [ "man1[20:28]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 27, "text": [ "'argument'" ] } ], "prompt_number": 27 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Key elements of regular expressions\n", "- fixed text\n", "- character classes that match a range of possibilities, like `\\\\s` for space, `.` for anything\n", "- repetitions, like `*` for any number of repetitions including 0\n", "- `()` to group subexpressions (which can then be repeated, reported, etc.)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "pat2 = r\"I'd like to\\s(.*)\\splease\"" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 28 }, { "cell_type": "code", "collapsed": false, "input": [ "p2 = re.compile(pat2)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 29 }, { "cell_type": "code", "collapsed": false, "input": [ "m2 = p2.search(man1)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 30 }, { "cell_type": "code", "collapsed": false, "input": [ "m2" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 31, "text": [ "<_sre.SRE_Match at 0x105a700a8>" ] } ], "prompt_number": 31 }, { "cell_type": "code", "collapsed": false, "input": [ "m2.group()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 32, "text": [ "\"I'd like to have an argument please\"" ] } ], "prompt_number": 32 }, { "cell_type": "code", "collapsed": false, "input": [ "m2.group(1)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 33, "text": [ "'have an argument'" ] } ], "prompt_number": 33 }, { "cell_type": "code", "collapsed": false, "input": [ "\"You can't \" + m2.group(1) + \"!\"" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 34, "text": [ "\"You can't have an argument!\"" ] } ], "prompt_number": 34 } ], "metadata": {} } ] }