{ "metadata": { "name": "Extra_01_Basic_Python_Extended" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Basic Python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is just to give you a glimpse of what Python can do.\n", "We select only subset of the feature we think will be useful for doing analysis. We also leave links in various place in case you want to do your own further study.\n", "\n", "For more complete features, you can look at Python [official documentation](http://docs.python.org/2/) or a book like [Think Python](http://www.greenteapress.com/thinkpython/html/index.html)\n", "\n", "In this tutorial we will be using [IPython](http://ipython.org). To execute current cell and go to the next cell in this tutorial press ``Shift+Enter``. If things go wrong you can restart Kernel by either click on Kernel at the top bar and choose restart or press `Ctrl+M+.` (Press that DOT symbol too)" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Hello World" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#press shift+enter to execute this\n", "print 'Hello world'" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Hello world\n" ] } ], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "#ipython automatically show representation of \n", "#the return value of the last command\n", "1+1" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 2, "text": [ "2" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Data Type(Usual Stuff)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x = 1 #integer\n", "y = 2.0 #float\n", "t = True #boolean (False)\n", "s = 'hello' #string\n", "s2 = \"world\" #double quotes works too\n", "#there are also triple quotes google python triple quotes\n", "n=None #Null like variable None." ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "print x+y #you can refer to previously assigned variable." ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "3.0\n" ] } ], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "s+' '+s2" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 5, "text": [ "'hello world'" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "#boolean operations\n", "x>1 and (y>=3 or not t) and not s=='hello' and n is None" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 6, "text": [ "False" ] } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "#Bonus: The only language I know that can do this\n", "0 < x < 10" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 7, "text": [ "True" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "####Bonus: String formatting\n", "One of the best implentation. There are couple ways to do string formatting in Python.\n", "This is the one I personally like." ] }, { "cell_type": "code", "collapsed": false, "input": [ "'x is %d. y is %f'%(x,y)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 8, "text": [ "'x is 1. y is 2.000000'" ] } ], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [ "#even more advance stuff\n", "#locals returns dictionary of\n", "#local variables which you then use \n", "#in formatting by name\n", "'x is %(x)d. y is %(y)f'%locals() #easier to read" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 9, "text": [ "'x is 1. y is 2.000000'" ] } ], "prompt_number": 9 }, { "cell_type": "markdown", "metadata": {}, "source": [ "##List, Set, Tuple, Dictionary, Generator" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "List\n", "----\n", "Think of it as std::vector++" ] }, { "cell_type": "code", "collapsed": false, "input": [ "l = [1, 2, 3, 4, 5, 6, 7]\n", "print l #[1, 2, 3, 4, 5, 6, 7]\n", "print l[2] #3\n", "print len(l) # list length\n", "print l[-1] #7 negative index works from the back (-1)\n", "l2 = [] #want an empty list?\n", "print l2" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[1, 2, 3, 4, 5, 6, 7]\n", "3\n", "7\n", "7\n", "[]\n" ] } ], "prompt_number": 10 }, { "cell_type": "code", "collapsed": false, "input": [ "#doesn't really need hold the same type\n", "#but don't recommend. You will just get confused\n", "bad_list = ['dog','cat',1,1.234]" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [ "l[1] = 10 #assignment\n", "l" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 12, "text": [ "[1, 10, 3, 4, 5, 6, 7]" ] } ], "prompt_number": 12 }, { "cell_type": "code", "collapsed": false, "input": [ "l.append(999) #append list\n", "l" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 13, "text": [ "[1, 10, 3, 4, 5, 6, 7, 999]" ] } ], "prompt_number": 13 }, { "cell_type": "code", "collapsed": false, "input": [ "#can be created from list com\n", "l.sort() #sort\n", "l" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 14, "text": [ "[1, 3, 4, 5, 6, 7, 10, 999]" ] } ], "prompt_number": 14 }, { "cell_type": "code", "collapsed": false, "input": [ "#searching O(N) use set for O(log(N))\n", "10 in l" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 15, "text": [ "True" ] } ], "prompt_number": 15 }, { "cell_type": "code", "collapsed": false, "input": [ "11 not in l" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 16, "text": [ "True" ] } ], "prompt_number": 16 }, { "cell_type": "code", "collapsed": false, "input": [ "#useful list function\n", "range(10) #build it all in memory" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 17, "text": [ "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" ] } ], "prompt_number": 17 }, { "cell_type": "code", "collapsed": false, "input": [ "#list comprehension\n", "#we will get to for loop later but for simple one\n", "#list comprehension is much more readable\n", "my_list = [2*x for x in l]\n", "print my_list\n", "my_list = [ (2*x,x) for x in range(10)]\n", "print my_list\n", "my_list = [3*x for x in range(10) if x%2==0]\n", "print my_list" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[2, 6, 8, 10, 12, 14, 20, 1998]\n", "[(0, 0), (2, 1), (4, 2), (6, 3), (8, 4), (10, 5), (12, 6), (14, 7), (16, 8), (18, 9)]\n", "[0, 6, 12, 18, 24]\n" ] } ], "prompt_number": 18 }, { "cell_type": "code", "collapsed": false, "input": [ "#This might come in handy\n", "[1]*10" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 19, "text": [ "[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]" ] } ], "prompt_number": 19 }, { "cell_type": "markdown", "metadata": {}, "source": [ "####Bonus: Python Autocomplete" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#in this cell try my_ and press tab\n", "#IPython knows about local variables and\n", "#can do autocomplete (remember locals()?)\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 20 }, { "cell_type": "code", "collapsed": false, "input": [ "#try type len( here\n", "#python can give you documentation/function signature etc.\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 21 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Tuple\n", "-----\n", "Think of it as immutable list" ] }, { "cell_type": "code", "collapsed": false, "input": [ "tu = (1,2,3) #tuple immutable list\n", "print tu\n", "tu2 = tuple(l) #convert list to tuple\n", "print tu2\n", "tu3 = 4,5,6 #parenthesis is actually optional but makes it more readable\n", "print tu3" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "(1, 2, 3)\n", "(1, 3, 4, 5, 6, 7, 10, 999)\n", "(4, 5, 6)\n" ] } ], "prompt_number": 22 }, { "cell_type": "code", "collapsed": false, "input": [ "#access\n", "tu[1]\n", "#you can't assign to it" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 23, "text": [ "2" ] } ], "prompt_number": 23 }, { "cell_type": "code", "collapsed": false, "input": [ "#tuple expansion\n", "x, y, z = tu\n", "print x #1\n", "print y #2\n", "print z #3\n", "print x, y, z #you can use tuple in print statement too\n", "\n", "x, y, z = 10, 20, 30#parenthesis is actually optional\n", "print z, y, x #any order" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1\n", "2\n", "3\n", "1 2 3\n", "30 20 10\n" ] } ], "prompt_number": 24 }, { "cell_type": "code", "collapsed": false, "input": [ "#useful for returning multiple values\n", "def f(x,y):\n", " return x+y, x-y #parenthesis is implied\n", "a, b = f(10,5)\n", "print a #15\n", "print b #5\n", "print a, b #works too" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "15\n", "5\n", "15 5\n" ] } ], "prompt_number": 25 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Dictionary\n", "----------\n", "\n", "Think of it as std::map - ish. It's actually a hash table. There is also OrderedDict if you also care about ordering." ] }, { "cell_type": "code", "collapsed": false, "input": [ "d = {'a':1, 'b':10, 'c':100}\n", "print d #{'a': 1, 'c': 100, 'b': 10}\n", "d2 = dict(a=2, b=20, c=200) #using named argument\n", "print d2 #{'a': 2, 'c': 200, 'b': 20}\n", "d3 = dict([('a', 3),('b', 30),('c', 300)]) #list of tuples\n", "print d3 #{'a': 3, 'c': 300, 'b': 30}\n", "d4 = {x:2*x for x in range(10)}#comprehension (key doesn't have to be string)\n", "print d4 #{0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18}\n", "d5 = {} #empty dict\n", "print d5 #{}" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "{'a': 1, 'c': 100, 'b': 10}\n", "{'a': 2, 'c': 200, 'b': 20}\n", "{'a': 3, 'c': 300, 'b': 30}\n", "{0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18}\n", "{}\n" ] } ], "prompt_number": 26 }, { "cell_type": "code", "collapsed": false, "input": [ "print d['a'] #access\n", "print len(d) #count element\n", "d['d'] = 1000#insert\n", "print d #{'a': 1, 'c': 100, 'b': 10, 'd': 1000}\n", "del d['c']#remove\n", "print d #{'a': 1, 'b': 10, 'd': 10}\n", "print 'c' in d #keyexists?" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1\n", "3\n", "{'a': 1, 'c': 100, 'b': 10, 'd': 1000}\n", "{'a': 1, 'b': 10, 'd': 1000}\n", "False\n" ] } ], "prompt_number": 27 }, { "cell_type": "code", "collapsed": false, "input": [ "#use dictionary in comprehension\n", "#d.items() return generator which gives tuple \n", "#k,v in d.items() does tuple expansion in disguise\n", "new_d = {k:2*v for k,v in d.items()}\n", "print new_d #{'a': 2, 'b': 20, 'd': 20}" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "{'a': 2, 'b': 20, 'd': 2000}\n" ] } ], "prompt_number": 28 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Set\n", "---\n", "Binary tree-ish. Very good at membership searching. O(log(N)) instead of O(N) in list." ] }, { "cell_type": "code", "collapsed": false, "input": [ "s = {1,2,3,4,5,6}\n", "print s\n", "s2 = set([4, 5, 6, 7, 8, 9, 9, 9, 9]) #from a list\n", "#duplicated element is ignored\n", "print s2" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "set([1, 2, 3, 4, 5, 6])\n", "set([4, 5, 6, 7, 8, 9])\n" ] } ], "prompt_number": 29 }, { "cell_type": "code", "collapsed": false, "input": [ "#membership this is O(log(n))\n", "print 3 in s\n", "print 10 in s\n", "print 11 not in s" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "True\n", "False\n", "True\n" ] } ], "prompt_number": 30 }, { "cell_type": "code", "collapsed": false, "input": [ "print s | s2 #union\n", "print s & s2 #intersection\n", "print s - s2 #differece\n", "print s ^ s2 #symmetric differnce\n", "print {2,3,4} <= s #subset\n", "print s >= {2,3,4} #superset" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "set([1, 2, 3, 4, 5, 6, 7, 8, 9])\n", "set([4, 5, 6])\n", "set([1, 2, 3])\n", "set([1, 2, 3, 7, 8, 9])\n", "True\n", "True\n" ] } ], "prompt_number": 31 }, { "cell_type": "code", "collapsed": false, "input": [ "#insert\n", "s.update([10,11,12])\n", "print s\n", "print 11 in s" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "set([1, 2, 3, 4, 5, 6, 10, 11, 12])\n", "True\n" ] } ], "prompt_number": 32 }, { "cell_type": "markdown", "metadata": {}, "source": [ "##Control Flow\n", "###if else elif\n", "Indentation in python is meaningful. There is \"NO\" bracket scoping in python.\n", "\n", "Recommended indentation is 4 spaces not Tab. Tab works but not recommended\n", "Set your text editor to soft tab. [PEP8](http://www.python.org/dev/peps/pep-0008/) which list all the\n", "recommended style: space, comma, indentation, new line, comment etc. Fun Read." ] }, { "cell_type": "code", "collapsed": false, "input": [ "x = 20\n", "if x>10: #colon\n", " print 'greater than 10'#don't for get the indentation\n", "elif x>5: #parenthesis is not really needed\n", " print 'greater than 5'\n", "else:\n", " print 'not greater than 10'\n", "x+=1#continue your execution with lower indentation\n", "print x" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "greater than 10\n", "21\n" ] } ], "prompt_number": 33 }, { "cell_type": "code", "collapsed": false, "input": [ "#shorthand if\n", "y = 'oh yes' if x>100 else 'oh no' #no colon\n", "print y" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "oh no\n" ] } ], "prompt_number": 34 }, { "cell_type": "code", "collapsed": false, "input": [ "#since indentation matters sometime we don't need any statement\n", "if x>10:\n", " print 'yes'\n", "else:\n", " pass #pass keywoard means do nothing\n", "x+=1\n", "print x" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "yes\n", "22\n" ] } ], "prompt_number": 35 }, { "cell_type": "code", "collapsed": false, "input": [ "#why is there no bracket??\n", "from __future__ import braces #easter egg" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "not a chance (, line 2)", "output_type": "pyerr", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m from __future__ import braces #easter egg\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m not a chance\n" ] } ], "prompt_number": 36 }, { "cell_type": "markdown", "metadata": {}, "source": [ "###For loop, While loop, Generator, Iterable\n", "\n", "There is actually no `for(i=0;i<10;i++)` in python. `list` is an example of iterable." ] }, { "cell_type": "code", "collapsed": false, "input": [ "#iterate over list\n", "for i in range(5): #not recommended use xrange instead\n", " print i#again indentation is meaningful" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n" ] } ], "prompt_number": 37 }, { "cell_type": "markdown", "metadata": {}, "source": [ "####Generator\n", "In previous example, we use `range`. But `range` will be evaluated \n", "right away and try to put ``[1,2,3,4,5]`` in the memory\n", "\n", "This is bad if you try to loop over large number. `for i in range(100000)` will put 100000 numbers in to the memory first. This is very inefficient since you use each one of them once.\n", "\n", "To fix this we use generator instead. As far as we are concern they are\n", "object that spit out number only when ask and doesn't keep it's previous\n", "states which means no access by index nor going backward. You can read more about it from [python wiki](http://wiki.python.org/moin/Generators). Or just google for python `yield` keyword.\n", "\n", "Long story short just use `for i in xrange(5)` instead" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Lazy programming\n", "#save memory\n", "for i in xrange(5):\n", " print i" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n" ] } ], "prompt_number": 38 }, { "cell_type": "code", "collapsed": false, "input": [ "#looping the list\n", "l = ['a','b','c']\n", "for x in l:\n", " print x" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "a\n", "b\n", "c\n" ] } ], "prompt_number": 39 }, { "cell_type": "code", "collapsed": false, "input": [ "#you can build your own generator too\n", "l = [1,2,3,4]\n", "#(2*y for y in l) is a generator that split out 2*y\n", "#for every element in l\n", "#not really a good way to write it but just to show it\n", "for x in (2*y for y in l):#notice the brackets\n", " print x" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "2\n", "4\n", "6\n", "8\n" ] } ], "prompt_number": 40 }, { "cell_type": "code", "collapsed": false, "input": [ "#if you need index\n", "for i,x in enumerate(l):\n", " print i,x" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0 1\n", "1 2\n", "2 3\n", "3 4\n" ] } ], "prompt_number": 41 }, { "cell_type": "code", "collapsed": false, "input": [ "#looping dictionary\n", "d = {'a':1,'b':10,'c':100}\n", "#items() returns a generator which return tuple\n", "#k,v in d.items() is tuple expansion in disguise\n", "for k,v in d.items():\n", " print k,v" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "a 1\n", "c 100\n", "b 10\n" ] } ], "prompt_number": 42 }, { "cell_type": "code", "collapsed": false, "input": [ "#looping over multiple list together\n", "lx = [1,2,3]\n", "ly = [x+1 for x in l]\n", "print l,l2\n", "for x,y in zip(lx,ly): #there is also itertools.izip that does generator\n", " print x,y" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[1, 2, 3, 4] []\n", "1 2\n", "2 3\n", "3 4\n" ] } ], "prompt_number": 43 }, { "cell_type": "code", "collapsed": false, "input": [ "#complete the list with while loop\n", "x = 0\n", "while x<5:\n", " print x\n", " x+=1" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n" ] } ], "prompt_number": 44 }, { "cell_type": "markdown", "metadata": {}, "source": [ "####See Also\n", "For more complex looping you can look at [itertools](http://docs.python.org/2/library/itertools.html)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "###Function\n", "Functions in python is a first class object(except in a very few cases)." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def f(x, y): #remember the colon\n", " print 'x =',x #again indentation\n", " print 'y =',y\n", " return x+y\n", "f(10,20)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "x = 10\n", "y = 20\n" ] }, { "output_type": "pyout", "prompt_number": 45, "text": [ "30" ] } ], "prompt_number": 45 }, { "cell_type": "code", "collapsed": false, "input": [ "#python is dynamic typing language\n", "#specifically it's Duck Typing(wikipedia it. Fun Read.)\n", "#this means as long as it has the right signature\n", "#Python doesn't care\n", "f('hello','world')" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "x = hello\n", "y = world\n" ] }, { "output_type": "pyout", "prompt_number": 46, "text": [ "'helloworld'" ] } ], "prompt_number": 46 }, { "cell_type": "code", "collapsed": false, "input": [ "#you can pass it by name too\n", "#this is useful since you can't always remember the order\n", "#of the arguments\n", "f(y='y',x='x') # notice i put y before x" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "x = x\n", "y = y\n" ] }, { "output_type": "pyout", "prompt_number": 47, "text": [ "'xy'" ] } ], "prompt_number": 47 }, { "cell_type": "code", "collapsed": false, "input": [ "#default/keyword arguments\n", "def g(x, y, z='hey'):\n", " #one of the most useful function\n", " print locals()#return dictionary of all local variables\n", "g(10,20)\n", "g(10,20,30)#can do it positionally" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "{'y': 20, 'x': 10, 'z': 'hey'}\n", "{'y': 20, 'x': 10, 'z': 30}\n" ] } ], "prompt_number": 48 }, { "cell_type": "code", "collapsed": false, "input": [ "g(10,z='ZZZZ',y='YYYY') #or using keyword" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "{'y': 'YYYY', 'x': 10, 'z': 'ZZZZ'}\n" ] } ], "prompt_number": 49 }, { "cell_type": "code", "collapsed": false, "input": [ "def myfunc(x,y,z, long_keyword=\"111000\"):\n", " return None" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 50 }, { "cell_type": "code", "collapsed": false, "input": [ "#IPython knows about keyword arguments name try type this\n", "#myfunc(x, y, z, lon\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 51 }, { "cell_type": "markdown", "metadata": {}, "source": [ "####Be careful" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#in your programming life time you might be\n", "#you might be tempting to put a mutable object like list\n", "#as default argument. Just Don't\n", "def f(x,y,z=[]): #Don't do this\n", " pass\n", "def f(x,y,z=None):\n", " z = [] if z is None else z" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 52 }, { "cell_type": "markdown", "metadata": {}, "source": [ "It has to do with [closure](http://en.wikipedia.org/wiki/Closure_(computer_science)). If you wonder why, you can read [\u201cLeast Astonishment\u201d in Python: The Mutable Default Argument](http://stackoverflow.com/questions/1132941/least-astonishment-in-python-the-mutable-default-argument). " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "####Bonus\n", "This might comes in handy" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#arbitary number of argument C's va_arg\n", "def h(x,y,*arg,**kwd):\n", " print locals()\n", "h(10,20,30,40,50,custom_kwd='hey')" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "{'y': 20, 'x': 10, 'kwd': {'custom_kwd': 'hey'}, 'arg': (30, 40, 50)}\n" ] } ], "prompt_number": 53 }, { "cell_type": "code", "collapsed": false, "input": [ "#Bonus: more cool stuff.\n", "#argument expansion\n", "def g(x, y, z):\n", " print locals()\n", "t = (1,2,3)\n", "g(*t)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "{'y': 2, 'x': 1, 'z': 3}\n" ] } ], "prompt_number": 54 }, { "cell_type": "code", "collapsed": false, "input": [ "#If you know lambda calculus\n", "f = lambda x: x+1\n", "f(3)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 55, "text": [ "4" ] } ], "prompt_number": 55 }, { "cell_type": "markdown", "metadata": {}, "source": [ "###Classes, Object etc.\n", "\n", "Think about Object as pointer to object in C. This will answer so many question about whether we are passing by reference or value or is it copy or assignment. Internally, it actually is C pointer to struct." ] }, { "cell_type": "code", "collapsed": false, "input": [ "#define a class\n", "class MyClass:\n", " x = 1 #you can define a field like this\n", " \n", " #first argument is called self. It refers to itself\n", " #think of it as this keyword in C\n", " def __init__(self, y): #constructor\n", " self.y = y #or define it here\n", " \n", " def do_this(self, z):\n", " return self.x + self.y + z" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 56 }, { "cell_type": "code", "collapsed": false, "input": [ "a = MyClass(10)\n", "print a.do_this(100)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "111\n" ] } ], "prompt_number": 57 }, { "cell_type": "code", "collapsed": false, "input": [ "#press a. here for IPython autocomplete\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 58 }, { "cell_type": "code", "collapsed": false, "input": [ "#you can even add field to it\n", "a.z = 'haha'\n", "print a.z" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "haha\n" ] } ], "prompt_number": 59 }, { "cell_type": "code", "collapsed": false, "input": [ "#remember when I said think of it as C pointer??\n", "b = a\n", "b.x = 999 #change b\n", "print a.x #printing a.x not b.x" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "999\n" ] } ], "prompt_number": 60 }, { "cell_type": "code", "collapsed": false, "input": [ "#you may think you won't encounter it but...\n", "a = [1,2,3]\n", "b = a\n", "b[1]=10\n", "print a" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[1, 10, 3]\n" ] } ], "prompt_number": 61 }, { "cell_type": "code", "collapsed": false, "input": [ "#shallow copy is easy\n", "a = [1,2,3]\n", "b = a[:] #remember slicing? it creates a new list\n", "b[1] = 10\n", "print a, b" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[1, 2, 3] [1, 10, 3]\n" ] } ], "prompt_number": 62 }, { "cell_type": "markdown", "metadata": {}, "source": [ "####Inheritance\n", "Python support multiple inheritance aka [mixin](http://stackoverflow.com/questions/533631/what-is-a-mixin-and-why-are-they-useful). You can read about it [here](http://docs.python.org/2/tutorial/classes.html#inheritance)\n", "We won't need it in our tutorial. The basic syntax is the following." ] }, { "cell_type": "code", "collapsed": false, "input": [ "class Parent:\n", " x = 10\n", " y = 20\n", " \n", "class Child(Parent):\n", " x = 30\n", " z = 50\n", "\n", "p = Parent()\n", "c = Child()\n", "print p.x\n", "print c.x" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "10\n", "30\n" ] } ], "prompt_number": 63 }, { "cell_type": "markdown", "metadata": {}, "source": [ "####Bonus" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "####Everything in python is actually an Object.\n", "This includes string, integer, functions etc. I really mean it." ] }, { "cell_type": "code", "collapsed": false, "input": [ "#this might comes in handy\n", "'_'.join([\"let's\",'join','this'])" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 64, "text": [ "\"let's_join_this\"" ] } ], "prompt_number": 64 }, { "cell_type": "code", "collapsed": false, "input": [ "x =1 \n", "x.real" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 65, "text": [ "1" ] } ], "prompt_number": 65 }, { "cell_type": "code", "collapsed": false, "input": [ "def f(x,y):\n", " return x+y\n", "f.func_code.co_varnames #This is used in writing fitting library" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 66, "text": [ "('x', 'y')" ] } ], "prompt_number": 66 }, { "cell_type": "markdown", "metadata": {}, "source": [ "####Introspection" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#bonus introspection\n", "a = MyClass(10)\n", "dir(a)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 67, "text": [ "['__doc__', '__init__', '__module__', 'do_this', 'x', 'y']" ] } ], "prompt_number": 67 }, { "cell_type": "markdown", "metadata": {}, "source": [ "####Class Method(Static method-ish)\n", "If you need a classmethod(aka static-ish method), there is classmethod [decorator](http://wiki.python.org/moin/PythonDecorators). It's actually just a [Higher order function](http://en.wikipedia.org/wiki/Higher-order_function): a function that returns function (in disguise). But we won't go into details here." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "###Modularizing your code: import\n", "\n", "You normally want to put these imports at the top of your file." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import math #import module called math" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 68 }, { "cell_type": "code", "collapsed": false, "input": [ "print math.exp(2)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "7.38905609893\n" ] } ], "prompt_number": 69 }, { "cell_type": "code", "collapsed": false, "input": [ "from math import exp #import exp from math module\n", "print exp(3)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "20.0855369232\n" ] } ], "prompt_number": 70 }, { "cell_type": "code", "collapsed": false, "input": [ "#not recommended but\n", "from math import * #import every symbol in that module\n", "sin(100)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 71, "text": [ "-0.5063656411097588" ] } ], "prompt_number": 71 }, { "cell_type": "code", "collapsed": false, "input": [ "#if you hate typing\n", "#import it into a symbol\n", "import math as m\n", "m.exp(10)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 72, "text": [ "22026.465794806718" ] } ], "prompt_number": 72 }, { "cell_type": "markdown", "metadata": {}, "source": [ "####Writing Your Own Module\n", "For basic stuff: you just write your code and name it `my_module.py` put it in the same directory as the file you want to load my_module then you can do\n", " \n", " import my_module\n", "\n", "All functions and variables you declared in ``my_module.py`` will be in there. Python has pretty advance module system. You can read about it [here](http://docs.python.org/2/tutorial/modules.html)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "####Bonus: Module search path\n", "See [The Module Search Path](http://docs.python.org/2/tutorial/modules.html#the-module-search-path). Basically what it says is that it will look for `.py` file or directory with the same name as module you are trying to import in the following order.\n", "\n", "1. current directory\n", "2. stuff in PYTHON_PATH environment variable\n", "3. site-packages directory (those from python setup.py install)\n", "\n", "The path is stored in sys.path which you can manipulate at runtime\n", "\n", " import sys\n", " print sys.path\n", " sys.path.append(path_to_your_library)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "###Persistence: Read/Write File.\n", "Python support introspeciton so it can dump object to a file. Most popular way to do this is [pickle](http://docs.python.org/2/library/pickle.html). We won't go into details about it but if you need it just read about it. If you need to read and write a raw file, look [here](http://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "###Exception: raise, try catch finally\n" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def f(x):\n", " #we need x to be >0\n", " if x<0:\n", " raise RuntimeError('Dude!! watch what you are doing')\n", " return 10" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 73 }, { "cell_type": "code", "collapsed": false, "input": [ "f(-1)" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "RuntimeError", "evalue": "Dude!! watch what you are doing", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mf\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m\u001b[0m in \u001b[0;36mf\u001b[0;34m(x)\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;31m#we need x to be >0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m<\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mRuntimeError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Dude!! watch what you are doing'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 5\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0;36m10\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mRuntimeError\u001b[0m: Dude!! watch what you are doing" ] } ], "prompt_number": 74 }, { "cell_type": "code", "collapsed": false, "input": [ "#we can catch with with try except block\n", "try:\n", " f(-1)\n", "except RuntimeError as e:\n", " print e" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Dude!! watch what you are doing\n" ] } ], "prompt_number": 75 }, { "cell_type": "code", "collapsed": false, "input": [ "try:\n", " f(-1)\n", "except RuntimeError as e:\n", " print 'hoooooooooooo'\n", " print e\n", " raise #you can reraise it" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "RuntimeError", "evalue": "Dude!! watch what you are doing", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mf\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mRuntimeError\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32mprint\u001b[0m \u001b[0;34m'hoooooooooooo'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mprint\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m\u001b[0m in \u001b[0;36mf\u001b[0;34m(x)\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;31m#we need x to be >0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m<\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mRuntimeError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Dude!! watch what you are doing'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 5\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0;36m10\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mRuntimeError\u001b[0m: Dude!! watch what you are doing" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "hoooooooooooo\n", "Dude!! watch what you are doing\n" ] } ], "prompt_number": 76 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }