{ "metadata": { "name": "", "signature": "sha256:a8f06c438400da26901e1ce3130ea8504c4d90f8f64cd4acad3c2efd761e8991" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Introduction to Python 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "May, 2014\n", "\n", "Chang Y. Chung" ] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Why Python?" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "* Popular\n", "* Easy to lean and use\n", "* Open-source\n", "* General-purpose\n", "* Multi-paradigm\n", "* Hettinger, \"What makes Python Awesome?\" [2] [http:/tinyurl.com/mndd4er](http://tinyurl.com/mndd4er)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "* Did I mention _popular_?" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Running Python REPL\n", "-------------------\n", "\n", "For those who are using UNIX-like systems, including the Ubuntu image running on a Virtual Box.\n", "\n", "Type \"python\" at the terminal shell prompt.\n", "\n", "![](files/graphics/vmrepl.png)" ] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Running a Python Script File (.py)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Type \"python\" followed by the script file name.\n", "\n", "![](files/graphics/vmscript.png)" ] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Running Python REPL on Windows" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For those who are using Windows system with Python (and IDLE) installed.\n", "\n", "Run the IDLE application.\n", "\n", "![](files/graphics/winrepl.png)" ] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Running a Python Script File (.py) on Windows" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "File > New (or Open) brings up a script window.\n", "\n", "Run > Run Module (Output will show in IDLE shell).\n", "\n", "![](files/graphics/winscript.png)" ] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Using IPython Notebook in a Web Browser" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![](files/graphics/ipynb.png)" ] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Quiz" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Print out a \"Hello\" in your environment." ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"hello\"" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "* Print out \"Hello\" 20 times." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# here is one way\n", "print \"Hello \" * 20" ], "language": "python", "metadata": { "slideshow": { "slide_type": "skip" } }, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Conceptual Hierarchy by Mark Lutz[3]" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "* Programs are composed of _modules_.\n", "* Modules contain *statements*.\n", "* Statements contain *expressions*.\n", "* Expressions create and process *objects*." ] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Script File (.py)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "* A script file is a module.\n", "* A script is a sequence of statements, delimited by a newline (or the end of line character).\n", "* Python executes one statement at a time, from the top of a script file to the bottom.\n", "* Execution happens in *namespaces* (modules, classes, functions all have their own)." ] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Executable Python Script File" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "On a Unix-like system, we can change the mode of the script file and make it executable:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n",
      "$ chmod +x hellp.py\n",
      "$ ./hello.py\n",
      "
" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Just add the first line with a hashbang(#!):" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#!/usr/bin/python\n", "\n", "# say hello to the world\n", "def main():\n", " print \"Hello, World!\"\n", "\n", "if __name__ == \"__main__\":\n", " main()" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Comments" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Comments start with a hash (#) and end with a newline." ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "# this whole line is a comment\n", "\n", "# add 10 integers.\n", "total = 0\n", "\n", "for i in range(10): # 0, 1, 2, ..., 9\n", " total += i # total = total + i\n", " \n", "print \"total =\", total" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Variables" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Variables are created when first assigned a value." ] }, { "cell_type": "code", "collapsed": false, "input": [ "my_var = 3\n", "answer_to_everything = 42\n", "\n", "# also works are:\n", "x = y = 0\n", "a, b, c = 1, 2, 3" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Variable names start with a letter or an underscore and can have letters, underscores, or digits.\n", "\n", "* Variable names are case-sensitive." ] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Assignment Semantics According to David Godger[1]" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "Variables in many _other languages_ are a container that stores a value." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "
\n",
      "int a = 1;\n",
      "
" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "![](files/graphics/a1box.png)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "In Python, an assignment creates an object, and labels it with the variable." ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = 1" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "![](files/graphics/a1tag.png)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "If you assign another value to a, then the variable labels the new value (2)." ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = 2" ], "language": "python", "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "![](files/graphics/1.png)\n", "![](files/graphics/a2tag.png)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "This is what happens if you assign a to a new variable b:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "b = a" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "![](files/graphics/1.png)\n", "![](files/graphics/ab2tag.png)" ] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Integers" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x = 0\n", "age = 20\n", "size_of_household = 5\n", "\n", "print type(age)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# can handle arbitrarily large numbers\n", "huge = 10 ** 100 + 1\n", "huge" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Floating-point Numbers" ] }, { "cell_type": "code", "collapsed": false, "input": [ "g = 0.1\n", "f = 6.67384" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is the dot (.) that makes it a float." ] }, { "cell_type": "code", "collapsed": false, "input": [ "velocity = 1.\n", "print velocity" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "type(velocity)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Most of the arithmetic operators behave as expected" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = 10\n", "b = 20\n", "print a - (b **2) + 23" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "x = 2.0\n", "print x / 0.1" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Watch out for integer divisions. In Python 2, it _truncates down_ to an integer." ] }, { "cell_type": "code", "collapsed": false, "input": [ "print 10 / 3 # 3.33... (in Python 3)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print -10 / 3 # -3.33... (in Python 3)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "A solution: use floating point numbers" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print 10.0 / 3.0" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Quiz" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* What is the remainder of 5 divided by 2?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "dividend = 5\n", "divisor = 2\n", "\n", "quotient = dividend / divisor\n", "remainder = dividend - (quotient * divisor)\n", "\n", "print remainder" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "* What is the remainder of 2837465 divided by 2834?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print 2837465 % 2834" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "String Literals" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* A string is a sequence of characters.\n", "* Either double(`\"`) or single(`'`) quotes for creating string literals." ] }, { "cell_type": "code", "collapsed": false, "input": [ "name = \"Changarilla Dingdong\"\n", "file_name = 'workshop.tex'" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "* Triple-quoted strings can span multiple lines." ] }, { "cell_type": "code", "collapsed": false, "input": [ "starwars = \"\"\"\n", "A long time ago is a galaxy far, far away...\n", "\n", "It is a period of civil war. Rebel\n", "spaceships, striking from a hidden\n", "base, have won their first victory\n", "...\n", "\n", "What is the last character of this string?\n", "\"\"\"\n", "last_char = starwars[-1]\n", "print ord(last_char), ord(\"\\n\")" ], "language": "python", "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Working with strings" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Strings are immutable." ] }, { "cell_type": "code", "collapsed": false, "input": [ "s = \"abcde\"\n", "s[0] = \"x\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "* We can always create a new string instead." ] }, { "cell_type": "code", "collapsed": false, "input": [ "t = \"x\" + s[1:]\n", "print t" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "* Many functions and methods are available." ] }, { "cell_type": "code", "collapsed": false, "input": [ "s = \"abcde\"\n", "print s + s # concatenation" ], "language": "python", "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print len(s)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "* Index is 0-based. `find` returns -1 if not found." ] }, { "cell_type": "code", "collapsed": false, "input": [ "print s.find(\"c\")" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "String Manipulation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A few more string methods." ] }, { "cell_type": "code", "collapsed": false, "input": [ "s = \"abcde\"\n", "print s.upper(), \"XYZ\".lower()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print \" xxx yy \".strip()" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"a,bb,ccc\".split(\",\")" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "What are *methods* anyway?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Functions which are a member of a type (or class)." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "* int, str, Word Document are types (or classes)." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "![](files/graphics/methods.png)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "* 2, \"abcde\", diary.docx\" are an _instance_ (or _object_) of the respective type." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "* Types have members: properties (data) and methods (functions)." ] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Two ways to Format" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "* `format()` method" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"The answer is {0}\".format(21)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"The real answer is {0:6.4f}\".format(21.2345678)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "* formatting operator" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"The answer is %d\" % 21" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"The real answer is %6.4f\" % 21.2345678" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Raw Strings" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Within a string literal, escape sequences start with a backslash." ] }, { "cell_type": "code", "collapsed": false, "input": [ "a_string = 'It\\'s a great day\\nto learn \\\\Python\\\\. \\n 1\\t 2\\t 3'\n", "print a_string" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "A _raw_ string literal starts with the prefix r. In a raw string, the backslash is not special. It is great for writing reg ex patterns." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import re\n", "\n", "p = re.compile(r\"\\d\\d\\d-\\d\\d\\d\\d\")\n", "m = p.match(\"123-4567\")\n", "if m is not None:\n", " print m.group()" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Unicode Strings" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can create Unicode strings using the u prefix and slash-u escape sequences. Write them with: \\u followed by a hexadecimal value, xxxx." ] }, { "cell_type": "code", "collapsed": false, "input": [ "a_string = u\"Euro \\u20AC\"\n", "print a_string, len(a_string)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Unicode?" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "* Unicode strings are sequences of _code points_.\n", "* Code points are numbers, each representing a \"character\" e.g., U+00612 is \"Latin small letter a\".\n", "* Unicode text strings are _encoded_ into bytes. UTF-8 is one of many Unicode encodings, using one to four bytes to store a Unicode \"character\".\n", "* Once you have Unicode strings in Python, all the string functions and properties work as expected." ] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Best Practices According to Thomas Wouters[5]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Never mix Unicode and bytecode (i.e., ordinary) strings.\n", "* Decode bytecode strings on input.\n", "* Encode unicode strings on output.\n", "* Try automatic conversion (`codecs.open()`)\n", "* Pay attention to exceptions, `UnicodeDecodeError` and `UnicodeEncodeError`" ] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "A Unicode example:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "ustr = u\"Euro \\u20AC\" # Euro symbol\n", "print ustr" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python's default encoding codec is 'ascii'." ] }, { "cell_type": "code", "collapsed": false, "input": [ "ustr.encode()" ], "language": "python", "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Encoding to UTF-8 works fine. This takes 8 bytes: 5 one-byte's and 1 three-byte." ] }, { "cell_type": "code", "collapsed": false, "input": [ "utf_8 = ustr.encode(\"UTF-8\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we want to decode to ascii, ignoring non-ascii characters." ] }, { "cell_type": "code", "collapsed": false, "input": [ "print utf_8.decode(\"ascii\", \"ignore\")" ], "language": "python", "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Quiz" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Say hello to Abe, Betty, and Carl -- write three print statements." ] }, { "cell_type": "code", "collapsed": false, "input": [ "template = \"Hello, {0}!\"\n", "print template.format(\"Abe\")\n", "print template.format(\"Betty\")\n", "print template.format(\"Carl\")" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "None" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Is a place-holder, like NULL in other languages." ] }, { "cell_type": "code", "collapsed": false, "input": [ "x = None" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "None is s a universal object, i.e, there is only one None." ] }, { "cell_type": "code", "collapsed": false, "input": [ "print None is None" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "* None is evaluated as False." ] }, { "cell_type": "code", "collapsed": false, "input": [ "x = None\n", "if x:\n", " print \"this will never print\"" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "None, however, is distinct from others which are False." ] }, { "cell_type": "code", "collapsed": false, "input": [ "print None is 0, None is False, None is []" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Core Data Types" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Basic code data types: int, float, and str\n", "* \"Python is dynamically, but _strongly_ typed.\"" ] }, { "cell_type": "code", "collapsed": false, "input": [ "n = 1.0\n", "print n + \"99\"" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "* Use `int()` or `float()` to go from str to numeric" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print n + float(\"99\")" ], "language": "python", "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "* str() returns the string representation of the given object." ] }, { "cell_type": "code", "collapsed": false, "input": [ "n = 1.0\n", "print str(n) + \"99\"" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Operators" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Python supports following types of operators:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " * Arithmetic (`+ - + / % ** //`)\n", " * Comparison (`== != > < >= <=`)\n", " * Assignment (`= += -= *= /= %= **= //=`)\n", " * Logical (`and or not`)\n", " * Bitwise (`& | ^ ~ << >>`)\n", " * Membership (`in not in`)\n", " * Identity (`is is not`)" ] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "A Few Surprises" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Power operator binds more tightly than unary operators on the left." ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = -2 ** 2\n", "print a" ], "language": "python", "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "* solution: parenthesize the base" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print (-2)**2" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "* Comparisons can be chained" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x = 1\n", "y = 3\n", "z = 3\n", "\n", "# y is evaluated only once below\n", "x < y <= z\n", "\n", "# but it is equivalent to:\n", "x < y and y <= z" ], "language": "python", "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "* Logical operators (and, or) short-circuit evaluation and return an _operand_." ] }, { "cell_type": "code", "collapsed": false, "input": [ "print 3 or 2" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Quiz" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Demographic and Health Surveys (DHS) Century Month Code (CMC)[4,p5] provides and easy way working with year and month.\n", "* The CMC is an integer representing a month, taking the value of 1 in January 1900, 2 in February 1900, ..., 13 in January 1901, etc. The CMC in February 2011 is 1333.\n", "* What is the CMC for this month, i.e., January, 2014?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# An answer:\n", "year = 2014\n", "month = 1\n", "print \"CMC\", (year - 1900) * 12 + month" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "* What is the month (and year) of CMC 1000?" ] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Quiz" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* According to U.S. National Debt Clock, the current outstanding public debt, as of a day last month, was a big number:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "debt = 17234623718339.96" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "* Count how many times the degit 3 appears in the number. (Hint: create a string variable and use the count() method of the string type.)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# An answer:\n", "sdebt = \"17234623718339.96\"\n", "print sdebt.count(\"3\")" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "* (tricky) It feels rather silly to rewrite the value as a string. Can you think of a way to _convert_ the number into a string?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# this may or may not work. \n", "s1 = str(debt)\n", "print s1.count(\"3\")" ], "language": "python", "metadata": { "slideshow": { "slide_type": "skip" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# this should not fail.\n", "s2 = \"{0:20.2f}\".format(debt)\n", "print s2.count(\"3\")" ], "language": "python", "metadata": { "slideshow": { "slide_type": "skip" } }, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Flow Control" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Conditional Execution (`if`)\n", "* Iteration\n", " * `while` loop\n", " * `for` loop" ] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "If Statement" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* If statement is used for conditional execution." ] }, { "cell_type": "code", "collapsed": false, "input": [ "x = 4\n", "if x > 0:\n", " print \"x is positive\"\n", "else:\n", " print \"x is zero or negative\"" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "* Only one suite (block of statements) under a True conditional expression is executed." ] }, { "cell_type": "code", "collapsed": false, "input": [ "me = \"rock\"\n", "wins = 0\n", "\n", "you = \"scissors\"\n", "\n", "if you == \"paper\":\n", " print \"You win!\"\n", " \n", "elif you == \"scissors\":\n", " print \"I win!\"\n", " wins += 1\n", " \n", "else:\n", " print \"draw\"" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Compound Statements" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* If, while, and for are _compound_ statements, which have one or more _clauses_. A clause, in turn, consists of a _header_ that ends with a colon and a _suite_.\n", "* A suite, a block of statements, is identified by _indentation_." ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = 1 # 1 \n", "if a > 0: # 2\n", " desc = \"a is positive\" # 3\n", " print a, desc # 4\n", " # 5\n", "print \"done\" # 6" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "* Lines 3 and 4 being \"in-dented\" signals a suite (or a block).\n", "* Line 5, a blank line, is ignore.\n", "* Line 6 being \"de-dented\" signals the end of the block of lines 3 and 4." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "The following raises an `IndentationError`." ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"another\" # 1\n", " # 2\n", " if a > 0: # 3\n", "desc = \"a is positive\" # 4" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Below is syntactically OK, just not so stylish." ] }, { "cell_type": "code", "collapsed": false, "input": [ "if a > 0: # 1\n", " desc = \"a > 0\" # 2\n", " print desc # 3\n", "else: # 4\n", " print \"a <= 0\" # 5" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "More on indentation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* The amount of indentation does not matter (as long as the same within a level). Four spaces (per level) and no tabs are the convention.\n", "* Use editor's python mode, which prevents/converts TAB to (four) spaces.\n", "* Why indentation? A good answer at: [http://tinyurl.com/kxv9vts](http://tinyurl.com/kxv9vts).\n", "* For an empty suite, use pass statement, which does nothing." ] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "While" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Repeats a block of statements as long as the condition remains True." ] }, { "cell_type": "code", "collapsed": false, "input": [ "total = 0\n", "n = 0\n", "while n < 5:\n", " print n, total\n", " n += 1\n", " total += n" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "* `break` terminates a loop immediately.\n", "* `continue` skips the remainder of the block and goes back to the test condition at the top." ] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "For" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* `for` is used to iterate over a sequence." ] }, { "cell_type": "code", "collapsed": false, "input": [ "days = [\"Sunday\", \"Monday\", \"Tuesday\", \"Wednesday\",\n", " \"Thursday\", \"Friday\", \"Saturday\"]\n", "\n", "for day in days:\n", " if day == \"Friday\":\n", " print \"I am outta here.\"\n", " break\n", " print \"Happy\" + \" \" + day + \"!\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "* Another example:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "numbers = range(5)\n", "print numbers" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "for n in numbers:\n", " print n,\n", " if n % 2 == 0:\n", " print \"is even\"\n", " else:\n", " print \"is odd\"" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "File I/O" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* The built-in function, `open()`, returns a file type object, unless there is an error opening the file." ] }, { "cell_type": "code", "collapsed": false, "input": [ "in_file = open(\"code/yourfile.txt\", \"r\")\n", "out_file = open(\"code/myfile.txt\", \"w\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "* Once we get the file type object, then use its methods to read from, write to, or close a file." ] }, { "cell_type": "code", "collapsed": false, "input": [ "content = in_file.read()\n", "\n", "out_file.write(\"hello?\\n\")\n", "\n", "in_file.close()\n", "out_file.close()" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Reading a file one line at a time" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* with ensures that the file is closed when done." ] }, { "cell_type": "code", "collapsed": false, "input": [ "with open(\"code/lorem.txt\", \"r\") as f:\n", " for line in f:\n", " print line" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "* Creating a file and writing three lines." ] }, { "cell_type": "code", "collapsed": false, "input": [ "with open(\"code/small.txt\", \"w\") as f:\n", " f.write(\"first\\n\")\n", " f.write(\"second\\n\")\n", " f.write(\"third\") " ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Read line inclues the newline character." ] }, { "cell_type": "code", "collapsed": false, "input": [ "with open(\"code/small.txt\", \"r\") as f:\n", " for line in f:\n", " print line" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Function" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Defined with `def` and called by name followed by `()`." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def the_answer():\n", " return 42\n", "\n", "print the_answer()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Argument(s) can be passed." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def shout(what):\n", " print what.upper() + \"!\"\n", " \n", "shout(\"hello\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "* If the function returns nothing, then it returns None." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def shout(what):\n", " print what.upper() + \"!\"\n", " \n", "r = shout(\"hi\")\n", "print r" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "CMC again" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def cmc(year, month):\n", " ''' returns DHS Century Month Code ''' # doc string\n", " \n", " if year < 1900 or year > 2099:\n", " print \"year out of range\"\n", " return\n", "\n", " if month < 1 or month > 12:\n", " print \"month out of range\"\n", " return\n", "\n", " value = (year - 1900) * 12 + month \n", " return value\n", "\n", "print cmc(2014, 1) \n", "print cmc(2014, 15)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Local and Global Variables" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Within your function:\n", "\n", "* A new variable is _local_, and independent of the global variable with the same name, if any." ] }, { "cell_type": "code", "collapsed": false, "input": [ "x = 1 # global\n", "\n", "def my_func():\n", " x = 2 # local\n", " \n", "my_func()\n", "print x" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "* Both local and global variables can be read.\n", "* Global variables can be written to once _declared_ so." ] }, { "cell_type": "code", "collapsed": false, "input": [ "x = 1\n", "\n", "def my_func():\n", " global x\n", " x = 2\n", " \n", "my_func()\n", "print x" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Quiz" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Wirte a function that returns Body Mass Index (BMI) of an adult given weight in kilograms and height in meters. (Hint: BMI = weight(kg) / (height(m) squared). For instance, if a person is 70kg and 1.80m, then BMI is about 21.6)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def bmi(kg, m):\n", " return float(kg) / (m ** 2)\n", "\n", "print bmi(70, 1.80)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "* Re-write the bmi function so that it accepts height in feet and inches, and the weight in pounds. (Hint. Make foot, inch, and pound arguments. Convert them into local variables, kg and m, before calculating bmi to return.)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def bmi2(pound, foot, inch):\n", " kg = 0.453592 * pound\n", " m = 0.0254 * (12 * foot + inch)\n", " return bmi(kg, m)\n", "\n", "print bmi2(154.324, 5, 10.8661)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "skip" } }, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Importing a module" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* `import` command reads in a module, runs it (top-to-bottom) to create the module object.\n", "* Via the module object, you get access to its variables, functions, classes, ...\n", "* We've already seen an example of importing a standard regular expression module:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import re\n", "\n", "p = re.compile(r\"\\d\\d\\d-\\d\\d\\d\\d\")\n", "m = p.match(\"123-4567\")\n", "if m is not None:\n", " print m.group()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Another Example" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are many standard modules that come already installed, and be ready to be imported." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import math\n", "\n", "s = math.sqrt(4.0)\n", "print \"square root of 4.0 is {0:.2f}\".format(s)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "* You can selectively import as well." ] }, { "cell_type": "code", "collapsed": false, "input": [ "from math import sqrt\n", "\n", "print sqrt(9.0)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "* You can import your own Python script file (.py) the same way." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "\n", "sys.path.append('code/')\n", "import hello\n", "\n", "hello.main()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Quiz" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write a function such that, given a BMI value, returns the BMI category as a string. Recall the categories are:\n", "* Underweight: less than 18.5\n", "* Normal weight: 18.5 upto but not including 25\n", "* Overweight: 25 upto but not including 30\n", "* Obesity: 30 or greater\n", "For instance, the function should return a string \"Normal weight\", when it is called with an argument of, say 20. (Hint: use conditional statements, i.e., if ... elif ...)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def BMI_category(bmi):\n", " cat = \"\"\n", " if bmi < 18.5:\n", " cat = \"Underweight\"\n", " elif bmi < 25:\n", " cat = \"Normal weight\"\n", " elif bmi < 30:\n", " cat = \"Overweight\"\n", " else:\n", " cat = \"Obesity\"\n", " return cat\n", "\n", "# check\n", "print BMI_category(20)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "* Print out a BMI table showing several lines of a pair: a BMI value and its category. BMI value may start at 15 and go up by 3 up to 36. (Hint: use a loop.)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def BMI_table():\n", " print \"bmi\", \"category\"\n", " print \"-\" * 17\n", " bmi = 15\n", " while bmi <= 36:\n", " print \"\", bmi, BMI_category(bmi)\n", " bmi += 3\n", " \n", "# check\n", "BMI_table()" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Quiz" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Create a comma-separated values (.csv) file of the BMI table." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import csv\n", "\n", "with open(\"code/test.csv\", \"wb\") as f:\n", " my_writer = csv.writer(f)\n", " bmi = 15\n", " while bmi < 36:\n", " cat = BMI_category(bmi)\n", " my_writer.writerow([bmi, cat])\n", " bmi += 3\n", "\n", "with open(\"code/test.csv\", \"r\") as f:\n", " for line in f:\n", " print line," ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Summary" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Using Python interactively or by running a script.\n", "* Comments.\n", "* Variables and assignment semantics.\n", "* Core data types (int, float, str, None)\n", "* Operators.\n", "* Conditionals and looping.\n", "* Defining and calling functions.\n", "* Basic File I/O.\n", "* Importing a module." ] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "References" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Godger, D. Code like a pythonista: Idiomatic python. http://tinyurl.com/2cv9kg.\n", "* Hettinger, R. What makes Python Awesome? http://tinyurl.com/mndd4er.\n", "* Lutz, M. Learning Python, fifth ed. O\u2019Reilly Media, Sebastopol, CA, 2013.\n", "* MEASURE DHS Plus. Description of the Dempgraphic and Health Surveys Individual Recode Data File, version 1.0 ed., March 2008.\n", "* Wouters, T. Advanced python: (or understanding python) google tech talks. feb 21, 2007. http://www.youtube.com/watch?v=uOzdG3lwcB4." ] } ], "metadata": {} } ] }