# IPython log file get_ipython().magic(u'logstart') def foo(*args): print args foo(x=1) foo(1) foo(1, 3) foo foo() def foo(): x = 1 x foo() x def sub(x, y): return x - y sub(x=1, y=2) sub(y=2, x=1) d = dict(x=1, y=2) d sub(x=d['x'], y=d['y']) sub(**d) def sub(**kwargs): print kwargs sub() sub(1) sub(x=1) sub(x=1, abc=30)) sub(x=1, abc=30) def sub(x, **kwargs): print x print kwargs sub(1) sub(1, 2) sub(1, x=2) def sub(x, y=0, **kwargs): print x, y, kwargs sub(1, 2) sub(1, 2, y=2) sub(1, 2, 2) sub(z=2, 1, 2) lst = [1,3] d= dict() sub(*lst, **d) get_ipython().magic(u'hist ') sub(*lst, **d) d d['foo'] = 1 sub(*lst, **d) def generic(*args, **kwargs): print args print kwargs get_ipython().magic(u'pinfo zip') lst1 = range(10, 20) lst2 = range(20, 30) lst3 = range(30, 40) lst1 lst 2 lst2 zip(lst1, lst2, lst3) lst = [lst1, lst2, lst3] lst get_ipython().magic(u'pinfo zip') lst zip(lst[0], lst[1], lst[2]) zip(*lst) zip(*lst)[0] def app(x, lst=[]): lst.append(x) return lst app('foo') app('bar') lst app(1) app(1, [1,2,3])) app(1, [1,2,3]) app(1) app(1) app(1) app(1) def app(x, lst=[]): lst.append(x) return lst def app(x, lst=None): if lst is None: lst = [] lst.append(x) return lst def app(x, lst=None): if lst is None: lst = [] lst.append(x) return lst def app(x, lst=None): if lst is None: lst = [] lst.append(x) return lst x x = 1 def foo(): x = 2 print x foo() def foo(): print x foo() def foo(): print x x = 2 foo() def foo(): global x x = 1 print x x x = 10 x foo() x x = [0] def foo(): x[0] = 10 foo() x yy yy[1] = 1 def foo(): x = [23] x foo() x def foo(): if 3 < 2: x = 1 print x foo() 4/0 dir(__builtin__) [k for k in dir(__builtin__) if "Error" in k] try: x = 5 / 0 except ZeroDivisionError as err: print err try: x = 5 / 0 except ZeroDivisionError as err: pass try: x = 5 / 0 except ZeroDivisionError as err: pass else: print "no error occurred" try: x = 5 / 0 except ZeroDivisionError as err: pass else: print "no error occurred" finally: print "this always runs" def foo(): try: return 1 finally: print "This happens after the return statement" foo() raise try: x = 5/0 except Exception: print "Something bad happened but I can't fix it" raise def foo(): return 5/0 try: foo() except Exception: print "Something went wrong with foo" try: foo() except : print "Something went wrong with foo" try: foo3234e() except : print "Something went wrong with foo" try: foo3234e() except Exception: print "Something went wrong with foo" SyntaxError.__class__ SyntaxError.__class__.mro SyntaxError.__class__.mro() BaseException() BaseException.mro() Exception.mro() SyntaxError.mro() try: = except: print "what went wrong" try: = except Exception: print "what went wrong" try: print d['key'] except KeyError: d['key'] = 1 d import urllib fp = urllib.open("http://feeds.finance.yahoo.com/rss/2.0/headline?s=DELL®ion=US&lang=en-US") fp = urllib.urlopen("http://feeds.finance.yahoo.com/rss/2.0/headline?s=DELL®ion=US&lang=en-US") data = fp.read() data from xml.etree import ElementTree t = ElementTree.fromstring(data) t t.findall('link') t.getchildren() get_ipython().magic(u'pinfo t.findall') get_ipython().magic(u'pinfo t.iterfind') t.iterfind("link") list(t.iterfind("link")) channel = t.getchildren()[0] channel channel.getchildren() channel.findall("item") channel.findall("item")[0] el = channel.findall("item")[0] el.getchildren() el.find('title').text urllib.urlopen("http://finance.google.com/finance/info?client=ig&q=NASDAQ%3aGOOG").read() import json data = urllib.urlopen("http://finance.google.com/finance/info?client=ig&q=NASDAQ%3aGOOG").read() json.loads(data) data data.strip() data = data.strip() data = data.strip("/") data json.loads(data) lst = json.loads(data) lst def simple(): ass def simple(): pass def diff(x, y): return x - y def diff(x, y=0): return x - y def foo(*args): pass def foo(x, *args): pass foo() def foo(**kwargs): pass foo(2) foo(z=2) l = [1,2, 3] def foo(x, y, z): return x * y * z foo(*l) l = range(10) l l[-3:] foo(*l[-3:]) d = dict(x=10, y=2, z=3) foo(**d) d['zz'] = 10 foo(**d) def generic(*args, **kwargs): pass def foo(x=0): print x foo() def foo(x, y): return x/y try: foo(1, 0) except Exception: print "something bad happened" try: x = foo(1, 0) except Exception: print "something bad happened" x = 0 x try: x = foo(1, 0) except Exception: print "something bad happened" raise lst lst = range(20) lst newlst = [] for n in lst: if n % 5 == 0: newlst.append(n) newlst import this import testmodule x testmodule.x import sys sys.path sys.path.append("/tmp/foo") sys.path import testmodule as t t testmodule import testmodule t = testmoudule t = testmodule del(testmodule) t import testmodule import testmodule reload(testmodule) get_ipython().magic(u'pinfo reload') reload(testmodule) from testmodule import x x from testmodule import * x from testmodule import * y reload testmodule reload(testmodule) from testmodule import * y from testmodule import y as test_y get_ipython().system(u'ls -F *') get_ipython().system(u'less newfile.csv') get_ipython().system(u'ls -F *csv') get_ipython().system(u'less classmates.csv') get_ipython().magic(u'pinfo file') get_ipython().magic(u'pinfo open') fp = open("./classmates.csv", "r") fp.mode f.closed fp.closed get_ipython().magic(u'pinfo fp.seek') fp.readlines() fp.read() fp.seek() fp.seek(0) fp for line in fp: print line get_ipython().magic(u'pinfo fp.write') get_ipython().magic(u'pinfo fp.writelines') fp.seek(0) for line in file: tupe =line.strip().split(",") print tupe for line in fp: tupe =line.strip().split(",") print tupe fp.seek(0) for line in fp: tupe = tuple(line.strip().split(",")) print tupe lst newlst = [] for val in lst: if val % 5 == 0: newlst.append(val) newlst with open("classmates.csv") as fp: print fp.readlines() fp import StringIO StringIO.StringIO("asdf\nbar") fp = StringIO.StringIO("asdf\nbar") fp.mode fp.closed fp.readlines() def foo(): pass foo x = 1 lst = range(10) lst lst = lst[::-1] lstr lst lst.reverse() lst lst.reverse() lst lst.sort() lst lst = ['a', 'c', 'A'] lst.sort() lst get_ipython().magic(u'pinfo ord') get_ipython().magic(u'pinfo chr') ord("A") ord("a") lst = [(1, 2), (-1, 3), (2, 4)] lst.sort() lst lst = [(-1, 2), (-1, -1), (-1, 3), (2, 4)] lst.sort() lst get_ipython().magic(u'pinfo lst.sort') lst = [(-1, -1), (-1, 2), (-1, 3), (2, 4)] lst lst = [(-1, -1, 2), (-1, 2), (-1, 3), (2, 4)] lst.sort() lst get_ipython().magic(u'pinfo lst.sort') lst.sort() get_ipython().magic(u'pinfo sorted') sorted(lst) def foo(x): print x foo(4) y = 4 foo(y) foo(foo) lst def second_item(t): return t[1] sorted(lst, key=second_item) lst sorted(lst, key=second_item, reverse=True) lst = [dict(x=10, z="a"), dict(x=1), dict(x=3, abc="foo")] lst def x_value(d): return d.get('x') sorted(lst, key=x_value) lst newlst = [] for d in lst: newlst.append([d['x'], d]) newlst sorted(newlst) output = sorted(newlst) get_ipython().magic(u'pinfo sorted') cmp get_ipython().magic(u'pinfo cmp') get_ipython().magic(u'pinfo sorte') get_ipython().magic(u'pinfo sorted') get_ipython().magic(u'pinfo filter') lst def has_z(d): return "z" in d filter(has_z, lst) sort_abs get_ipython().magic(u'pinfo map') abs get_ipython().magic(u'pinfo abs') get_ipython().magic(u'pinfo map') def double_me(x): return x * 2 map(double_me, [1,2,3]) newlst = [] for num in [1,2,3]: newlst.append(num * 2) str.lower str.lower("Abc") "Abc".lower() get_ipython().magic(u'pinfo filter') filter(None, [0, 1, 2]) filter(None, ["simeon", "franklin", ""])